Enable passing a symbol to :meta to signal a method to call.
This commit is contained in:
parent
be57b8d2af
commit
6baf664282
|
@ -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 || {})
|
||||
|
|
|
@ -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.' }
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
Loading…
Reference in New Issue