Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Corrected new callbacks semantics with regards to using objects for a…

…round filters.
  • Loading branch information...
commit 67247ca8ee850223193a5fe77f5460aefc0336c0 1 parent 36058f4
Yehuda Katz + Carl Lerche authored
View
10 activesupport/lib/active_support/new_callbacks.rb
@@ -296,9 +296,13 @@ def #{method_name}
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
View
62 activesupport/test/new_callbacks_test.rb
@@ -396,6 +396,68 @@ def save
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
Please sign in to comment.
Something went wrong with that request. Please try again.