module SimpleActivity::ControllerMethods

Public: To be used on controller. The module is supposed to be included in ActionController::Base

Public Class Methods

included(base) click to toggle source

included do

after_filter :record_activity, only: [:create, :update, :destroy]

end

# File lib/simple_activity/controller_methods.rb, line 14
def self.included(base)
  base.after_filter :record_activity, only: SimpleActivity.allowed_actions
end

Private Instance Methods

process_activity(target) click to toggle source
# File lib/simple_activity/controller_methods.rb, line 54
def process_activity(target)
  activity = ::SimpleActivity::ActivityProcessor.new(self, target)
  activity.save
end
record_activity(target=nil) click to toggle source

The main method to log activity.

By default it is used as an after_filter

If after_filter disabled, it can be called without arguments

# ArticlesController
def create
  @article = Article.create(params[:article])
  if @article.save
    record_activity
  end
end

target argument is needed if the instance is not the convention (the sigularize of controller name)

# ArticlesController
def create
  @article_in_other_name = Article.create(params[:article])
  if @article_in_other_name.save
    record_activity(@article_in_other_name)
  end
end

@param target [Object] the target instance variable. If nil, the processor

will build it according to mathcing instance variable in controller
automatically
# File lib/simple_activity/controller_methods.rb, line 48
def record_activity(target=nil)
  unless controller_name.match SimpleActivity.filtered_controllers
    process_activity(target)
  end
end