Skip to content

Commit

Permalink
Try to create a version record when associated object is touched
Browse files Browse the repository at this point in the history
Before #1285, a version record was created when associated object is touched.
But because of Rails don't track implicit touch mutation[1], now PaperTail
don't create a version record was created when associated object is touched.

This patch try to keep creating a version record in that case by doing checking an
event and changed value.

Fixes #1339.

[1]: rails/rails@dcb8259
  • Loading branch information
y-yagi committed Mar 1, 2022
1 parent ee1b000 commit f5652ea
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 2 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ recommendations of [keepachangelog.com](http://keepachangelog.com/).

### Fixed

- None
- [#1376](https://github.com/paper-trail-gem/paper_trail/pull/1376) -
Create a version record when associated object was changed the same as
PaperTrail < v12.1.0.

## 12.2.0 (2022-01-21)

Expand Down
14 changes: 14 additions & 0 deletions lib/paper_trail/events/update.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,20 @@ def data
merge_metadata_into(data)
end

# If it is a touch event, and changed are empty, it is assumed to be
# implicit `touch` mutation, and will a version is created.
#
# See https://github.com/rails/rails/commit/dcb825902d79d0f6baba956f7c6ec5767611353e
#
# @api private
def changed_notably?
if @is_touch && changes_in_latest_version.empty?
true
else
super
end
end

private

# @api private
Expand Down
2 changes: 1 addition & 1 deletion spec/dummy_app/app/models/order.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# frozen_string_literal: true

class Order < ActiveRecord::Base
belongs_to :customer
belongs_to :customer, touch: :touched_at
has_many :line_items
has_paper_trail
end
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,7 @@ def up

create_table :customers, force: true do |t|
t.string :name
t.datetime :touched_at, limit: 6
end

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

require "spec_helper"

RSpec.describe Order, type: :model, versioning: true do
context "when the record destroyed" do
it "creates a version record for association" do
customer = Customer.create!
described_class.create!(customer: customer)
described_class.destroy_all

expect(customer.versions.count).to(eq(3))
end
end
end

0 comments on commit f5652ea

Please sign in to comment.