aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord
diff options
context:
space:
mode:
authorDavid Heinemeier Hansson <david@loudthinking.com>2005-01-06 02:31:35 +0000
committerDavid Heinemeier Hansson <david@loudthinking.com>2005-01-06 02:31:35 +0000
commitd1abe806c54a0c0c845476715e47c52ca48cabab (patch)
treee80b7b355d37a2922b6f6d275b973249e4d27433 /activerecord
parentfd48e70d6cc450cd2c5baa8eb7567246d59b34d1 (diff)
downloadrails-d1abe806c54a0c0c845476715e47c52ca48cabab.tar.gz
rails-d1abe806c54a0c0c845476715e47c52ca48cabab.tar.bz2
rails-d1abe806c54a0c0c845476715e47c52ca48cabab.zip
Added Base#toggle(attribute) and Base#toggle!(attribute) that makes it 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
Diffstat (limited to 'activerecord')
-rw-r--r--activerecord/CHANGELOG19
-rwxr-xr-xactiverecord/lib/active_record/base.rb47
-rwxr-xr-xactiverecord/test/base_test.rb34
3 files changed, 93 insertions, 7 deletions
diff --git a/activerecord/CHANGELOG b/activerecord/CHANGELOG
index be46c22096..cea70c095a 100644
--- a/activerecord/CHANGELOG
+++ b/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
diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb
index 7471e91516..76b9ea0e36 100755
--- a/activerecord/lib/active_record/base.rb
+++ b/activerecord/lib/active_record/base.rb
@@ -828,8 +828,8 @@ module ActiveRecord #:nodoc:
# 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 @@ module ActiveRecord #:nodoc:
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 @@ module ActiveRecord #:nodoc:
# 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 @@ module ActiveRecord #:nodoc:
string[0..3] == "--- "
end
end
-end
+end \ No newline at end of file
diff --git a/activerecord/test/base_test.rb b/activerecord/test/base_test.rb
index 1094430dbf..07740547cd 100755
--- a/activerecord/test/base_test.rb
+++ b/activerecord/test/base_test.rb
@@ -585,4 +585,36 @@ class BasicsTest < Test::Unit::TestCase
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 \ No newline at end of file