Enable passing a symbol to :meta to signal a method to call.

This commit is contained in:
Emmanuel Gomez 2011-02-06 00:32:35 -08:00
parent be57b8d2af
commit 6baf664282
3 changed files with 18 additions and 1 deletions

View File

@ -117,7 +117,14 @@ module PaperTrail
def merge_metadata(data)
# First we merge the model-level metadata in `meta`.
meta.each do |k,v|
data[k] = v.respond_to?(:call) ? v.call(self) : v
data[k] =
if v.respond_to?(:call)
v.call(self)
elsif v.is_a?(Symbol) && respond_to?(v)
send(v)
else
v
end
end
# Second we merge any extra data from the controller (if available).
data.merge(PaperTrail.controller_info || {})

View File

@ -21,8 +21,13 @@ end
class Article < ActiveRecord::Base
has_paper_trail :ignore => [:title],
:meta => {:answer => 42,
:action => :action_data_provider_method,
:question => Proc.new { "31 + 11 = #{31 + 11}" },
:article_id => Proc.new { |article| article.id } }
def action_data_provider_method
self.object_id.to_s
end
end
class Book < ActiveRecord::Base
@ -528,6 +533,10 @@ class HasPaperTrailModelTest < Test::Unit::TestCase
assert_equal @article.id, @article.versions.last.article_id
end
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
context 'and updated' do
setup { @article.update_attributes! :content => 'Better text.' }

View File

@ -25,6 +25,7 @@ ActiveRecord::Schema.define(:version => 0) do
# Metadata columns.
t.integer :answer
t.string :action
t.string :question
t.integer :article_id