From d1abe806c54a0c0c845476715e47c52ca48cabab Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Thu, 6 Jan 2005 02:31:35 +0000 Subject: 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 --- activerecord/test/base_test.rb | 34 +++++++++++++++++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) (limited to 'activerecord/test/base_test.rb') 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 -- cgit v1.2.3