Skip to content

Commit

Permalink
Add reify_original method.
Browse files Browse the repository at this point in the history
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.

Fixes paper-trail-gem#1204.
  • Loading branch information
joshuapinter committed Jan 8, 2025
1 parent 94e9c0d commit 607aea8
Showing 1 changed file with 7 additions and 0 deletions.
7 changes: 7 additions & 0 deletions lib/paper_trail/record_trail.rb
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,13 @@ def record_create
end
end

# Returns the original version of this object or just this object if there has been no changes.
#
# @api public
def reify_original
versions.second&.reify || @record
end

# `recording_order` is "after" or "before". See ModelConfig#on_destroy.
#
# @api private
Expand Down

0 comments on commit 607aea8

Please sign in to comment.