diff options
author | Yehuda Katz + Carl Lerche <ykatz+clerche@engineyard.com> | 2009-05-19 17:41:17 -0700 |
---|---|---|
committer | Yehuda Katz + Carl Lerche <ykatz+clerche@engineyard.com> | 2009-05-19 18:11:44 -0700 |
commit | 67247ca8ee850223193a5fe77f5460aefc0336c0 (patch) | |
tree | 8870ef4ccd99585d8a7a1628f4f5e217ad711cc6 /activesupport | |
parent | 36058f45040b5559fd8f6a44a17ead27a6b3d2f7 (diff) | |
download | rails-67247ca8ee850223193a5fe77f5460aefc0336c0.tar.gz rails-67247ca8ee850223193a5fe77f5460aefc0336c0.tar.bz2 rails-67247ca8ee850223193a5fe77f5460aefc0336c0.zip |
Corrected new callbacks semantics with regards to using objects for around filters.
Diffstat (limited to 'activesupport')
-rw-r--r-- | activesupport/lib/active_support/new_callbacks.rb | 10 | ||||
-rw-r--r-- | activesupport/test/new_callbacks_test.rb | 62 |
2 files changed, 69 insertions, 3 deletions
diff --git a/activesupport/lib/active_support/new_callbacks.rb b/activesupport/lib/active_support/new_callbacks.rb index 9316d6d2b6..f8108780f1 100644 --- a/activesupport/lib/active_support/new_callbacks.rb +++ b/activesupport/lib/active_support/new_callbacks.rb @@ -296,9 +296,13 @@ module ActiveSupport method_name else kind, name = @kind, @name - @klass.send(:define_method, method_name) do - filter.send("#{kind}_#{name}", self) - end + @klass.send(:define_method, "#{method_name}_object") { filter } + + @klass.class_eval <<-RUBY_EVAL, __FILE__, __LINE__ + 1 + def #{method_name}(&blk) + #{method_name}_object.send("#{kind}_#{name}", self, &blk) + end + RUBY_EVAL method_name end end diff --git a/activesupport/test/new_callbacks_test.rb b/activesupport/test/new_callbacks_test.rb index dec6106ac1..7bec47224d 100644 --- a/activesupport/test/new_callbacks_test.rb +++ b/activesupport/test/new_callbacks_test.rb @@ -396,6 +396,68 @@ module NewCallbacksTest end end + class CallbackObject + def before_save(caller) + caller.record << "before" + end + + def around_save(caller) + caller.record << "around before" + yield + caller.record << "around after" + end + end + + class UsingObjectBefore + include ActiveSupport::NewCallbacks + + define_callbacks :save + save_callback :before, CallbackObject.new + + attr_accessor :record + def initialize + @record = [] + end + + def save + _run_save_callbacks do + @record << "yielded" + end + end + end + + class UsingObjectAround + include ActiveSupport::NewCallbacks + + define_callbacks :save + save_callback :around, CallbackObject.new + + attr_accessor :record + def initialize + @record = [] + end + + def save + _run_save_callbacks do + @record << "yielded" + 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 + class CallbackTerminatorTest < Test::Unit::TestCase def test_termination terminator = CallbackTerminator.new |