Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Handle case where ancestor is not the end of the chain

  • Loading branch information...
commit 3d0e4895cdc82bee9fb6ccc1208baa93fc5101ef 1 parent eb09411
@pixeltrix pixeltrix authored
View
19 activesupport/lib/active_support/inflector/methods.rb
@@ -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
View
2  activesupport/test/constantize_test_cases.rb
@@ -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") }
@@ -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") }
Please sign in to comment.
Something went wrong with that request. Please try again.