Skip to content

Commit

Permalink
Add model option for not synchronizing timestamps (#1416)
Browse files Browse the repository at this point in the history
* Add model option for not synchronizing timestamps

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

* Add test to verify that created_at is not synced
  • Loading branch information
SebRollen authored Jan 30, 2023
1 parent f348708 commit 1fe26c9
Show file tree
Hide file tree
Showing 6 changed files with 35 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
5 changes: 5 additions & 0 deletions spec/dummy_app/app/models/gizmo.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# frozen_string_literal: true

class Gizmo < ApplicationRecord
has_paper_trail synchronize_version_creation_timestamp: false
end
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ def up
t.timestamps null: true, limit: 6
end

create_table :gizmos, force: true do |t|
t.string :name
t.timestamps null: true, limit: 6
end

create_table :widgets, force: true do |t|
t.string :name
t.text :a_text
Expand Down
15 changes: 15 additions & 0 deletions spec/models/gizmo_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# frozen_string_literal: true

require "spec_helper"
require "support/performance_helpers"

RSpec.describe Gizmo, type: :model, versioning: true do
context "with a persisted record" do
it "does not use the gizmo `updated_at` as the version's `created_at`" do
gizmo = described_class.create(name: "Fred", created_at: Time.current - 1.day)
gizmo.name = "Allen"
gizmo.save(touch: false)
expect(gizmo.versions.last.created_at).not_to(eq(gizmo.updated_at))
end
end
end

0 comments on commit 1fe26c9

Please sign in to comment.