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