diff options
author | José Valim <jose.valim@gmail.com> | 2009-09-08 10:22:45 -0500 |
---|---|---|
committer | Joshua Peek <josh@joshpeek.com> | 2009-09-08 10:26:39 -0500 |
commit | 2ea1d684d93bd59887a9fd12e647941f0d1f4868 (patch) | |
tree | 808bb2e7a081c5010df134269e765d3b4c0a12b9 /activesupport/test | |
parent | 4f37b97033f596ec2c95eb53e9964e051c224981 (diff) | |
download | rails-2ea1d684d93bd59887a9fd12e647941f0d1f4868.tar.gz rails-2ea1d684d93bd59887a9fd12e647941f0d1f4868.tar.bz2 rails-2ea1d684d93bd59887a9fd12e647941f0d1f4868.zip |
Refactor new callbacks and AR implementation.
Signed-off-by: Joshua Peek <josh@joshpeek.com>
Diffstat (limited to 'activesupport/test')
-rw-r--r-- | activesupport/test/new_callbacks_test.rb | 56 |
1 files changed, 45 insertions, 11 deletions
diff --git a/activesupport/test/new_callbacks_test.rb b/activesupport/test/new_callbacks_test.rb index 54b278cd56..04db376fc6 100644 --- a/activesupport/test/new_callbacks_test.rb +++ b/activesupport/test/new_callbacks_test.rb @@ -364,7 +364,7 @@ module NewCallbacksTest class CallbackTerminator include ActiveSupport::NewCallbacks - define_callbacks :save, "result == :halt" + define_callbacks :save, :terminator => "result == :halt" set_callback :save, :before, :first set_callback :save, :before, :second @@ -412,7 +412,11 @@ module NewCallbacksTest def before(caller) caller.record << "before" end - + + def before_save(caller) + caller.record << "before save" + end + def around(caller) caller.record << "around before" yield @@ -422,15 +426,15 @@ module NewCallbacksTest class UsingObjectBefore include ActiveSupport::NewCallbacks - + define_callbacks :save set_callback :save, :before, CallbackObject.new - + attr_accessor :record def initialize @record = [] end - + def save _run_save_callbacks do @record << "yielded" @@ -455,19 +459,49 @@ module NewCallbacksTest end end end - + + class CustomScopeObject + include ActiveSupport::NewCallbacks + + define_callbacks :save, :scope => [:kind, :name] + set_callback :save, :before, CallbackObject.new + + attr_accessor :record + def initialize + @record = [] + end + + def save + _run_save_callbacks do + @record << "yielded" + "CallbackResult" + end + end + end + class UsingObjectTest < Test::Unit::TestCase def test_before_object u = UsingObjectBefore.new u.save assert_equal ["before", "yielded"], u.record end - + def test_around_object u = UsingObjectAround.new u.save assert_equal ["around before", "yielded", "around after"], u.record - end + end + + def test_customized_object + u = CustomScopeObject.new + u.save + assert_equal ["before save", "yielded"], u.record + end + + def test_block_result_is_returned + u = CustomScopeObject.new + assert_equal "CallbackResult", u.save + end end class CallbackTerminatorTest < Test::Unit::TestCase @@ -481,7 +515,7 @@ module NewCallbacksTest obj = CallbackTerminator.new obj.save assert !obj.saved - end + end end class HyphenatedKeyTest < Test::Unit::TestCase @@ -489,6 +523,6 @@ module NewCallbacksTest obj = HyphenatedCallbacks.new obj.save assert_equal obj.stuff, "OMG" - end - end + end + end end |