Skip to content

Commit

Permalink
Fix invalid version created_at value when using update_columns
Browse files Browse the repository at this point in the history
Use the default(AR generated) timestamp for the `Version#created_at` value when using `PaperTrail::RecordTrail#update_columns`.

`PaperTrail::RecordTrail#update_columns` was previously setting `Version#created_at` as the recod `updated_at` value.
Since `update_columns` does not update timestamps - the previous update date was being used for the `Version#created_at` instead of the actual update date.

Fixes #1395
  • Loading branch information
iiwo authored and jaredbeck committed Nov 12, 2022
1 parent 7cdd212 commit 0c69342
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 6 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ recommendations of [keepachangelog.com](http://keepachangelog.com/).

### Fixed

- [#1395](/~https://github.com/paper-trail-gem/paper_trail/issues/1395) -
Fix incorrect `Version#created_at` value when using
`PaperTrail::RecordTrail#update_columns`
- [#1404](/~https://github.com/paper-trail-gem/paper_trail/pull/1404) -
Delay referencing ActiveRecord until after Railtie is loaded

Expand Down
7 changes: 5 additions & 2 deletions lib/paper_trail/record_trail.rb
Original file line number Diff line number Diff line change
Expand Up @@ -148,11 +148,14 @@ def data_for_update
# paper_trail-association_tracking
def record_update_columns(changes)
return unless enabled?
event = Events::Update.new(@record, false, false, changes)
data = Events::Update.new(@record, false, false, changes).data

# use AR default timestamp value instead of record.updated_at value
data.delete(:created_at)

# Merge data from `Event` with data from PT-AT. We no longer use
# `data_for_update_columns` but PT-AT still does.
data = event.data.merge(data_for_update_columns)
data.merge!(data_for_update_columns)

versions_assoc = @record.send(@record.class.versions_association_name)
version = versions_assoc.create(data)
Expand Down
12 changes: 8 additions & 4 deletions spec/models/widget_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -970,15 +970,19 @@
end

describe ".paper_trail.update_columns", versioning: true do
let(:widget) { described_class.create! name: "Bob", an_integer: 1 }

it "creates a version record" do
widget = described_class.create
widget = described_class.create(updated_at: "2015-01-01 15:00")
expect(widget.versions.count).to eq(1)
widget.paper_trail.update_columns(name: "Bugle")
now = nil
freeze_time do
now = Time.zone.now
widget.paper_trail.update_columns(name: "Bugle")
end

expect(widget.versions.count).to eq(2)
expect(widget.versions.last.event).to(eq("update"))
expect(widget.versions.last.changeset[:name]).to eq([nil, "Bugle"])
expect(widget.versions.last.created_at.to_i).to eq(now.to_i)
end
end

Expand Down
2 changes: 2 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
SimpleCov.minimum_coverage(ENV["DB"] == "postgres" ? 96.8 : 92.4)

require "byebug"
require "active_support/testing/time_helpers"
require_relative "support/pt_arel_helpers"

unless ENV["BUNDLE_GEMFILE"].match?(/rails_\d\.\d\.gemfile/)
Expand Down Expand Up @@ -41,6 +42,7 @@
end
config.order = :random
config.include PTArelHelpers
config.include ActiveSupport::Testing::TimeHelpers
Kernel.srand config.seed
end

Expand Down

0 comments on commit 0c69342

Please sign in to comment.