aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/test/base_test.rb
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/test/base_test.rb
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/test/base_test.rb')
-rwxr-xr-xactiverecord/test/base_test.rb34
1 files changed, 33 insertions, 1 deletions
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