Module: AccessLogging::Controller::ClassMethods

Defined in:
lib/access_logging/controller.rb

Instance Method Summary collapse

Instance Method Details

#log_access_to(model, opts = {}) ⇒ Object

example usage:

class ReportsController < ActionController::Base
  log_access_to :report, through: :print

  # restful actions here...

  def print
    @report = Report.find(params[:id])
  end
end

Also depends on the presence of a ‘current_anyone` method (probably on ApplicationController) to determine the user.



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/access_logging/controller.rb', line 22

def log_access_to(model, opts={})
  after_filter(only: :index) do
    log_access
  end

  after_filter only: [:show, :create, :edit, :update, :destroy] do
    log_access instance_variable_get("@#{model}")
  end

  if opts[:through]
    opts[:through] = [ *opts[:through] ]
    opts[:through].each do |action|
      after_filter only: action do
        log_access instance_variable_get("@#{model}"), "#{action}ed"
      end
    end
  end
end