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
This commit is contained in:
parent
a38f28fff1
commit
46f092097b
|
@ -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]
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue