Touch should honour if and unless options (#1354)

* Honour if: and unless: options in on_touch

The on_touch callback was not honouring the if: or unless: options
that were set in the model config.

* Fix some test descriptions

These tests were actually testing the opposite of what they said they
were testing.

* Doc: changelog entry for #1349

[ci skip]

Co-authored-by: Nigel Williams <nigel.williams@marketplacer.com>
This commit is contained in:
Jared Beck 2021-11-27 22:05:13 -05:00 committed by GitHub
parent 7484eff4fb
commit 94e167b5ac
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 35 additions and 8 deletions

View File

@ -11,7 +11,8 @@ recommendations of [keepachangelog.com](http://keepachangelog.com/).
### Added
- None
- [#1349](https://github.com/paper-trail-gem/paper_trail/pull/1349) -
`if:` and `unless:` work with `touch` events now.
### Fixed

View File

@ -91,11 +91,13 @@ module PaperTrail
# @api public
def on_touch
@model_class.after_touch { |r|
r.paper_trail.record_update(
force: RAILS_LT_6_0,
in_after_callback: true,
is_touch: true
)
if r.paper_trail.save_version?
r.paper_trail.record_update(
force: RAILS_LT_6_0,
in_after_callback: true,
is_touch: true
)
end
}
end

View File

@ -24,6 +24,14 @@ RSpec.describe Translation, type: :model, versioning: true do
expect(PaperTrail::Version.count).to(eq(0))
end
end
context "when after touch" do
it "not change the number of versions" do
translation = described_class.create!(headline: "Headline")
translation.touch
expect(PaperTrail::Version.count).to(eq(0))
end
end
end
context "with US translations" do
@ -44,6 +52,15 @@ RSpec.describe Translation, type: :model, versioning: true do
translation.update(content: "Content")
expect(PaperTrail::Version.count).to(eq(0))
end
it "touch does not change the number of versions" do
translation = described_class.new(headline: "Headline")
translation.language_code = "US"
translation.type = "DRAFT"
translation.save!
translation.touch
expect(PaperTrail::Version.count).to(eq(0))
end
end
context "with non-drafts" do
@ -52,14 +69,21 @@ RSpec.describe Translation, type: :model, versioning: true do
expect(PaperTrail::Version.count).to(eq(1))
end
it "update does not change the number of versions" do
it "update changes the number of versions" do
translation = described_class.create!(headline: "Headline", language_code: "US")
translation.update(content: "Content")
expect(PaperTrail::Version.count).to(eq(2))
expect(translation.versions.size).to(eq(2))
end
it "destroy does not change the number of versions" do
it "touch changes the number of versions" do
translation = described_class.create!(headline: "Headline", language_code: "US")
translation.touch
expect(PaperTrail::Version.count).to(eq(2))
expect(translation.versions.size).to(eq(2))
end
it "destroy changes the number of versions" do
translation = described_class.new(headline: "Headline")
translation.language_code = "US"
translation.save!