diff options
author | Jeremy Kemper <jeremy@bitsweat.net> | 2007-03-18 03:16:53 +0000 |
---|---|---|
committer | Jeremy Kemper <jeremy@bitsweat.net> | 2007-03-18 03:16:53 +0000 |
commit | 46f092097bf2e6e827a1a3c9485e326cafc0beb6 (patch) | |
tree | 715c4492eaa8465a3eaebe83694d4858973bc053 /activesupport | |
parent | a38f28fff1e9e3faeeb22ac9b7c6b3cdcb7e2b29 (diff) | |
download | rails-46f092097bf2e6e827a1a3c9485e326cafc0beb6.tar.gz rails-46f092097bf2e6e827a1a3c9485e326cafc0beb6.tar.bz2 rails-46f092097bf2e6e827a1a3c9485e326cafc0beb6.zip |
alias_method_chain preserves the original method's visibility. Closes #7854.
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@6441 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'activesupport')
-rw-r--r-- | activesupport/CHANGELOG | 2 | ||||
-rw-r--r-- | activesupport/lib/active_support/core_ext/module/aliasing.rb | 16 | ||||
-rw-r--r-- | activesupport/test/core_ext/module_test.rb | 45 |
3 files changed, 61 insertions, 2 deletions
diff --git a/activesupport/CHANGELOG b/activesupport/CHANGELOG index deddebd458..63ca49bec4 100644 --- a/activesupport/CHANGELOG +++ b/activesupport/CHANGELOG @@ -1,5 +1,7 @@ *SVN* +* alias_method_chain preserves the original method's visibility. #7854 [Jonathan Viney] + * Update Dependencies to ignore constants inherited from ancestors. Closes #6951. [Nicholas Seckar] * Array#to_query preserves its ordering. #7756 [Greg Spurrier] diff --git a/activesupport/lib/active_support/core_ext/module/aliasing.rb b/activesupport/lib/active_support/core_ext/module/aliasing.rb index 81e81696d8..b36874520b 100644 --- a/activesupport/lib/active_support/core_ext/module/aliasing.rb +++ b/activesupport/lib/active_support/core_ext/module/aliasing.rb @@ -25,8 +25,20 @@ class Module # 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? - alias_method "#{aliased_target}_without_#{feature}#{punctuation}", target - alias_method target, "#{aliased_target}_with_#{feature}#{punctuation}" + + with_method, without_method = "#{aliased_target}_with_#{feature}#{punctuation}", "#{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 diff --git a/activesupport/test/core_ext/module_test.rb b/activesupport/test/core_ext/module_test.rb index 884b44030c..73ab371da5 100644 --- a/activesupport/test/core_ext/module_test.rb +++ b/activesupport/test/core_ext/module_test.rb @@ -129,6 +129,10 @@ module BarMethods 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 < Test::Unit::TestCase @@ -226,4 +230,45 @@ class MethodAliasingTest < Test::Unit::TestCase end assert block_called end + + def test_alias_method_chain_preserves_private_method_status + FooClassWithBarMethod.send(:define_method, 'duck', Proc.new { 'duck' }) + FooClassWithBarMethod.send(:include, BarMethodAliaser) + FooClassWithBarMethod.send(:private, :duck) + + FooClassWithBarMethod.alias_method_chain :duck, :orange + + assert_raises NoMethodError do + @instance.duck + end + + assert_equal 'duck_with_orange', @instance.send(:duck) + assert FooClassWithBarMethod.private_method_defined?(:duck) + end + + def test_alias_method_chain_preserves_protected_method_status + FooClassWithBarMethod.send(:define_method, 'duck', Proc.new { 'duck' }) + FooClassWithBarMethod.send(:include, BarMethodAliaser) + FooClassWithBarMethod.send(:protected, :duck) + + FooClassWithBarMethod.alias_method_chain :duck, :orange + + assert_raises NoMethodError do + @instance.duck + end + + assert_equal 'duck_with_orange', @instance.send(:duck) + assert FooClassWithBarMethod.protected_method_defined?(:duck) + end + + def test_alias_method_chain_preserves_public_method_status + FooClassWithBarMethod.send(:define_method, 'duck', Proc.new { 'duck' }) + FooClassWithBarMethod.send(:include, BarMethodAliaser) + FooClassWithBarMethod.send(:public, :duck) + + FooClassWithBarMethod.alias_method_chain :duck, :orange + + assert_equal 'duck_with_orange', @instance.duck + assert FooClassWithBarMethod.public_method_defined?(:duck) + end end |