Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't update counters twice if non-slotted counter #10

Merged
merged 1 commit into from
Jan 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Change log

## master
- Fix prevent double increment/decrement of native counter caches [#10](https://github.com/evilmartians/activerecord-slotted_counters/pull/10) by @danielwestendorf

## 0.1.0 (2022-11-29)

Expand Down
4 changes: 2 additions & 2 deletions lib/activerecord_slotted_counters/has_slotted_counter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class SlottedCounter < ::ActiveRecord::Base
module BelongsToAssociation
def update_counters_via_scope(klass, foreign_key, by)
counter_name = reflection.counter_cache_column
super unless klass.registered_slotted_counter? counter_name
return super unless klass.registered_slotted_counter? counter_name

klass.update_counters(foreign_key, counter_name => by, :touch => reflection.options[:touch])
end
Expand Down Expand Up @@ -182,7 +182,7 @@ def prepare_slotted_counters_params(ids, counters)
end

def increment!(attribute, by = 1, touch: nil)
super unless self.class.registered_slotted_counter? attribute
return super unless self.class.registered_slotted_counter? attribute

self.class.update_counters(id, attribute => by, :touch => touch)
end
Expand Down
4 changes: 2 additions & 2 deletions spec/support/shared_examples_for_cache_counters.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,10 @@
it "changes counter after creating and destroying comments" do
article = article_class.create!
article.comments.create!
expect(article.comments_count).to eq(1)
expect(article.reload.comments_count).to eq(1)

comment_class.create!(article: article)
expect(article.comments_count).to eq(2)
expect(article.reload.comments_count).to eq(2)
article.comments.destroy_all

article.reload
Expand Down