mirror of
https://github.com/github/rails.git
synced 2026-02-04 03:05:27 -05:00
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
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
*SVN*
|
||||
|
||||
* Added by parameter to increment, decrement, and their bang varieties so you can do player1.increment!(:points, 5) #10542 [Sam]
|
||||
|
||||
* Optimize ActiveRecord::Base#exists? to use #select_all instead of #find. Closes #10605 [jamesh, fcheung, protocool]
|
||||
|
||||
* Don't unnecessarily load has_many associations in after_update callbacks. Closes #6822 [stopdropandrew, canadaduane]
|
||||
|
||||
@@ -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.
|
||||
|
||||
@@ -1275,6 +1275,15 @@ class BasicsTest < Test::Unit::TestCase
|
||||
assert_equal 1, topics(:first).parent_id
|
||||
end
|
||||
|
||||
def test_increment_attribute_by
|
||||
assert_equal 50, accounts(:signals37).credit_limit
|
||||
accounts(:signals37).increment! :credit_limit, 5
|
||||
assert_equal 55, accounts(:signals37, :reload).credit_limit
|
||||
|
||||
accounts(:signals37).increment(:credit_limit, 1).increment!(:credit_limit, 3)
|
||||
assert_equal 59, accounts(:signals37, :reload).credit_limit
|
||||
end
|
||||
|
||||
def test_decrement_attribute
|
||||
assert_equal 50, accounts(:signals37).credit_limit
|
||||
|
||||
@@ -1285,6 +1294,15 @@ class BasicsTest < Test::Unit::TestCase
|
||||
assert_equal 47, accounts(:signals37, :reload).credit_limit
|
||||
end
|
||||
|
||||
def test_decrement_attribute_by
|
||||
assert_equal 50, accounts(:signals37).credit_limit
|
||||
accounts(:signals37).decrement! :credit_limit, 5
|
||||
assert_equal 45, accounts(:signals37, :reload).credit_limit
|
||||
|
||||
accounts(:signals37).decrement(:credit_limit, 1).decrement!(:credit_limit, 3)
|
||||
assert_equal 41, accounts(:signals37, :reload).credit_limit
|
||||
end
|
||||
|
||||
def test_toggle_attribute
|
||||
assert !topics(:first).approved?
|
||||
topics(:first).toggle!(:approved)
|
||||
|
||||
Reference in New Issue
Block a user