1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Singleton classes returns parent's methods with instance_methods(false) and this makes remove_method in Module#delegate fail. Add a test case and fix the bug.

This commit is contained in:
José Valim 2010-03-19 12:01:48 +01:00
parent b395c81e3c
commit fbe35656a9
2 changed files with 17 additions and 1 deletions

View file

@ -1,3 +1,5 @@
require "active_support/core_ext/module/remove_method"
class Module
# Provides a delegate class method to easily expose contained objects' methods
# as your own. Pass one or more methods (specified as symbols or strings)
@ -121,7 +123,7 @@ class Module
module_eval(<<-EOS, file, line)
if instance_methods(false).map(&:to_s).include?("#{prefix}#{method}")
remove_method("#{prefix}#{method}")
remove_possible_method("#{prefix}#{method}")
end
def #{prefix}#{method}(*args, &block) # def customer_name(*args, &block)

View file

@ -141,6 +141,20 @@ class ModuleTest < Test::Unit::TestCase
assert_equal 0.0, nil_project.to_f
end
def test_delegation_does_not_raise_error_when_removing_singleton_instance_methods
parent = Class.new do
def self.parent_method; end
end
assert_nothing_raised do
child = Class.new(parent) do
class << self
delegate :parent_method, :to => :superclass
end
end
end
end
def test_parent
assert_equal Yz::Zy, Yz::Zy::Cd.parent
assert_equal Yz, Yz::Zy.parent