Skip to content

Commit

Permalink
Touch should honour if and unless options (#1354)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
jaredbeck and nrw505 committed Nov 28, 2021
1 parent 7484eff commit 94e167b
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 8 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Expand Up @@ -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

Expand Down
12 changes: 7 additions & 5 deletions lib/paper_trail/model_config.rb
Expand Up @@ -91,11 +91,13 @@ def on_update
# @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

Expand Down
28 changes: 26 additions & 2 deletions spec/models/translation_spec.rb
Expand Up @@ -24,6 +24,14 @@
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
Expand All @@ -44,6 +52,15 @@
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
Expand All @@ -52,14 +69,21 @@
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!
Expand Down

0 comments on commit 94e167b

Please sign in to comment.