Made the Version class customizable

This commit is contained in:
Ben Zittlau 2011-03-24 13:19:32 -06:00
parent f18cb6d491
commit 8f7af9a45d
2 changed files with 16 additions and 12 deletions

View File

@ -11,12 +11,13 @@ module PaperTrail
# the model is available in the `versions` association.
#
# Options:
# :ignore an array of attributes for which a new `Version` will not be created if only they change.
# :only inverse of `ignore` - a new `Version` will be created only for these attributes if supplied
# :meta a hash of extra data to store. You must add a column to the `versions` table for each key.
# Values are objects or procs (which are called with `self`, i.e. the model with the paper
# trail). See `PaperTrail::Controller.info_for_paper_trail` for how to store data from
# the controller.
# :class_name the name of a custom Version class. This class should inherit from Version.
# :ignore an array of attributes for which a new `Version` will not be created if only they change.
# :only inverse of `ignore` - a new `Version` will be created only for these attributes if supplied
# :meta a hash of extra data to store. You must add a column to the `versions` table for each key.
# Values are objects or procs (which are called with `self`, i.e. the model with the paper
# trail). See `PaperTrail::Controller.info_for_paper_trail` for how to store data from
# the controller.
def has_paper_trail(options = {})
# Lazily include the instance methods so we don't clutter up
# any more ActiveRecord models than we have to.
@ -24,6 +25,9 @@ module PaperTrail
# The version this instance was reified from.
attr_accessor :version
cattr_accessor :version_class_name
self.version_class_name = options[:class_name] || "Version"
cattr_accessor :ignore
self.ignore = ([options[:ignore]].flatten.compact || []).map &:to_s
@ -39,7 +43,7 @@ module PaperTrail
cattr_accessor :paper_trail_active
self.paper_trail_active = true
has_many :versions, :as => :item, :order => 'created_at ASC, id ASC'
has_many :versions, :class_name => version_class_name, :as => :item, :order => 'created_at ASC, id ASC'
after_create :record_create
before_update :record_update
@ -68,7 +72,7 @@ module PaperTrail
# Returns who put the object into its current state.
def originator
Version.with_item_keys(self.class.name, id).last.try :whodunnit
version_class_name.constantize.with_item_keys(self.class.name, id).last.try :whodunnit
end
# Returns the object (not a Version) as it was at the given timestamp.
@ -111,7 +115,7 @@ module PaperTrail
def record_destroy
if switched_on? and not new_record?
Version.create merge_metadata(:item_id => self.id,
version_class_name.constantize.create merge_metadata(:item_id => self.id,
:item_type => self.class.name,
:event => 'destroy',
:object => object_to_string(item_before_change),

View File

@ -7,11 +7,11 @@ class Version < ActiveRecord::Base
}
scope :subsequent, lambda { |version|
where(["id > ?", version.is_a?(Version) ? version.id : version]).order("id ASC")
where(["id > ?", version.is_a?(self) ? version.id : version]).order("id ASC")
}
scope :preceding, lambda { |version|
where(["id < ?", version.is_a?(Version) ? version.id : version]).order("id DESC")
where(["id < ?", version.is_a?(self) ? version.id : version]).order("id DESC")
}
scope :after, lambda { |timestamp|
@ -87,7 +87,7 @@ class Version < ActiveRecord::Base
end
def sibling_versions
Version.with_item_keys(item_type, item_id)
self.class.with_item_keys(item_type, item_id)
end
def next