提交 015477ae 编写于 作者: E Edouard CHIN

A regression in deprecate_methods was introduced in a982a42d:

- Refactoring alias_chain to Module#prepend broke the possibility to deprecate class methods since the module
  generated was prepended to the target's instance.

  A suggestion to fix this was  to use `AS#redefine_method` which would solve the
  problem but with the cost of redefining directly the method.
  Decided to go with the same alias_chain implementation as before instead.

- Fixes #33253
上级 d4ea114b
......@@ -54,23 +54,26 @@ def deprecate_methods(target_module, *method_names)
deprecator = options.delete(:deprecator) || self
method_names += options.keys
mod = Module.new do
method_names.each do |method_name|
define_method(method_name) do |*args, &block|
deprecator.deprecation_warning(method_name, options[method_name])
super(*args, &block)
end
method_names.each do |method_name|
aliased_method, punctuation = method_name.to_s.sub(/([?!=])$/, ""), $1
with_method = "#{aliased_method}_with_deprecation#{punctuation}"
without_method = "#{aliased_method}_without_deprecation#{punctuation}"
case
when target_module.protected_method_defined?(method_name)
protected method_name
when target_module.private_method_defined?(method_name)
private method_name
end
target_module.send(:define_method, with_method) do |*args, &block|
deprecator.deprecation_warning(method_name, options[method_name])
send(without_method, *args, &block)
end
end
target_module.prepend(mod)
target_module.send(:alias_method, without_method, method_name)
target_module.send(:alias_method, method_name, with_method)
case
when target_module.protected_method_defined?(without_method)
target_module.send(:protected, method_name)
when target_module.private_method_defined?(without_method)
target_module.send(:private, method_name)
end
end
end
end
end
......
......@@ -55,4 +55,39 @@ def test_deprecate_methods_private_method
assert(@klass.private_method_defined?(:old_private_method))
end
def test_deprecate_class_method
mod = Module.new do
extend self
def old_method
"abc"
end
end
ActiveSupport::Deprecation.deprecate_methods(mod, old_method: :new_method)
warning = /old_method is deprecated and will be removed from Rails \d.\d \(use new_method instead\)/
assert_deprecated(warning) { assert_equal "abc", mod.old_method }
end
def test_deprecate_method_when_class_extends_module
mod = Module.new do
def old_method
"abc"
end
end
@klass.extend mod
ActiveSupport::Deprecation.deprecate_methods(mod, old_method: :new_method)
warning = /old_method is deprecated and will be removed from Rails \d.\d \(use new_method instead\)/
assert_deprecated(warning) { assert_equal "abc", @klass.old_method }
end
def test_method_with_without_deprecation_is_exposed
ActiveSupport::Deprecation.deprecate_methods(@klass, old_method: :new_method)
warning = /old_method is deprecated and will be removed from Rails \d.\d \(use new_method instead\)/
assert_deprecated(warning) { assert_equal "abc", @klass.new.old_method_with_deprecation }
assert_equal "abc", @klass.new.old_method_without_deprecation
end
end
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册