public
Description: acts_as_audited is an ActiveRecord extension that logs all changes to your models in an audits table.
Home | Edit | New

Home

acts_as_audited is an Active Record plugin that logs all modifications to your models in an audits table. It uses a polymorphic association to store an audit record for any of the model objects that you wish to have audited. The audit log stores the model that the change was on, the “action” (create, update, destroy), a serialzied hash of the changes, and optionally the user that performed the action.

Demo

Check out the demo app that shows off some of acts_as_audited’s features.

Installation

Install acts_as_audited as a plugin:

script/plugin install git://github.com/collectiveidea/acts_as_audited.git

Run the migration generator and migrate to add the audits table.

script/generate audited_migration add_audits_table
rake db:migrate

Auditing in Rails

If you’re using acts_as_audited within Rails, you can simply declare which models should be audited. acts_as_audited can also automatically record the user that made the change if your controller has a current_user method.

class ApplicationController < ActionController::Base
  audit User, List, Item
protected
  def current_user
    @user ||= User.find(session[:user])
  end
end

Customizing

To get auditing outside of Rails, or to customize which fields are audited within Rails, you can explicitly declare acts_as_audited on your models. The :except option allows you to specify one or more attributes that you don’t want to be saved in the audit log.

class User < ActiveRecord::Base
  acts_as_audited :except => [:password, :credit_card_number]
end

Contributing

Contributions are always welcome. Please report bugs or feature suggestions in LightHouse

acts_as_audited tips

If you are auditing your User model but you also want to be able to get a list of audit events that a particular user performed, you can’t do User.first.audits. This gives you the changes on that user.

So you may want to specify an explicit association. You need to override the polymorphic association:


class User < ActiveRecord::Base
  has_many :changes, :class_name => 'Audit', :as => :user
end

Now User.first.audits gives you changes TO that user. User.first.changes gives you changes BY that user.

Last edited by brandon, Tue Jan 27 21:02:53 -0800 2009
Home | Edit | New
Versions: