Define callbacks on descendants.

We set callbacks on all descendants, so we need to make sure that they are also defined on all descendants as well.
This commit is contained in:
Ryan Wallace 2018-02-06 14:53:26 -05:00
parent d57c52a385
commit 56640b4de4
2 changed files with 29 additions and 17 deletions

View File

@ -806,28 +806,30 @@ module ActiveSupport
def define_callbacks(*names)
options = names.extract_options!
names.each do |name|
name = name.to_sym
([self] + ActiveSupport::DescendantsTracker.descendants(self)).each do |target|
names.each do |name|
name = name.to_sym
set_callbacks name, CallbackChain.new(name, options)
target.set_callbacks name, CallbackChain.new(name, options)
module_eval <<-RUBY, __FILE__, __LINE__ + 1
def _run_#{name}_callbacks(&block)
run_callbacks #{name.inspect}, &block
end
target.module_eval <<-RUBY, __FILE__, __LINE__ + 1
def _run_#{name}_callbacks(&block)
run_callbacks #{name.inspect}, &block
end
def self._#{name}_callbacks
get_callbacks(#{name.inspect})
end
def self._#{name}_callbacks
get_callbacks(#{name.inspect})
end
def self._#{name}_callbacks=(value)
set_callbacks(#{name.inspect}, value)
end
def self._#{name}_callbacks=(value)
set_callbacks(#{name.inspect}, value)
end
def _#{name}_callbacks
__callbacks[#{name.inspect}]
end
RUBY
def _#{name}_callbacks
__callbacks[#{name.inspect}]
end
RUBY
end
end
end

View File

@ -176,3 +176,13 @@ class DynamicInheritedCallbacks < ActiveSupport::TestCase
assert_equal 1, child.count
end
end
class DynamicDefinedCallbacks < ActiveSupport::TestCase
def test_callbacks_should_be_performed_once_in_child_class_after_dynamic_define
GrandParent.define_callbacks(:foo)
GrandParent.set_callback(:foo, :before, :before1)
parent = Parent.new("foo")
parent.run_callbacks(:foo)
assert_equal %w(before1), parent.log
end
end