Module: ActsAsRevisionable::ActsMethods

Defined in:
lib/acts_as_revisionable.rb

Instance Method Summary collapse

Instance Method Details

#acts_as_revisionable(options = {}) ⇒ Object

Calling acts_as_revisionable will inject the revisionable behavior into the class. Specifying a :limit option will limit the number of revisions that are kept per record. Specifying :minimum_age will ensure that revisions are kept for at least a certain amount of time (i.e. 2.weeks). Associations to be revisioned can be specified with the :associations option as an array of association names. To specify associations of associations, use a hash for that association with the association name as the key and the value as an array of sub associations. For instance, this declaration will revision :tags, :comments, as well as the :ratings association on :comments:

:associations => [:tags, {:comments => [:ratings]}]

You can also pass an options of :on_update => true to automatically enable revisioning on every update. Otherwise you will need to perform your updates in a store_revision block. The reason for this is so that revisions for complex models with associations can be better controlled.

You can keep a revisions of deleted records by passing :dependent => :keep. When a record is destroyed, an additional revision will be created and marked as trash. Trash records can be deleted by calling the empty_trash method. You can set :on_destroy => true to automatically create the trash revision whenever a record is destroyed. It is recommended that you turn both of these features on.

Revision records can be extended to include other fields as needed and set with the :meta option. In order to extend a revision record, you must add columns to the database table. The values of the :meta option hash will be provided to the newly created revision record.

acts_as_revisionable :meta => {
  :updated_by => :last_updated_by,
  :label => lambda{|record| "Updated by #{record.updated_by} at #{record.updated_at}"},
  :version => 1
}

As a shortcut, you can can also just pass an attribute name or array of attribute names to copy to the revision record.

acts_as_revisionable :meta => :updated_by

The values to the :meta hash can be either symbols or Procs. If it is a symbol, the method so named will be called on the record being revisioned. If it is a Proc, it will be called with the record as the argument. Any other class will be sent directly to the revision record.

You can also use a subclass of RevisionRecord if desired so that you can add your own model logic as necessary. To specify a different class to use for revision records, simply subclass RevisionRecord and provide the class name to the :class_name option.

acts_as_revisionable :class_name => "MyRevisionRecord"

A has_many :revision_records will also be added to the model for accessing the revisions.



54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/acts_as_revisionable.rb', line 54

def acts_as_revisionable(options = {})
  class_attribute :acts_as_revisionable_options, :instance_writer => false, :instance_reader => false
  defaults = {:class_name => "ActsAsRevisionable::RevisionRecord"}
  self.acts_as_revisionable_options = defaults.merge(options)
  acts_as_revisionable_options[:class_name] = acts_as_revisionable_options[:class_name].name if acts_as_revisionable_options[:class_name].is_a?(Class)
  extend ClassMethods
  include InstanceMethods
  class_name = acts_as_revisionable_options[:class_name].to_s if acts_as_revisionable_options[:class_name]
  has_many_options = {:as => :revisionable, :order => 'revision DESC', :class_name => class_name}
  has_many_options[:dependent] = :destroy unless options[:dependent] == :keep
  has_many :revision_records, has_many_options
  alias_method_chain :update, :revision if options[:on_update]
  alias_method_chain :destroy, :revision if options[:on_destroy]
end