diff --git a/lib/paper_trail/has_paper_trail.rb b/lib/paper_trail/has_paper_trail.rb index e1103c731..6dc9fe0a7 100644 --- a/lib/paper_trail/has_paper_trail.rb +++ b/lib/paper_trail/has_paper_trail.rb @@ -14,11 +14,11 @@ module ClassMethods # `:create`, `:update`, `:destroy` as desired. # :class_name the name of a custom Version class. This class should inherit from `PaperTrail::Version`. # :ignore an array of attributes for which a new `Version` will not be created if only they change. - # it can also aceept a Hash as an argument where the key is the attribute to ignore (a `String` or `Symbol`), + # it can also aceept a Hash as an argument where the key is the attribute to ignore (a `String` or `Symbol`), # which will only be ignored if the value is a `Proc` which returns truthily. # :if, :unless Procs that allow to specify conditions when to save versions for an object # :only inverse of `ignore` - a new `Version` will be created only for these attributes if supplied - # it can also aceept a Hash as an argument where the key is the attribute to track (a `String` or `Symbol`), + # it can also aceept a Hash as an argument where the key is the attribute to track (a `String` or `Symbol`), # which will only be counted if the value is a `Proc` which returns truthily. # :skip fields to ignore completely. As with `ignore`, updates to these fields will not create # a new `Version`. In addition, these fields will not be included in the serialized versions @@ -232,6 +232,11 @@ def touch_with_version(name = nil) save! end + def whodunnit(whodunnit) + @whodunnit = whodunnit + yield if block_given? + end + private def source_version @@ -242,7 +247,7 @@ def record_create if paper_trail_switched_on? data = { :event => paper_trail_event || 'create', - :whodunnit => PaperTrail.whodunnit + :whodunnit => _whodunnit } if changed_notably? and self.class.paper_trail_version_class.column_names.include?('object_changes') @@ -362,6 +367,10 @@ def save_version? unless_condition = self.paper_trail_options[:unless] (if_condition.blank? || if_condition.call(self)) && !unless_condition.try(:call, self) end + + def _whodunnit + @whodunnit || PaperTrail.whodunnit + end end end end diff --git a/test/unit/model_test.rb b/test/unit/model_test.rb index 27983f47e..d2bb2d3f1 100644 --- a/test/unit/model_test.rb +++ b/test/unit/model_test.rb @@ -535,6 +535,20 @@ def without(&block) @widget = Widget.new :name => 'Fidget' end + context 'defining whodunnit using a block' do + setup do + @widget.whodunnit 'Clair' do + @widget.save + end + + @version = @widget.versions.last # only 1 version + end + + should 'track who made the change' do + assert_equal 'Clair', @version.whodunnit + end + end + context 'when a record is created' do setup do PaperTrail.whodunnit = 'Alice' @@ -771,7 +785,7 @@ 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 @@ -793,7 +807,7 @@ 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 @@ -814,7 +828,7 @@ 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