":ignore_if" condition check moved to before_create/before_update

This commit is contained in:
Nikita Cernovs 2012-01-16 18:17:57 +02:00
parent 183ba3e7ec
commit abf15f646e
1 changed files with 9 additions and 9 deletions

View File

@ -15,6 +15,7 @@ module PaperTrail
# `:create`, `:update`, `:destroy` as desired.
# :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.
# :ignore_if a Proc that allows not to create versions for a record based on the given condition
# :only inverse of `ignore` - a new `Version` will be created only for these attributes if supplied
# :skip fields to ignore completely. As with `ignore`, updates to these fields will not create
# a new `Version`. In addition, these fields will not be included in the serialized versions
@ -66,8 +67,8 @@ module PaperTrail
:as => :item,
:order => "created_at ASC, #{self.version_class_name.constantize.primary_key} ASC"
after_create :record_create if !options[:on] || options[:on].include?(:create)
before_update :record_update if !options[:on] || options[:on].include?(:update)
after_create :record_create, :unless => :prevent_versioning? if !options[:on] || options[:on].include?(:create)
before_update :record_update, :unless => :prevent_versioning? if !options[:on] || options[:on].include?(:update)
after_destroy :record_destroy if !options[:on] || options[:on].include?(:destroy)
end
@ -127,10 +128,6 @@ module PaperTrail
self.class.paper_trail_on if paper_trail_was_enabled
end
def prevent_versioning?
ignore_condition && ignore_condition.call(self)
end
private
def version_class
@ -142,13 +139,13 @@ module PaperTrail
end
def record_create
if switched_on? && !prevent_versioning?
if switched_on?
send(self.class.versions_association_name).create merge_metadata(:event => 'create', :whodunnit => PaperTrail.whodunnit)
end
end
def record_update
if switched_on? && changed_notably? && !prevent_versioning?
if switched_on? && changed_notably?
data = {
:event => 'update',
:object => object_to_string(item_before_change),
@ -222,7 +219,10 @@ module PaperTrail
def switched_on?
PaperTrail.enabled? && PaperTrail.enabled_for_controller? && self.class.paper_trail_enabled_for_model
end
end
def prevent_versioning?
ignore_condition.try :call, self
end
end
end
end