Skip to content

Commit

Permalink
Delegation method bug
Browse files Browse the repository at this point in the history
Add documentation and test to delegation method that make sure we're
aware that when a delegated object is not nil or false and doesn't
respond to the method it will still raise a NoMethodError exception.
  • Loading branch information
lellisga committed Apr 26, 2013
1 parent faa2c71 commit ccbe023
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
14 changes: 14 additions & 0 deletions activesupport/lib/active_support/core_ext/module/delegation.rb
Expand Up @@ -112,6 +112,20 @@ class Module
# end
#
# Foo.new.zoo # returns nil
#
# If the delegate object is not +nil+ or +false+ and the object doesn't
# respond to the delegated method it will raise an exception.
#
# class Foo
# def initialize(bar)
# @bar = bar
# end
#
# delegate :name, to: :@bar
# end
#
# Foo.new("Bar").name # raises NoMethodError: undefined method `name'
#
def delegate(*methods)
options = methods.pop
unless options.is_a?(Hash) && to = options[:to]
Expand Down
5 changes: 5 additions & 0 deletions activesupport/test/core_ext/module_test.rb
Expand Up @@ -171,6 +171,11 @@ def test_delegation_with_allow_nil_and_nil_value
assert_nil rails.name
end

def test_delegation_with_allow_nil_and_invalid_value
rails = Project.new("Rails", "David")
assert_raise(NoMethodError) { rails.name }
end

def test_delegation_with_allow_nil_and_nil_value_and_prefix
Project.class_eval do
delegate :name, :to => :person, :allow_nil => true, :prefix => true
Expand Down

0 comments on commit ccbe023

Please sign in to comment.