diff options
author | David Heinemeier Hansson <david@loudthinking.com> | 2008-01-03 00:30:22 +0000 |
---|---|---|
committer | David Heinemeier Hansson <david@loudthinking.com> | 2008-01-03 00:30:22 +0000 |
commit | 424c3306346320c09031cbe2e4672ec7ad862c48 (patch) | |
tree | 1a8802cafaeeb7f1251f4e59341d4407922826b1 /activerecord/lib/active_record/base.rb | |
parent | 3229b59495270a094d3a0f83814f1c3d5ebec7e3 (diff) | |
download | rails-424c3306346320c09031cbe2e4672ec7ad862c48.tar.gz rails-424c3306346320c09031cbe2e4672ec7ad862c48.tar.bz2 rails-424c3306346320c09031cbe2e4672ec7ad862c48.zip |
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
Diffstat (limited to 'activerecord/lib/active_record/base.rb')
-rwxr-xr-x | activerecord/lib/active_record/base.rb | 20 |
1 files changed, 10 insertions, 10 deletions
diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb index 2cdb43566c..149ee61c52 100755 --- a/activerecord/lib/active_record/base.rb +++ b/activerecord/lib/active_record/base.rb @@ -2052,28 +2052,28 @@ module ActiveRecord #:nodoc: save! end - # Initializes the +attribute+ to zero if nil and adds one. Only makes sense for number-based attributes. Returns self. - def increment(attribute) + # 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. + def increment(attribute, by = 1) self[attribute] ||= 0 - self[attribute] += 1 + self[attribute] += by self end # Increments the +attribute+ and saves the record. - def increment!(attribute) - increment(attribute).update_attribute(attribute, self[attribute]) + def increment!(attribute, by = 1) + increment(attribute, by).update_attribute(attribute, self[attribute]) end - # Initializes the +attribute+ to zero if nil and subtracts one. Only makes sense for number-based attributes. Returns self. - def decrement(attribute) + # 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. + def decrement(attribute, by = 1) self[attribute] ||= 0 - self[attribute] -= 1 + self[attribute] -= by self end # Decrements the +attribute+ and saves the record. - def decrement!(attribute) - decrement(attribute).update_attribute(attribute, self[attribute]) + def decrement!(attribute, by = 1) + decrement(attribute, by).update_attribute(attribute, self[attribute]) end # Turns an +attribute+ that's currently true into false and vice versa. Returns self. |