Skip to content
Browse files

Added Base#toggle(attribute) and Base#toggle!(attribute) that makes i…

…t easier to flip a switch or flag. Added Base#increment!(attribute) and Base#decrement!(attribute) that also saves the records. Added Base#increment(attribute) and Base#decrement(attribute) that encapsulates the += 1 and -= 1 patterns.

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@340 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
1 parent fd48e70 commit d1abe806c54a0c0c845476715e47c52ca48cabab @dhh dhh committed Jan 6, 2005
Showing with 93 additions and 7 deletions.
  1. +19 −0 activerecord/CHANGELOG
  2. +41 −6 activerecord/lib/active_record/base.rb
  3. +33 −1 activerecord/test/base_test.rb
View
19 activerecord/CHANGELOG
@@ -1,3 +1,22 @@
+*SVN*
+
+* Added Base#toggle(attribute) and Base#toggle!(attribute) that makes it easier to flip a switch or flag.
+
+ Before: topic.update_attribute(:approved, !approved?)
+ After : topic.toggle!(:approved)
+
+* Added Base#increment!(attribute) and Base#decrement!(attribute) that also saves the records. Example:
+
+ page.views # => 1
+ page.increment!(:views) # executes an UPDATE statement
+ page.views # => 2
+
+ page.increment(:views).increment!(:views)
+ page.views # => 4
+
+* Added Base#increment(attribute) and Base#decrement(attribute) that encapsulates the += 1 and -= 1 patterns.
+
+
*1.4.0* (January 4th, 2005)
* Added automated optimistic locking if the field <tt>lock_version</tt> is present. Each update to the
View
47 activerecord/lib/active_record/base.rb
@@ -828,8 +828,8 @@ def clone
# Note: This method is overwritten by the Validation module that'll make sure that updates made with this method
# doesn't get subjected to validation checks. Hence, attributes can be updated even if the full object isn't valid.
def update_attribute(name, value)
- self[name] = value
- return true
+ self[name.to_s] = value
+ save
end
# Updates all the attributes in from the passed hash and saves the record. If the object is invalid, the saving will
@@ -839,17 +839,52 @@ def update_attributes(attributes)
return save
end
+ # Initializes the +attribute+ to zero if nil and adds one. Only makes sense for number-based attributes. Returns self.
+ def increment(attribute)
+ self[attribute] ||= 0
+ self[attribute] += 1
+ self
+ end
+
+ # Increments the +attribute+ and saves the record.
+ def increment!(attribute)
+ increment(attribute).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)
+ self[attribute] ||= 0
+ self[attribute] -= 1
+ self
+ end
+
+ # Decrements the +attribute+ and saves the record.
+ def decrement!(attribute)
+ decrement(attribute).update_attribute(attribute, self[attribute])
+ end
+
+ # Turns an +attribute+ that's currently true into false and vice versa. Returns self.
+ def toggle(attribute)
+ self[attribute] = quote(!send("#{attribute}?", column_for_attribute(attribute)))
+ self
+ end
+
+ # Toggles the +attribute+ and saves the record.
+ def toggle!(attribute)
+ toggle(attribute).update_attribute(attribute, self[attribute])
+ end
+
# Returns the value of attribute identified by <tt>attr_name</tt> after it has been type cast (for example,
# "2004-12-12" in a data column is cast to a date object, like Date.new(2004, 12, 12)).
# (Alias for the protected read_attribute method).
def [](attr_name)
- read_attribute(attr_name)
+ read_attribute(attr_name.to_s)
end
# Updates the attribute identified by <tt>attr_name</tt> with the specified +value+.
# (Alias for the protected write_attribute method).
def []= (attr_name, value)
- write_attribute(attr_name, value)
+ write_attribute(attr_name.to_s, value)
end
# Allows you to set all the attributes at once by passing in a hash with keys
@@ -881,7 +916,7 @@ def attribute_names
# Returns the column object for the named attribute.
def column_for_attribute(name)
- self.class.columns_hash[name]
+ self.class.columns_hash[name.to_s]
end
# Returns true if the +comparison_object+ is of the same type and has the same id.
@@ -1176,4 +1211,4 @@ def has_yaml_encoding_header?(string)
string[0..3] == "--- "
end
end
-end
+end
View
34 activerecord/test/base_test.rb
@@ -585,4 +585,36 @@ def test_class_level_delete
assert_raises(ActiveRecord::RecordNotFound) { Topic.find(1) }
assert_nothing_raised { Reply.find(should_be_destroyed_reply.id) }
end
-end
+
+ def test_increment_attribute
+ assert_equal 0, @topics["first"].find.replies_count
+ @topics["first"].find.increment! :replies_count
+ assert_equal 1, @topics["first"].find.replies_count
+
+ @topics["first"].find.increment(:replies_count).increment!(:replies_count)
+ assert_equal 3, @topics["first"].find.replies_count
+ end
+
+ def test_increment_nil_attribute
+ assert_nil @topics["first"].find.parent_id
+ @topics["first"].find.increment! :parent_id
+ assert_equal 1, @topics["first"].find.parent_id
+ end
+
+ def test_decrement_attribute
+ @topics["first"].find.increment(:replies_count).increment!(:replies_count)
+ assert_equal 2, @topics["first"].find.replies_count
+
+ @topics["first"].find.decrement!(:replies_count)
+ assert_equal 1, @topics["first"].find.replies_count
+
+ @topics["first"].find.decrement(:replies_count).decrement!(:replies_count)
+ assert_equal -1, @topics["first"].find.replies_count
+ end
+
+ def test_toggle_attribute
+ assert !@topics["first"].find.approved?
+ @topics["first"].find.toggle!(:approved)
+ assert @topics["first"].find.approved?
+ end
+end

0 comments on commit d1abe80

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