Skip to content
This repository
Browse code

Added by parameter to increment, decrement, and their bang varieties …

…so you can do player1.increment!(:points, 5) (closes #10542) [Sam]

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@8534 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
commit 424c3306346320c09031cbe2e4672ec7ad862c48 1 parent 3229b59
David Heinemeier Hansson authored January 03, 2008
2  activerecord/CHANGELOG
... ...
@@ -1,5 +1,7 @@
1 1
 *SVN*
2 2
 
  3
+* Added by parameter to increment, decrement, and their bang varieties so you can do player1.increment!(:points, 5) #10542 [Sam]
  4
+
3 5
 * Optimize ActiveRecord::Base#exists? to use #select_all instead of #find.  Closes #10605 [jamesh, fcheung, protocool]
4 6
 
5 7
 * Don't unnecessarily load has_many associations in after_update callbacks.  Closes #6822 [stopdropandrew, canadaduane]
20  activerecord/lib/active_record/base.rb
@@ -2052,28 +2052,28 @@ def update_attributes!(attributes)
2052 2052
         save!
2053 2053
       end
2054 2054
 
2055  
-      # Initializes the +attribute+ to zero if nil and adds one. Only makes sense for number-based attributes. Returns self.
2056  
-      def increment(attribute)
  2055
+      # Initializes the +attribute+ to zero if nil and adds the value passed as +by+ (default is one). Only makes sense for number-based attributes. Returns self.
  2056
+      def increment(attribute, by = 1)
2057 2057
         self[attribute] ||= 0
2058  
-        self[attribute] += 1
  2058
+        self[attribute] += by
2059 2059
         self
2060 2060
       end
2061 2061
 
2062 2062
       # Increments the +attribute+ and saves the record.
2063  
-      def increment!(attribute)
2064  
-        increment(attribute).update_attribute(attribute, self[attribute])
  2063
+      def increment!(attribute, by = 1)
  2064
+        increment(attribute, by).update_attribute(attribute, self[attribute])
2065 2065
       end
2066 2066
 
2067  
-      # Initializes the +attribute+ to zero if nil and subtracts one. Only makes sense for number-based attributes. Returns self.
2068  
-      def decrement(attribute)
  2067
+      # Initializes the +attribute+ to zero if nil and subtracts the value passed as +by+ (default is one). Only makes sense for number-based attributes. Returns self.
  2068
+      def decrement(attribute, by = 1)
2069 2069
         self[attribute] ||= 0
2070  
-        self[attribute] -= 1
  2070
+        self[attribute] -= by
2071 2071
         self
2072 2072
       end
2073 2073
 
2074 2074
       # Decrements the +attribute+ and saves the record.
2075  
-      def decrement!(attribute)
2076  
-        decrement(attribute).update_attribute(attribute, self[attribute])
  2075
+      def decrement!(attribute, by = 1)
  2076
+        decrement(attribute, by).update_attribute(attribute, self[attribute])
2077 2077
       end
2078 2078
 
2079 2079
       # Turns an +attribute+ that's currently true into false and vice versa. Returns self.
18  activerecord/test/base_test.rb
@@ -1275,6 +1275,15 @@ def test_increment_nil_attribute
1275 1275
     assert_equal 1, topics(:first).parent_id
1276 1276
   end
1277 1277
   
  1278
+  def test_increment_attribute_by
  1279
+    assert_equal 50, accounts(:signals37).credit_limit
  1280
+    accounts(:signals37).increment! :credit_limit, 5
  1281
+    assert_equal 55, accounts(:signals37, :reload).credit_limit    
  1282
+
  1283
+    accounts(:signals37).increment(:credit_limit, 1).increment!(:credit_limit, 3)
  1284
+    assert_equal 59, accounts(:signals37, :reload).credit_limit
  1285
+  end
  1286
+  
1278 1287
   def test_decrement_attribute
1279 1288
     assert_equal 50, accounts(:signals37).credit_limit
1280 1289
 
@@ -1285,6 +1294,15 @@ def test_decrement_attribute
1285 1294
     assert_equal 47, accounts(:signals37, :reload).credit_limit
1286 1295
   end
1287 1296
   
  1297
+  def test_decrement_attribute_by
  1298
+    assert_equal 50, accounts(:signals37).credit_limit
  1299
+    accounts(:signals37).decrement! :credit_limit, 5
  1300
+    assert_equal 45, accounts(:signals37, :reload).credit_limit    
  1301
+
  1302
+    accounts(:signals37).decrement(:credit_limit, 1).decrement!(:credit_limit, 3)
  1303
+    assert_equal 41, accounts(:signals37, :reload).credit_limit
  1304
+  end
  1305
+  
1288 1306
   def test_toggle_attribute
1289 1307
     assert !topics(:first).approved?
1290 1308
     topics(:first).toggle!(:approved)

0 notes on commit 424c330

Please sign in to comment.
Something went wrong with that request. Please try again.