1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Remove deprecated method alias_method_chain

This commit is contained in:
Andrew White 2016-11-14 11:57:30 +00:00
parent 8e43fc5ace
commit 7c848e6dd4
3 changed files with 4 additions and 263 deletions

View file

@ -1,3 +1,7 @@
* Remove deprecated method `alias_method_chain`
*Andrew White*
* Remove deprecated constant `MissingSourceFile`
*Andrew White*

View file

@ -1,52 +1,4 @@
class Module
# NOTE: This method is deprecated. Please use <tt>Module#prepend</tt> that
# comes with Ruby 2.0 or newer instead.
#
# Encapsulates the common pattern of:
#
# alias_method :foo_without_feature, :foo
# alias_method :foo, :foo_with_feature
#
# With this, you simply do:
#
# alias_method_chain :foo, :feature
#
# And both aliases are set up for you.
#
# Query and bang methods (foo?, foo!) keep the same punctuation:
#
# alias_method_chain :foo?, :feature
#
# is equivalent to
#
# alias_method :foo_without_feature?, :foo?
# alias_method :foo?, :foo_with_feature?
#
# so you can safely chain foo, foo?, foo! and/or foo= with the same feature.
def alias_method_chain(target, feature)
ActiveSupport::Deprecation.warn("alias_method_chain is deprecated. Please, use Module#prepend instead. From module, you can access the original method using super.")
# Strip out punctuation on predicates, bang or writer methods since
# e.g. target?_without_feature is not a valid method name.
aliased_target, punctuation = target.to_s.sub(/([?!=])$/, ""), $1
yield(aliased_target, punctuation) if block_given?
with_method = "#{aliased_target}_with_#{feature}#{punctuation}"
without_method = "#{aliased_target}_without_#{feature}#{punctuation}"
alias_method without_method, target
alias_method target, with_method
case
when public_method_defined?(without_method)
public target
when protected_method_defined?(without_method)
protected target
when private_method_defined?(without_method)
private target
end
end
# Allows you to make aliases for attributes, which includes
# getter, setter, and a predicate.
#

View file

@ -385,221 +385,6 @@ class ModuleTest < ActiveSupport::TestCase
assert_equal [Yz::Zy, Yz, Object], Yz::Zy::Cd.parents
assert_equal [Yz, Object], Yz::Zy.parents
end
end
module BarMethodAliaser
def self.included(foo_class)
foo_class.class_eval do
include BarMethods
alias_method_chain :bar, :baz
end
end
end
module BarMethods
def bar_with_baz
bar_without_baz << "_with_baz"
end
def quux_with_baz!
quux_without_baz! << "_with_baz"
end
def quux_with_baz?
false
end
def quux_with_baz=(v)
send(:quux_without_baz=, v) << "_with_baz"
end
def duck_with_orange
duck_without_orange << "_with_orange"
end
end
class MethodAliasingTest < ActiveSupport::TestCase
def setup
Object.const_set :FooClassWithBarMethod, Class.new { def bar() "bar" end }
@instance = FooClassWithBarMethod.new
end
def teardown
Object.instance_eval { remove_const :FooClassWithBarMethod }
end
def test_alias_method_chain_deprecated
assert_deprecated(/alias_method_chain/) do
Module.new do
def base
end
def base_with_deprecated
end
alias_method_chain :base, :deprecated
end
end
end
def test_alias_method_chain
assert_deprecated(/alias_method_chain/) do
assert @instance.respond_to?(:bar)
feature_aliases = [:bar_with_baz, :bar_without_baz]
feature_aliases.each do |method|
assert !@instance.respond_to?(method)
end
assert_equal "bar", @instance.bar
FooClassWithBarMethod.class_eval { include BarMethodAliaser }
feature_aliases.each do |method|
assert_respond_to @instance, method
end
assert_equal "bar_with_baz", @instance.bar
assert_equal "bar", @instance.bar_without_baz
end
end
def test_alias_method_chain_with_punctuation_method
assert_deprecated(/alias_method_chain/) do
FooClassWithBarMethod.class_eval do
def quux!; "quux" end
end
assert !@instance.respond_to?(:quux_with_baz!)
FooClassWithBarMethod.class_eval do
include BarMethodAliaser
alias_method_chain :quux!, :baz
end
assert_respond_to @instance, :quux_with_baz!
assert_equal "quux_with_baz", @instance.quux!
assert_equal "quux", @instance.quux_without_baz!
end
end
def test_alias_method_chain_with_same_names_between_predicates_and_bang_methods
assert_deprecated(/alias_method_chain/) do
FooClassWithBarMethod.class_eval do
def quux!; "quux!" end
def quux?; true end
def quux=(v); "quux=" end
end
assert !@instance.respond_to?(:quux_with_baz!)
assert !@instance.respond_to?(:quux_with_baz?)
assert !@instance.respond_to?(:quux_with_baz=)
FooClassWithBarMethod.class_eval { include BarMethodAliaser }
assert_respond_to @instance, :quux_with_baz!
assert_respond_to @instance, :quux_with_baz?
assert_respond_to @instance, :quux_with_baz=
FooClassWithBarMethod.alias_method_chain :quux!, :baz
assert_equal "quux!_with_baz", @instance.quux!
assert_equal "quux!", @instance.quux_without_baz!
FooClassWithBarMethod.alias_method_chain :quux?, :baz
assert_equal false, @instance.quux?
assert_equal true, @instance.quux_without_baz?
FooClassWithBarMethod.alias_method_chain :quux=, :baz
assert_equal "quux=_with_baz", @instance.send(:quux=, 1234)
assert_equal "quux=", @instance.send(:quux_without_baz=, 1234)
end
end
def test_alias_method_chain_with_feature_punctuation
assert_deprecated(/alias_method_chain/) do
FooClassWithBarMethod.class_eval do
def quux; "quux" end
def quux?; "quux?" end
include BarMethodAliaser
alias_method_chain :quux, :baz!
end
assert_nothing_raised do
assert_equal "quux_with_baz", @instance.quux_with_baz!
end
assert_raise(NameError) do
FooClassWithBarMethod.alias_method_chain :quux?, :baz!
end
end
end
def test_alias_method_chain_yields_target_and_punctuation
assert_deprecated(/alias_method_chain/) do
args = nil
FooClassWithBarMethod.class_eval do
def quux?; end
include BarMethods
FooClassWithBarMethod.alias_method_chain :quux?, :baz do |target, punctuation|
args = [target, punctuation]
end
end
assert_not_nil args
assert_equal "quux", args[0]
assert_equal "?", args[1]
end
end
def test_alias_method_chain_preserves_private_method_status
assert_deprecated(/alias_method_chain/) do
FooClassWithBarMethod.class_eval do
def duck; "duck" end
include BarMethodAliaser
private :duck
alias_method_chain :duck, :orange
end
assert_raise NoMethodError do
@instance.duck
end
assert_equal "duck_with_orange", @instance.instance_eval { duck }
assert FooClassWithBarMethod.private_method_defined?(:duck)
end
end
def test_alias_method_chain_preserves_protected_method_status
assert_deprecated(/alias_method_chain/) do
FooClassWithBarMethod.class_eval do
def duck; "duck" end
include BarMethodAliaser
protected :duck
alias_method_chain :duck, :orange
end
assert_raise NoMethodError do
@instance.duck
end
assert_equal "duck_with_orange", @instance.instance_eval { duck }
assert FooClassWithBarMethod.protected_method_defined?(:duck)
end
end
def test_alias_method_chain_preserves_public_method_status
assert_deprecated(/alias_method_chain/) do
FooClassWithBarMethod.class_eval do
def duck; "duck" end
include BarMethodAliaser
public :duck
alias_method_chain :duck, :orange
end
assert_equal "duck_with_orange", @instance.duck
assert FooClassWithBarMethod.public_method_defined?(:duck)
end
end
def test_delegate_with_case
event = Event.new(Tester.new)