Module: ActionAuditor::Extensions::ActionController::ClassMethods

Defined in:
lib/action_auditor/extensions/action_controller.rb

Instance Method Summary collapse

Instance Method Details

#audit(*args, &block) ⇒ Object

Set up an audit trail for the next action defined. This is similar to desc in a Rake task: you call it immediately before the action.

Simple messages

This will log “hello, world” each time someone visits the index page.

class PostsController < ApplicationController
  audit "hello, world"
  def index
  end
end

Blocks

If you need to interpolate any information at runtime, you’ll need to use a block:

class PostsController < ApplicationController
  audit { "#{current_user} stopped by" }
  def index
  end
end

…otherwise the string will get evaluated when the controller class is defined.

Parameters

If you need to save any information besides a message, you can write a block that returns [message, params], where params is a hash of objects:

class PostsController < ApplicationController
  audit {[
    "{{user}} created {{post}}",
    { :user => current_user, :post => @post }
  ]}
  def create
    @post = Post.create(params[:post])
  end
end

Notice that you can interpolate these parameters within your log message.

It’s up to the individual auditors how your parameters are saved — and, indeed, whether they’re saved at all. An ActiveRecord implementation might serialize the objects, or just their IDs, while a text-only auditor might discard them completely. You should not rely on having access to these parameters later on, unless you know how and where they are saved.



52
53
54
55
56
# File 'lib/action_auditor/extensions/action_controller.rb', line 52

def audit(*args, &block)
  options = args.last.is_a?(Hash) ? args.pop : {}
  block_or_message = block_given? ? block : args.shift
  @pending_auditor = [ args, options, block_or_message ]
end

#method_added_with_auditing(name) ⇒ Object

:nodoc:



58
59
60
61
62
63
64
# File 'lib/action_auditor/extensions/action_controller.rb', line 58

def method_added_with_auditing(name) #:nodoc:
  method_added_without_auditing(name)
  if @pending_auditor
    auditors[name.to_sym] = @pending_auditor
    @pending_auditor = nil
  end
end