diff --git a/lib/paper_trail/has_paper_trail.rb b/lib/paper_trail/has_paper_trail.rb index 7f543613..2d77e13f 100644 --- a/lib/paper_trail/has_paper_trail.rb +++ b/lib/paper_trail/has_paper_trail.rb @@ -50,9 +50,9 @@ module PaperTrail :as => :item, :order => "created_at ASC, #{self.primary_key} ASC" - after_create :record_create - before_update :record_update - after_destroy :record_destroy + after_create :record_create if !options[:on] || (options[:on] && options[:on].include?(:create)) + before_update :record_update if !options[:on] || (options[:on] && options[:on].include?(:update)) + after_destroy :record_destroy if !options[:on] || (options[:on] && options[:on].include?(:destroy)) end # Switches PaperTrail off for this class. diff --git a/test/dummy/app/models/document.rb b/test/dummy/app/models/document.rb index 97e51725..f0a2a7a1 100644 --- a/test/dummy/app/models/document.rb +++ b/test/dummy/app/models/document.rb @@ -1,3 +1,4 @@ class Document < ActiveRecord::Base - has_paper_trail :versions => :paper_trail_versions + has_paper_trail :versions => :paper_trail_versions, + :on => [:create, :update] end diff --git a/test/unit/model_test.rb b/test/unit/model_test.rb index 563b7d09..b03b99b6 100644 --- a/test/unit/model_test.rb +++ b/test/unit/model_test.rb @@ -812,7 +812,7 @@ class HasPaperTrailModelTest < ActiveSupport::TestCase end end - context 'A model with a custom association' do + context 'A model with a custom association and "on" option' do setup do @doc = Document.create @doc.update_attributes :name => 'Doc 1' @@ -831,6 +831,11 @@ class HasPaperTrailModelTest < ActiveSupport::TestCase assert_equal 3, @doc.paper_trail_versions.length assert_equal 'Doc 1', @doc.previous_version.name end + + should 'not create new version after destroy' do + @doc.destroy + assert_equal 2, @doc.paper_trail_versions.length + end end private