Changeset 8534
- Timestamp:
- 01/03/08 00:30:22 (8 months ago)
- Files:
-
- trunk/activerecord/CHANGELOG (modified) (1 diff)
- trunk/activerecord/lib/active_record/base.rb (modified) (1 diff)
- trunk/activerecord/test/base_test.rb (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/activerecord/CHANGELOG
r8531 r8534 1 1 *SVN* 2 3 * Added by parameter to increment, decrement, and their bang varieties so you can do player1.increment!(:points, 5) #10542 [Sam] 2 4 3 5 * Optimize ActiveRecord::Base#exists? to use #select_all instead of #find. Closes #10605 [jamesh, fcheung, protocool] trunk/activerecord/lib/active_record/base.rb
r8531 r8534 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] += 12058 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])2065 end 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 )2063 def increment!(attribute, by = 1) 2064 increment(attribute, by).update_attribute(attribute, self[attribute]) 2065 end 2066 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] -= 12070 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 trunk/activerecord/test/base_test.rb
r8510 r8534 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 … … 1284 1293 accounts(:signals37).decrement(:credit_limit).decrement!(:credit_limit) 1285 1294 assert_equal 47, accounts(:signals37, :reload).credit_limit 1295 end 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 1286 1304 end 1287 1305