diff --git a/activesupport/lib/active_support/callbacks.rb b/activesupport/lib/active_support/callbacks.rb index 9c59b7a..2eb0a9a 100644 --- activesupport/lib/active_support/callbacks.rb +++ activesupport/lib/active_support/callbacks.rb @@ -269,7 +269,10 @@ module ActiveSupport # pass # stop def run_callbacks(kind, options = {}, &block) - self.class.send("#{kind}_callback_chain").run(self, options, &block) + callbacks = self.class.send("#{kind}_callback_chain") + metaclass_callbacks = (class << self; self; end).send("#{kind}_callback_chain") + callbacks |= metaclass_callbacks if metaclass_callbacks + callbacks.run(self, options, &block) end end end diff --git a/activesupport/test/callbacks_test.rb b/activesupport/test/callbacks_test.rb index 7f71ca2..b43e072 100644 --- activesupport/test/callbacks_test.rb +++ activesupport/test/callbacks_test.rb @@ -64,6 +64,19 @@ class ConditionalPerson < Record end end +class MetaclassCallbackCalled < Exception; end +module ModuleWithCallbacks + def self.extended(obj) + class << obj + before_save :raise_metaclass_callback_called + end + end + + def raise_metaclass_callback_called + raise MetaclassCallbackCalled + end +end + class CallbacksTest < Test::Unit::TestCase def test_save_person person = Person.new @@ -84,6 +97,14 @@ class CallbacksTest < Test::Unit::TestCase end end +class MetaclassCallbacksTest < Test::Unit::TestCase + def test_metaclass_callbacks + person = Person.new + person.extend ModuleWithCallbacks + assert_raise(MetaclassCallbackCalled) { person.save } + end +end + class ConditionalCallbackTest < Test::Unit::TestCase def test_save_conditional_person person = ConditionalPerson.new