Skip to content

Commit

Permalink
defining and using a custumized whodunnit
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasas committed Feb 22, 2014
1 parent 2a7225f commit 1199c13
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 6 deletions.
15 changes: 12 additions & 3 deletions lib/paper_trail/has_paper_trail.rb
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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')
Expand Down Expand Up @@ -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
20 changes: 17 additions & 3 deletions test/unit/model_test.rb
Expand Up @@ -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'
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 1199c13

Please sign in to comment.