Made the Version class customizable
This commit is contained in:
parent
f18cb6d491
commit
8f7af9a45d
|
@ -11,6 +11,7 @@ module PaperTrail
|
|||
# the model is available in the `versions` association.
|
||||
#
|
||||
# Options:
|
||||
# :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.
|
||||
|
@ -25,6 +26,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),
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue