Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Force metadata calls to attributes to use current value if value is changing for attribute #176

Merged
merged 3 commits into from
Oct 12, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 6 additions & 1 deletion lib/paper_trail/has_paper_trail.rb
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,12 @@ def merge_metadata(data)
if v.respond_to?(:call)
v.call(self)
elsif v.is_a?(Symbol) && respond_to?(v)
send(v)
# if it is an attribute that is changing, be sure to grab the current version
if has_attribute?(v) && send("#{v}_changed?".to_sym)
send("#{v}_was".to_sym)
else
send(v)
end
else
v
end
Expand Down
7 changes: 5 additions & 2 deletions test/dummy/app/models/article.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@ class Article < ActiveRecord::Base
has_paper_trail :ignore => :title,
:only => [:content],
:skip => [:file_upload],
:meta => {:answer => 42,
:meta => {
:answer => 42,
:action => :action_data_provider_method,
:question => Proc.new { "31 + 11 = #{31 + 11}" },
:article_id => Proc.new { |article| article.id } }
:article_id => Proc.new { |article| article.id },
:title => :title
}

def action_data_provider_method
self.object_id.to_s
Expand Down
1 change: 1 addition & 0 deletions test/dummy/db/migrate/20110208155312_set_up_test_tables.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ def self.up
t.string :action
t.string :question
t.integer :article_id
t.string :title

# Controller info columns.
t.string :ip
Expand Down
2 changes: 1 addition & 1 deletion test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,5 @@ def change_schema

class Version < ActiveRecord::Base
attr_accessible :created_at, :updated_at,
:answer, :action, :question, :article_id, :ip, :user_agent
:answer, :action, :question, :article_id, :ip, :user_agent, :title
end
22 changes: 19 additions & 3 deletions test/unit/model_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -631,7 +631,10 @@ def without(&block)


context 'An item' do
setup { @article = Article.new }
setup do
@initial_title = 'Foobar'
@article = Article.new :title => @initial_title
end

context 'which is created' do
setup { @article.save }
Expand All @@ -651,10 +654,16 @@ def without(&block)
should 'store dynamic meta data based on a method of the item' do
assert_equal @article.action_data_provider_method, @article.versions.last.action
end

should 'store dynamic meta data based on an attribute of the item prior to creation' do
assert_equal nil, @article.versions.last.title
end


context 'and updated' do
setup { @article.update_attributes! :content => 'Better text.' }
setup do
@article.update_attributes! :content => 'Better text.', :title => 'Rhubarb'
end

should 'store fixed meta data' do
assert_equal 42, @article.versions.last.answer
Expand All @@ -667,6 +676,10 @@ def without(&block)
should 'store dynamic meta data which depends on the item' do
assert_equal @article.id, @article.versions.last.article_id
end

should 'store dynamic meta data based on an attribute of the item prior to the update' do
assert_equal @initial_title, @article.versions.last.title
end
end


Expand All @@ -684,7 +697,10 @@ def without(&block)
should 'store dynamic meta data which depends on the item' do
assert_equal @article.id, @article.versions.last.article_id
end


should 'store dynamic meta data based on an attribute of the item prior to the destruction' do
assert_equal @initial_title, @article.versions.last.title
end
end
end
end
Expand Down