Skip to content

Commit 26287c4

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 f19dd80 commit 26287c4

File tree

5 files changed

+52
-0
lines changed

5 files changed

+52
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ recommendations of [keepachangelog.com](http://keepachangelog.com/).
1515
`rails generate paper_trail:install CommentVersion` created `comment_versions` table
1616
- `rails generate paper_trail:update_item_subtype` now supports custom version classes via
1717
`--version-class-name` option, e.g. `--version-class-name=CommentVersion`
18+
- [#1204](https://github.com/paper-trail-gem/paper_trail/issues/1204) - Add `reify_original` method for reifying the original object.
1819

1920
### Fixed
2021

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
@@ -654,6 +654,29 @@
654654
end
655655
end
656656

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

0 commit comments

Comments
 (0)