= track_changes
TrackChanges is a Rails plugin to facilitate tracking
changes made to an ActiveRecord model in your
Controller actions.
== Why?
I originally looked at the available auditing solutions
and it appeared that most of them were not thread-safe.
== Installation
gem install track_changes
== Configuration
You need to have an Audit class that has a polymorphic
belongs_to association to :audited. In
addition, the Audit model must have a user
attribute, and your controllers must respond to
current_user.
Example Audit class:
class Audit < ActiveRecord::Base
belongs_to :audited, :polymorphic => true
serialize :change_set
end
Example Audited class:
class Post < ActiveRecord::Base
has_many :audits, :as => :audited
end
You can also tweak some of the defaults by creating an initializer
in RAILS_ROOT/config/initializers/track_changes_configuration.rb
and calling TrackChanges::Initializer.instance which yields a
TrackChanges::Configuration object:
# These are the defaults anyways
TrackChanges::Initializer.instance do |c|
c.audit_assocation = :audits # Calls create! on this association method
c.current_user = :current_user # Controller is sent this method to obtain current user.
c.ignore_nil = true # Don't crash if a model is nil.
end
== Controller Example
In this example, after the `update` action is called,
the `@post` will be compared to a previous version, and
if there are any changes, an audit will be created.
class PostController < ApplicationController
include TrackChanges
before_filter :get_post, :except => [:index, :new]
# specify a single model
track_changes :post
# you can also specify multiple models
#track_changes :post1, :post2, ...
def update
if @post.update_attributes(params[:post])
flash[:notice] = "Success."
else
render :action => "edit"
end
end
# Normal controller actions
# ...
protected
def get_post
@post = Post.find(params[:id])
end
end
== COPYRIGHT
Copyright (c) 2008-2010 Matt Haley. See LICENSE for details.