Skip to content

Commit

Permalink
Use PaperTrail's new save_with_version when available
Browse files Browse the repository at this point in the history
  • Loading branch information
magnusvk committed May 7, 2018
1 parent 6deddf5 commit 19ce856
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
5 changes: 3 additions & 2 deletions lib/counter_culture/counter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,9 @@ def change_counter_cache(obj, options)
if @with_papertrail
instance = klass.where(primary_key => id_to_change).first
if instance
if PaperTrail::VERSION::MAJOR >= 9
instance.touch
if instance.paper_trail.respond_to?(:save_with_version)
# touch_with_version is deprecated starting in PaperTrail 9.0.0
instance.paper_trail.save_with_version(validate: false)
else
instance.paper_trail.touch_with_version
end
Expand Down
15 changes: 15 additions & 0 deletions spec/counter_culture_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1949,6 +1949,21 @@ def mess_up_counts

expect(product.reviews_count).to eq(1)
expect(product.versions.count).to eq(2)

attrs_from_versions = YAML.load(product.versions.reorder(:id).last.object)
# should be the value before the counter change
expect(attrs_from_versions['reviews_count']).to eq(0)

user.reviews.create :user_id => user.id, :product_id => product.id, :approvals => 13

product.reload

expect(product.reviews_count).to eq(2)
expect(product.versions.count).to eq(3)

attrs_from_versions = YAML.load(product.versions.reorder(:id).last.object)
# should be the value before the counter change
expect(attrs_from_versions['reviews_count']).to eq(1)
end

it "does not create a papertrail version when papertrail flag not set" do
Expand Down

0 comments on commit 19ce856

Please sign in to comment.