mirror of
https://github.com/paper-trail-gem/paper_trail.git
synced 2022-11-09 11:33:19 -05:00
Version#reify return new object instance with dup: true options
This commit is contained in:
parent
44dcdbaaa3
commit
1cc1d7a1cc
3 changed files with 28 additions and 4 deletions
|
@ -146,6 +146,9 @@ And a `PaperTrail::Version` instance has these methods:
|
|||
# Returns the item restored from this version.
|
||||
version.reify(options = {})
|
||||
|
||||
# Return a new item from this version
|
||||
version.reify(dup: true)
|
||||
|
||||
# Returns who put the item into the state stored in this version.
|
||||
version.originator
|
||||
|
||||
|
|
|
@ -111,6 +111,8 @@ module PaperTrail
|
|||
# :has_one set to `false` to opt out of has_one reification.
|
||||
# set to a float to change the lookback time (check whether your db supports
|
||||
# sub-second datetimes if you want them).
|
||||
# :dup `false` default behavior
|
||||
# `true` it always create a new object instance. It is useful for comparing two versions of the same object
|
||||
def reify(options = {})
|
||||
return nil if object.nil?
|
||||
|
||||
|
@ -133,7 +135,7 @@ module PaperTrail
|
|||
# `item_type` will be the base class, not the actual subclass.
|
||||
# If `type` is present but empty, the class is the base class.
|
||||
|
||||
if item
|
||||
if item && ! options[:dup]
|
||||
model = item
|
||||
# Look for attributes that exist in the model and not in this version. These attributes should be set to nil.
|
||||
(model.attribute_names - attrs.keys).each { |k| attrs[k] = nil }
|
||||
|
|
|
@ -96,10 +96,29 @@ describe Widget do
|
|||
PaperTrail.whodunnit = new_name
|
||||
widget.update_attributes(:name => 'Elizabeth')
|
||||
end
|
||||
let(:reified_widget) { widget.versions[1].reify }
|
||||
|
||||
it "should return the appropriate originator" do
|
||||
reified_widget.originator.should == orig_name
|
||||
context "reverting a change" do
|
||||
let(:reified_widget) { widget.versions[1].reify }
|
||||
|
||||
it "should return the appropriate originator" do
|
||||
reified_widget.originator.should == orig_name
|
||||
end
|
||||
|
||||
it "should not create a new model instance" do
|
||||
reified_widget.should_not be_new_record
|
||||
end
|
||||
end
|
||||
|
||||
context "creating a new instance" do
|
||||
let(:reified_widget) { widget.versions[1].reify(dup: true) }
|
||||
|
||||
it "should return the appropriate originator" do
|
||||
reified_widget.originator.should == orig_name
|
||||
end
|
||||
|
||||
it "should not create a new model instance" do
|
||||
reified_widget.should be_new_record
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue