Skip to content

Commit aed329e

Browse files
committed
Add reify_original method.
Returning the original version is a common use-case and is not as easy as it should be, particularly because calling `reify` on the first version returns `nil`, which is the `object` *before* it was created. This method simply tries to `reify` from the *second* version, if it exists. If it doesn’t exist (i.e. there has not been any changes to the object), it falls back to `self`, which is the original and *only* version of the object. Added specs to document, legacy widget and widget. Fixes #1204.
1 parent 94e9c0d commit aed329e

File tree

4 files changed

+51
-0
lines changed

4 files changed

+51
-0
lines changed

lib/paper_trail/record_trail.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,13 @@ def record_create
6969
end
7070
end
7171

72+
# Returns the original version of this object or just this object if there has been no changes.
73+
#
74+
# @api public
75+
def reify_original
76+
versions.second&.reify || @record
77+
end
78+
7279
# `recording_order` is "after" or "before". See ModelConfig#on_destroy.
7380
#
7481
# @api private

spec/models/document_spec.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,17 @@
3030
end
3131
end
3232

33+
describe "#paper_trail.reify_original" do
34+
it "returns the expected document" do
35+
doc = described_class.create
36+
original_name = doc.name
37+
doc.update(name: "Doc 1")
38+
doc.update(name: "Doc 2")
39+
expect(doc.paper_trail_versions.length).to(eq(3))
40+
expect(doc.paper_trail.reify_original.name).to(eq(original_name))
41+
end
42+
end
43+
3344
describe "#paper_trail_versions" do
3445
it "returns the expected version records" do
3546
doc = described_class.create

spec/models/legacy_widget_spec.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,16 @@
2323
end
2424
end
2525

26+
describe "#reify_original" do
27+
it "return its original self" do
28+
widget = described_class.create(name: "foo", version: 2)
29+
%w[bar baz].each { |name| widget.update(name: name) }
30+
version = widget.versions.last
31+
reified = version.reify
32+
expect(reified.paper_trail.reify_original).to(eq(reified.versions[1].reify))
33+
end
34+
end
35+
2636
describe "#update" do
2737
it "does not create a PT version record because the updated column is ignored" do
2838
described_class.create.update(version: 1)

spec/models/widget_spec.rb

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -653,6 +653,29 @@
653653
end
654654
end
655655

656+
describe "#reify_original" do
657+
context "with a reified item" do
658+
it "returns the object (not a Version) as it was originally" do
659+
widget = described_class.create(name: "Bob")
660+
%w[Tom Dick Jane].each do |name|
661+
widget.update(name: name)
662+
end
663+
last_widget = widget.versions.last.reify
664+
expect(last_widget.paper_trail.reify_original.name).to(eq(widget.versions[1].reify.name))
665+
end
666+
end
667+
668+
context "with a non-reified item" do
669+
it "returns the object (not a Version) as it was originally" do
670+
widget = described_class.create
671+
%w[Tom Dick Jane].each do |name|
672+
widget.update(name: name)
673+
end
674+
expect(widget.paper_trail.reify_original.name).to(eq(widget.versions[1].reify.name))
675+
end
676+
end
677+
end
678+
656679
context "with an unsaved record" do
657680
it "not have a version created on destroy" do
658681
widget = described_class.new

0 commit comments

Comments
 (0)