Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Changeset 8534

Show
Ignore:
Timestamp:
01/03/08 00:30:22 (8 months ago)
Author:
david
Message:

Added by parameter to increment, decrement, and their bang varieties so you can do player1.increment!(:points, 5) (closes #10542) [Sam]

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/activerecord/CHANGELOG

    r8531 r8534  
    11*SVN* 
     2 
     3* Added by parameter to increment, decrement, and their bang varieties so you can do player1.increment!(:points, 5) #10542 [Sam] 
    24 
    35* 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  
    20532053      end 
    20542054 
    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
    20572057        self[attribute] ||= 0 
    2058         self[attribute] += 1 
     2058        self[attribute] += by 
    20592059        self 
    20602060      end 
    20612061 
    20622062      # 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
    20692069        self[attribute] ||= 0 
    2070         self[attribute] -= 1 
     2070        self[attribute] -= by 
    20712071        self 
    20722072      end 
    20732073 
    20742074      # 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]) 
    20772077      end 
    20782078 
  • trunk/activerecord/test/base_test.rb

    r8510 r8534  
    12761276  end 
    12771277   
     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   
    12781287  def test_decrement_attribute 
    12791288    assert_equal 50, accounts(:signals37).credit_limit 
     
    12841293    accounts(:signals37).decrement(:credit_limit).decrement!(:credit_limit) 
    12851294    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 
    12861304  end 
    12871305