From 17114235ce08705c58b6b6b407279e17aa5e254b Mon Sep 17 00:00:00 2001 From: Frederick Cheung Date: Sun, 14 Dec 2008 10:07:06 +0000 Subject: [PATCH] Make constantize look into ancestors --- activesupport/lib/active_support/inflector.rb | 7 ++++--- activesupport/test/inflector_test.rb | 15 +++++++++++++++ 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/activesupport/lib/active_support/inflector.rb b/activesupport/lib/active_support/inflector.rb index 4921b99..6f04107 100644 --- a/activesupport/lib/active_support/inflector.rb +++ b/activesupport/lib/active_support/inflector.rb @@ -353,10 +353,10 @@ module ActiveSupport def constantize(camel_cased_word) names = camel_cased_word.split('::') names.shift if names.empty? || names.first.empty? - constant = Object names.each do |name| - constant = constant.const_defined?(name) ? constant.const_get(name) : constant.const_missing(name) + module_with_constant = constant.ancestors.detect {|mod| mod.const_defined?(name)} + constant = module_with_constant ? module_with_constant.const_get(name) : constant.const_missing(name) end constant end @@ -367,7 +367,8 @@ module ActiveSupport constant = Object names.each do |name| - constant = constant.const_get(name, false) || constant.const_missing(name) + module_with_constant = constant.ancestors.detect {|mod| mod.const_defined?(name, false)} + constant = module_with_constant ? module_with_constant.const_get(name, false) : constant.const_missing(name) end constant end diff --git a/activesupport/test/inflector_test.rb b/activesupport/test/inflector_test.rb index d8c93dc..84f3a4e 100644 --- a/activesupport/test/inflector_test.rb +++ b/activesupport/test/inflector_test.rb @@ -2,8 +2,21 @@ require 'abstract_unit' require 'inflector_test_cases' module Ace + module Extension + def self.included(base) + base.extend(ClassMethods) + end + + module ClassMethods + def mission_accomplished? + false + end + end + end + module Base class Case + include Extension end end end @@ -168,6 +181,8 @@ class InflectorTest < Test::Unit::TestCase def test_constantize_does_lexical_lookup assert_raises(NameError) { ActiveSupport::Inflector.constantize("Ace::Base::InflectorTest") } + assert_nothing_raised { Ace::Base::Case::ClassMethods } + assert_nothing_raised { assert_equal Ace::Base::Case::ClassMethods, ActiveSupport::Inflector.constantize("Ace::Base::Case::ClassMethods") } end def test_ordinal -- 1.6.0.1