Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add model option for not synchronizing timestamps #1416

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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