Track Changes
Installation
Migration
class CreateTrackChangesTables < ActiveRecord::Migration
def change
create_table :track_changes_snapshots do |t|
t.references :record, :polymorphic => true
t.text :state
t.
end
create_table :track_changes_diffs do |t|
t.references :record, :polymorphic => true
t.text :from
t.text :to
t.string :action
t.string :changes_by
t.
end
end
end
Usage
class Person < ActiveRecord::Base
tracks_changes # may also pass an options hash
end
Options
By default all model attributes are tracked, except the primary_key, usually id
, created_at
, and updated_at
.
:only
accepts a field name or array of field names to track instead of the default fields:except
accepts a field name or array of field names to ignore:methods
accepts a field name or array of field names to track in addition to the default fields:track_timestamps
accepts a boolean, enabling or disabling tracking ofcreated_at
andupdated_at
:track_primary_key
accepts a boolean, enabling or disabling tracking of the model's primary key
Attribution
Changes can be attributed to a particular source. The source is saved as a string
in the :changes_by column
of the record. If given an instance of ActiveRecord::Base,
the record's id will be used.
# Model-level attribution
person.save(:changes_by => 'Joe Changems')
# Block-level attribution
TrackChanges.with_changes_attributed_to 'Joe Changems' do
person.save
end
# Controller-level attribution
class MyController < ApplicationController
attribute_changes_to :current_user
end