Permalink
Browse files

Fix GH #4344. A defined callback in extended module is called too.

Conflicts:

	activesupport/test/callbacks_test.rb
  • Loading branch information...
1 parent 1a56a76 commit dc826f9977650c105e9bfd308d6d4fc71bb6ec6e @kennyj kennyj committed with josevalim Jan 21, 2012
Showing with 62 additions and 5 deletions.
  1. +1 −1 activesupport/lib/active_support/callbacks.rb
  2. +61 −4 activesupport/test/callbacks_test.rb
@@ -396,7 +396,7 @@ def #{runner_method}(key = nil, &blk)
def __run_callback(key, kind, object, &blk) #:nodoc:
name = __callback_runner_name(key, kind)
unless object.respond_to?(name)
- str = send("_#{kind}_callbacks").compile(key, object)
+ str = object.send("_#{kind}_callbacks").compile(key, object)
class_eval <<-RUBY_EVAL, __FILE__, __LINE__ + 1
def #{name}() #{str} end
protected :#{name}
@@ -298,7 +298,7 @@ def save
end
end
end
-
+
class AroundPersonResult < MySuper
attr_reader :result
@@ -309,15 +309,15 @@ class AroundPersonResult < MySuper
def tweedle_dum
@result = yield
end
-
+
def tweedle_1
:tweedle_1
end
def tweedle_2
:tweedle_2
end
-
+
def save
run_callbacks :save do
:running
@@ -345,6 +345,55 @@ def save
end
end
+
+ module ExtendModule
+ def self.extended(base)
+ base.class_eval do
+ set_callback :save, :before, :record3
+ end
+ end
+ def record3
+ @recorder << 3
+ end
+ end
+
+ module IncludeModule
+ def self.included(base)
+ base.class_eval do
+ set_callback :save, :before, :record2
+ end
+ end
+ def record2
+ @recorder << 2
+ end
+ end
+
+ class ExtendCallbacks
+
+ include ActiveSupport::Callbacks
+
+ define_callbacks :save
+ set_callback :save, :before, :record1
+
+ include IncludeModule
+
+ def save
+ run_callbacks :save
+ end
+
+ attr_reader :recorder
+
+ def initialize
+ @recorder = []
+ end
+
+ private
+
+ def record1
+ @recorder << 1
+ end
+ end
+
class AroundCallbacksTest < Test::Unit::TestCase
def test_save_around
around = AroundPerson.new
@@ -363,7 +412,7 @@ def test_save_around
], around.history
end
end
-
+
class AroundCallbackResultTest < Test::Unit::TestCase
def test_save_around
around = AroundPersonResult.new
@@ -646,4 +695,12 @@ def test_skip_writer
end
end
+ class ExtendCallbacksTest < Test::Unit::TestCase
+ def test_save
+ model = ExtendCallbacks.new.extend ExtendModule
+ model.save
+ assert_equal [1, 2, 3], model.recorder
+ end
+ end
+
end

0 comments on commit dc826f9

Please sign in to comment.