Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Make alias_method_chain complain about duplicate aliases. Previously

repeated calls to alias_method_chain would cause infinite loops.

Signed-Off-By: Michael Koziarski <michael@koziarski.com>
  • Loading branch information...
commit be48cad9a41ebf88ae19f2381a57917d532734c9 1 parent 850aba5
@jbarnette jbarnette authored NZKoz committed
View
4 activesupport/lib/active_support/core_ext/module/aliasing.rb
@@ -28,6 +28,10 @@ def alias_method_chain(target, feature)
with_method, without_method = "#{aliased_target}_with_#{feature}#{punctuation}", "#{aliased_target}_without_#{feature}#{punctuation}"
+ if method_defined?(without_method) || private_method_defined?(without_method)
+ raise NameError, "#{with_method} is already in #{target}'s alias chain."
+ end
+
alias_method without_method, target
alias_method target, with_method
View
15 activesupport/test/core_ext/module_test.rb
@@ -166,6 +166,19 @@ def test_alias_method_chain
assert_equal 'bar_with_baz', @instance.bar
assert_equal 'bar', @instance.bar_without_baz
end
+
+ def test_alias_method_chain_complains_the_second_time
+ FooClassWithBarMethod.class_eval do
+ def bar_with_magic; end
+ alias_method_chain :bar, :magic
+ end
+
+ assert_raise(NameError) do
+ FooClassWithBarMethod.class_eval do
+ alias_method_chain :bar, :magic
+ end
+ end
+ end
def test_alias_method_chain_with_punctuation_method
FooClassWithBarMethod.class_eval do
@@ -289,5 +302,5 @@ def duck; 'duck' end
assert_equal 'duck_with_orange', @instance.duck
assert FooClassWithBarMethod.public_method_defined?(:duck)
- end
+ end
end
Please sign in to comment.
Something went wrong with that request. Please try again.