Permalink
Browse files

Run callbacks from object's metaclass [#575 state:resolved]

  • Loading branch information...
1 parent f4f6e57 commit be078ee162fcae883a5621a30929879cd783a238 @josh josh committed Jul 16, 2008
Showing with 35 additions and 1 deletion.
  1. +2 −0 activesupport/CHANGELOG
  2. +9 −1 activesupport/lib/active_support/callbacks.rb
  3. +24 −0 activesupport/test/callbacks_test.rb
View
@@ -1,5 +1,7 @@
*Edge*
+* Run callbacks from object's metaclass [Josh Peek]
+
* Add Array#in_groups which splits or iterates over the array in specified number of groups. #579. [Adrian Mugnolo] Example:
a = (1..10).to_a
@@ -269,7 +269,15 @@ def self.#{callback}_callback_chain
# pass
# stop
def run_callbacks(kind, options = {}, &block)
- self.class.send("#{kind}_callback_chain").run(self, options, &block)
+ callback_chain_method = "#{kind}_callback_chain"
+
+ # Meta class inherits Class so we don't have to merge it in 1.9
+ if RUBY_VERSION >= '1.9'
+ metaclass.send(callback_chain_method).run(self, options, &block)
+ else
+ callbacks = self.class.send(callback_chain_method) | metaclass.send(callback_chain_method)
+ callbacks.run(self, options, &block)
+ end
end
end
end
@@ -84,6 +84,30 @@ def test_save_person
end
end
+class MetaclassCallbacksTest < Test::Unit::TestCase
+ module ModuleWithCallbacks
+ def self.extended(object)
+ object.metaclass.before_save :raise_metaclass_callback_called
+ end
+
+ def module_callback_called?
+ @module_callback_called ||= false
+ end
+
+ def raise_metaclass_callback_called
+ @module_callback_called = true
+ end
+ end
+
+ def test_metaclass_callbacks
+ person = Person.new
+ person.extend(ModuleWithCallbacks)
+ assert !person.module_callback_called?
+ person.save
+ assert person.module_callback_called?
+ end
+end
+
class ConditionalCallbackTest < Test::Unit::TestCase
def test_save_conditional_person
person = ConditionalPerson.new

0 comments on commit be078ee

Please sign in to comment.