diff options
author | John Barnette <jbarnette@gmail.com> | 2008-04-27 21:09:52 -0700 |
---|---|---|
committer | Michael Koziarski <michael@koziarski.com> | 2008-04-28 16:23:46 +1200 |
commit | be48cad9a41ebf88ae19f2381a57917d532734c9 (patch) | |
tree | 0841b9ab9fcebff59f83577cb713758fb399c935 | |
parent | 850aba5473ce14edcd067f16badfa198e2070095 (diff) | |
download | rails-be48cad9a41ebf88ae19f2381a57917d532734c9.tar.gz rails-be48cad9a41ebf88ae19f2381a57917d532734c9.tar.bz2 rails-be48cad9a41ebf88ae19f2381a57917d532734c9.zip |
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>
-rw-r--r-- | activesupport/lib/active_support/core_ext/module/aliasing.rb | 4 | ||||
-rw-r--r-- | activesupport/test/core_ext/module_test.rb | 15 |
2 files changed, 18 insertions, 1 deletions
diff --git a/activesupport/lib/active_support/core_ext/module/aliasing.rb b/activesupport/lib/active_support/core_ext/module/aliasing.rb index 1894e3b0a2..cb7d4c1c3c 100644 --- a/activesupport/lib/active_support/core_ext/module/aliasing.rb +++ b/activesupport/lib/active_support/core_ext/module/aliasing.rb @@ -28,6 +28,10 @@ class Module 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 diff --git a/activesupport/test/core_ext/module_test.rb b/activesupport/test/core_ext/module_test.rb index ecdea38d44..d983044fc3 100644 --- a/activesupport/test/core_ext/module_test.rb +++ b/activesupport/test/core_ext/module_test.rb @@ -166,6 +166,19 @@ class MethodAliasingTest < Test::Unit::TestCase 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 @@ class MethodAliasingTest < Test::Unit::TestCase assert_equal 'duck_with_orange', @instance.duck assert FooClassWithBarMethod.public_method_defined?(:duck) - end + end end |