mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Ensure only delegations to methods can have an automatic prefix. [#1235 state:resolved]
Signed-off-by: Pratik Naik <pratiknaik@gmail.com>
This commit is contained in:
parent
206c5643aa
commit
a5156d9c2c
2 changed files with 15 additions and 0 deletions
|
@ -78,6 +78,10 @@ class Module
|
|||
raise ArgumentError, "Delegation needs a target. Supply an options hash with a :to key as the last argument (e.g. delegate :hello, :to => :greeter)."
|
||||
end
|
||||
|
||||
if options[:prefix] == true && options[:to].to_s =~ /^[^a-z_]/
|
||||
raise ArgumentError, "Can only automatically set the delegation prefix when delegating to a method."
|
||||
end
|
||||
|
||||
prefix = options[:prefix] && "#{options[:prefix] == true ? to : options[:prefix]}_"
|
||||
|
||||
methods.each do |method|
|
||||
|
|
|
@ -106,6 +106,17 @@ class ModuleTest < Test::Unit::TestCase
|
|||
assert_equal invoice.customer_city, "Chicago"
|
||||
end
|
||||
|
||||
def test_delegation_prefix_with_instance_variable
|
||||
assert_raise ArgumentError do
|
||||
Class.new do
|
||||
def initialize(client)
|
||||
@client = client
|
||||
end
|
||||
delegate :name, :address, :to => :@client, :prefix => true
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def test_parent
|
||||
assert_equal Yz::Zy, Yz::Zy::Cd.parent
|
||||
assert_equal Yz, Yz::Zy.parent
|
||||
|
|
Loading…
Reference in a new issue