Make increment_counter/decrement_counter accept an amount argument

This commit is contained in:
fatkodima 2023-05-04 19:13:58 +03:00
parent 490804f7d5
commit cabc1842b7
3 changed files with 34 additions and 4 deletions

@ -1,3 +1,11 @@
* Make `increment_counter`/`decrement_counter` accept an amount argument
```ruby
Post.increment_counter(:comments_count, 5, by: 3)
```
*fatkodima*
* Add support for `Array#intersect?` to `ActiveRecord::Relation`.
`Array#intersect?` is only available on Ruby 3.1 or later.

@ -126,6 +126,7 @@ def update_counters(id, counters)
#
# * +counter_name+ - The name of the field that should be incremented.
# * +id+ - The id of the object that should be incremented or an array of ids.
# * <tt>:by</tt> - The amount by which to increment the value. Defaults to +1+.
# * <tt>:touch</tt> - Touch timestamp columns when updating.
# Pass +true+ to touch +updated_at+ and/or +updated_on+. Pass a symbol to
# touch that column or an array of symbols to touch just those ones.
@ -136,10 +137,14 @@ def update_counters(id, counters)
# DiscussionBoard.increment_counter(:posts_count, 5)
#
# # Increment the posts_count column for the record with an id of 5
# # by a specific amount.
# DiscussionBoard.increment_counter(:posts_count, 5, by: 3)
#
# # Increment the posts_count column for the record with an id of 5
# # and update the updated_at value.
# DiscussionBoard.increment_counter(:posts_count, 5, touch: true)
def increment_counter(counter_name, id, touch: nil)
update_counters(id, counter_name => 1, touch: touch)
def increment_counter(counter_name, id, by: 1, touch: nil)
update_counters(id, counter_name => by, touch: touch)
end
# Decrement a numeric field by one, via a direct SQL update.
@ -151,6 +156,7 @@ def increment_counter(counter_name, id, touch: nil)
#
# * +counter_name+ - The name of the field that should be decremented.
# * +id+ - The id of the object that should be decremented or an array of ids.
# * <tt>:by</tt> - The amount by which to increment the value. Defaults to +1+.
# * <tt>:touch</tt> - Touch timestamp columns when updating.
# Pass +true+ to touch +updated_at+ and/or +updated_on+. Pass a symbol to
# touch that column or an array of symbols to touch just those ones.
@ -161,10 +167,14 @@ def increment_counter(counter_name, id, touch: nil)
# DiscussionBoard.decrement_counter(:posts_count, 5)
#
# # Decrement the posts_count column for the record with an id of 5
# by a specific amount.
# DiscussionBoard.decrement_counter(:posts_count, 5, by: 3)
#
# # Decrement the posts_count column for the record with an id of 5
# # and update the updated_at value.
# DiscussionBoard.decrement_counter(:posts_count, 5, touch: true)
def decrement_counter(counter_name, id, touch: nil)
update_counters(id, counter_name => -1, touch: touch)
def decrement_counter(counter_name, id, by: 1, touch: nil)
update_counters(id, counter_name => -by, touch: touch)
end
def counter_cache_column?(name) # :nodoc:

@ -40,12 +40,24 @@ class ::SpecialReply < ::Reply
end
end
test "increment counter by specific amount" do
assert_difference "@topic.reload.replies_count", +2 do
Topic.increment_counter(:replies_count, @topic.id, by: 2)
end
end
test "decrement counter" do
assert_difference "@topic.reload.replies_count", -1 do
Topic.decrement_counter(:replies_count, @topic.id)
end
end
test "decrement counter by specific amount" do
assert_difference "@topic.reload.replies_count", -2 do
Topic.decrement_counter(:replies_count, @topic.id, by: 2)
end
end
test "reset counters" do
# throw the count off by 1
Topic.increment_counter(:replies_count, @topic.id)