Skip to content

Commit

Permalink
Add model option for not synchronizing timestamps
Browse files Browse the repository at this point in the history
Paper trail automatically synchronizes the version
created_at timestamp to be the same as the updated_at
timestamp of the record being updated. This pull-requests
adds a configurable model-level option to disable this
behavior and instead have created_at be populated with
the timestamp when the version was inserted into the database.

The default remains to synchronize version timestamps
  • Loading branch information
SebRollen committed Jan 19, 2023
1 parent f348708 commit 7ae987b
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 2 deletions.
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ recommendations of [keepachangelog.com](http://keepachangelog.com/).

### Added

- None
- [#1416](https://github.com/paper-trail-gem/paper_trail/pull/1416) - Adds a
model-configurable option `synchronize_version_creation_timestamp` which, if
set to false, opts out of synchronizing timestamps between `Version.created_at`
and the record's `updated_at`.

### Fixed

Expand Down
4 changes: 4 additions & 0 deletions lib/paper_trail/has_paper_trail.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ module ClassMethods
# - A Hash - options passed to `has_many`, plus `name:` and `scope:`.
# - :version - The name to use for the method which returns the version
# the instance was reified from. Default is `:version`.
# - :synchronize_version_creation_timestamp - By default, paper trail
# sets the `created_at` field for a new Version equal to the `updated_at`
# column of the model being updated. If you instead want `created_at` to
# populate with the current timestamp, set this option to `false`.
#
# Plugins like the experimental `paper_trail-association_tracking` gem
# may accept additional options.
Expand Down
3 changes: 2 additions & 1 deletion lib/paper_trail/record_trail.rb
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,8 @@ def build_version_on_update(force:, in_after_callback:, is_touch:)
# unnatural to tamper with creation timestamps in this way. But, this
# feature has existed for a long time, almost a decade now, and some users
# may rely on it now.
if @record.respond_to?(:updated_at)
if @record.respond_to?(:updated_at) &&
@record.paper_trail_options[:synchronize_version_creation_timestamp] != false
data[:created_at] = @record.updated_at
end

Expand Down

0 comments on commit 7ae987b

Please sign in to comment.