Skip to content

Commit

Permalink
Handle case where ancestor is not the end of the chain
Browse files Browse the repository at this point in the history
  • Loading branch information
pixeltrix committed May 19, 2012
1 parent eb09411 commit 3d0e489
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 7 deletions.
19 changes: 12 additions & 7 deletions activesupport/lib/active_support/inflector/methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -198,21 +198,26 @@ def foreign_key(class_name, separate_class_name_and_id_with_underscore = true)
#
# NameError is raised when the name is not in CamelCase or the constant is
# unknown.
def constantize(camel_cased_word) #:nodoc:
def constantize(camel_cased_word)
names = camel_cased_word.split('::')
names.shift if names.empty? || names.first.empty?

names.inject(Object) do |constant, name|
candidate = constant.const_get(name)
if constant.const_defined?(name, false) || constant == Object || !Object.const_defined?(name)
candidate
if constant == Object
constant.const_get(name)
else
candidate = constant.const_get(name)
next candidate if constant.const_defined?(name, false)
next candidate unless Object.const_defined?(name)

# Go down the ancestors to check it it's owned
# directly before we reach Object or the end of ancestors.
constant.ancestors.each do |ancestor|
break if ancestor == Object
return candidate if ancestor.const_defined?(name, false)
constant = constant.ancestors.inject do |constant, ancestor|
break constant if ancestor == Object
break ancestor if ancestor.const_defined?(name, false)
constant
end

# owner is in Object, so raise
constant.const_get(name, false)
end
Expand Down
2 changes: 2 additions & 0 deletions activesupport/test/constantize_test_cases.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ def run_constantize_tests_on
assert_nothing_raised { assert_equal Ace::Base::Case::Dice, yield("Ace::Base::Case::Dice") }
assert_nothing_raised { assert_equal Ace::Base::Fase::Dice, yield("Ace::Base::Fase::Dice") }
assert_nothing_raised { assert_equal Ace::Gas::Case, yield("Ace::Gas::Case") }
assert_nothing_raised { assert_equal Ace::Gas::Case::Dice, yield("Ace::Gas::Case::Dice") }
assert_nothing_raised { assert_equal Case::Dice, yield("Case::Dice") }
assert_nothing_raised { assert_equal Case::Dice, yield("Object::Case::Dice") }
assert_nothing_raised { assert_equal ConstantizeTestCases, yield("ConstantizeTestCases") }
Expand All @@ -52,6 +53,7 @@ def run_safe_constantize_tests_on
assert_nothing_raised { assert_equal Ace::Base::Case::Dice, yield("Ace::Base::Case::Dice") }
assert_nothing_raised { assert_equal Ace::Base::Fase::Dice, yield("Ace::Base::Fase::Dice") }
assert_nothing_raised { assert_equal Ace::Gas::Case, yield("Ace::Gas::Case") }
assert_nothing_raised { assert_equal Ace::Gas::Case::Dice, yield("Ace::Gas::Case::Dice") }
assert_nothing_raised { assert_equal Case::Dice, yield("Case::Dice") }
assert_nothing_raised { assert_equal Case::Dice, yield("Object::Case::Dice") }
assert_nothing_raised { assert_equal ConstantizeTestCases, yield("ConstantizeTestCases") }
Expand Down

0 comments on commit 3d0e489

Please sign in to comment.