diff options
author | Andrew White <andrew.white@unboxed.co> | 2016-11-14 11:57:30 +0000 |
---|---|---|
committer | Andrew White <andrew.white@unboxed.co> | 2016-11-14 11:57:30 +0000 |
commit | 7c848e6dd493ff236d33a0410a92f4c3e5cc3c7f (patch) | |
tree | 068103efccb6b94709ff5b8d9cf05cf0ad31c200 /activesupport | |
parent | 8e43fc5ace8039370f233570863b34821a3be46f (diff) | |
download | rails-7c848e6dd493ff236d33a0410a92f4c3e5cc3c7f.tar.gz rails-7c848e6dd493ff236d33a0410a92f4c3e5cc3c7f.tar.bz2 rails-7c848e6dd493ff236d33a0410a92f4c3e5cc3c7f.zip |
Remove deprecated method alias_method_chain
Diffstat (limited to 'activesupport')
-rw-r--r-- | activesupport/CHANGELOG.md | 4 | ||||
-rw-r--r-- | activesupport/lib/active_support/core_ext/module/aliasing.rb | 48 | ||||
-rw-r--r-- | activesupport/test/core_ext/module_test.rb | 215 |
3 files changed, 4 insertions, 263 deletions
diff --git a/activesupport/CHANGELOG.md b/activesupport/CHANGELOG.md index 372583c533..af75d44b3a 100644 --- a/activesupport/CHANGELOG.md +++ b/activesupport/CHANGELOG.md @@ -1,3 +1,7 @@ +* Remove deprecated method `alias_method_chain` + + *Andrew White* + * Remove deprecated constant `MissingSourceFile` *Andrew White* diff --git a/activesupport/lib/active_support/core_ext/module/aliasing.rb b/activesupport/lib/active_support/core_ext/module/aliasing.rb index 4a04bdd446..c48bd3354a 100644 --- a/activesupport/lib/active_support/core_ext/module/aliasing.rb +++ b/activesupport/lib/active_support/core_ext/module/aliasing.rb @@ -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. # diff --git a/activesupport/test/core_ext/module_test.rb b/activesupport/test/core_ext/module_test.rb index 121f744a3c..8f2df22f27 100644 --- a/activesupport/test/core_ext/module_test.rb +++ b/activesupport/test/core_ext/module_test.rb @@ -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) |