Ticket #10806: add_quantity_option_to_increment_counter_and_decrement_counter_for_consistency_with_absolute.diff
| File add_quantity_option_to_increment_counter_and_decrement_counter_for_consistency_with_absolute.diff, 2.2 kB (added by sur, 9 months ago) |
|---|
-
test/base_test.rb
old new 559 559 560 560 Topic.increment_counter("replies_count", 1) 561 561 assert_equal 3, Topic.find(1).replies_count 562 563 Topic.increment_counter("replies_count", 1, 2) 564 assert_equal 5, Topic.find(1).replies_count 562 565 end 563 566 564 567 def test_decrement_counter … … 567 570 568 571 Topic.decrement_counter("replies_count", 2) 569 572 assert_equal -2, Topic.find(2).replies_count 573 574 Topic.decrement_counter("replies_count", 2, 2) 575 assert_equal -4, Topic.find(2).replies_count 570 576 end 571 577 572 578 def test_update_all -
lib/active_record/base.rb
old new 781 781 # 782 782 # +counter_name+ The name of the field that should be incremented 783 783 # +id+ The id of the object that should be incremented 784 # +by+ The amount by which to increment the column value (default is 1) 784 785 # 785 786 # ==== Examples 786 787 # 787 788 # # Increment the post_count column for the record with an id of 5 788 789 # DiscussionBoard.increment_counter(:post_count, 5) 789 def increment_counter(counter_name, id )790 update_counters(id, counter_name => 1)790 def increment_counter(counter_name, id, by = 1) 791 update_counters(id, counter_name => by.to_i.abs) 791 792 end 792 793 793 794 # Decrement a number field by one, usually representing a count. … … 798 799 # 799 800 # +counter_name+ The name of the field that should be decremented 800 801 # +id+ The id of the object that should be decremented 802 # +by+ The amount by which to decrement the column value (default is 1) 801 803 # 802 804 # ==== Examples 803 805 # 804 806 # # Decrement the post_count column for the record with an id of 5 805 807 # DiscussionBoard.decrement_counter(:post_count, 5) 806 def decrement_counter(counter_name, id )807 update_counters(id, counter_name => - 1)808 def decrement_counter(counter_name, id, by = 1) 809 update_counters(id, counter_name => -(by.to_i.abs)) 808 810 end 809 811 810 812