Skip to content

Commit

Permalink
Add :on option to has_paper_trail method
Browse files Browse the repository at this point in the history
  • Loading branch information
edtsech committed Jul 27, 2011
1 parent 64e5fa2 commit 7ed698e
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 5 deletions.
6 changes: 3 additions & 3 deletions lib/paper_trail/has_paper_trail.rb
Expand Up @@ -50,9 +50,9 @@ def has_paper_trail(options = {})
: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.
Expand Down
3 changes: 2 additions & 1 deletion 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
7 changes: 6 additions & 1 deletion test/unit/model_test.rb
Expand Up @@ -812,7 +812,7 @@ def without(&block)
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'
Expand All @@ -831,6 +831,11 @@ def without(&block)
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
Expand Down

0 comments on commit 7ed698e

Please sign in to comment.