From 5e59d755361e50e3f8c54d80f2a769b84f52dcc7 Mon Sep 17 00:00:00 2001 From: kennyj Date: Sun, 22 Jan 2012 03:23:40 +0900 Subject: [PATCH] Fix GH #4344. A defined callback in extended module is called too. --- activesupport/lib/active_support/callbacks.rb | 2 +- activesupport/test/callbacks_test.rb | 56 +++++++++++++++++++ 2 files changed, 57 insertions(+), 1 deletion(-) diff --git a/activesupport/lib/active_support/callbacks.rb b/activesupport/lib/active_support/callbacks.rb index 9cea09db41..5eaeac2cb3 100644 --- a/activesupport/lib/active_support/callbacks.rb +++ b/activesupport/lib/active_support/callbacks.rb @@ -359,7 +359,7 @@ module ActiveSupport def __run_callbacks(key, kind, object, &blk) #:nodoc: name = __callback_runner_name(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} diff --git a/activesupport/test/callbacks_test.rb b/activesupport/test/callbacks_test.rb index 2c2a420619..032787f0f4 100644 --- a/activesupport/test/callbacks_test.rb +++ b/activesupport/test/callbacks_test.rb @@ -344,6 +344,54 @@ module CallbacksTest 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 < ActiveSupport::TestCase def test_save_around around = AroundPerson.new @@ -645,4 +693,12 @@ module CallbacksTest end end + class ExtendCallbacksTest < ActiveSupport::TestCase + def test_save + model = ExtendCallbacks.new.extend ExtendModule + model.save + assert_equal [1, 2, 3], model.recorder + end + end + end