From abf15f646e5fa32c39493b538fa69434a6cdf13e Mon Sep 17 00:00:00 2001 From: Nikita Cernovs Date: Mon, 16 Jan 2012 18:17:57 +0200 Subject: [PATCH] ":ignore_if" condition check moved to before_create/before_update --- lib/paper_trail/has_paper_trail.rb | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/lib/paper_trail/has_paper_trail.rb b/lib/paper_trail/has_paper_trail.rb index 12e69119..d2c20b90 100644 --- a/lib/paper_trail/has_paper_trail.rb +++ b/lib/paper_trail/has_paper_trail.rb @@ -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