Skip to content

Commit

Permalink
Fix constantize so top level constants are looked up properly.
Browse files Browse the repository at this point in the history
  • Loading branch information
marcandre authored and pixeltrix committed May 19, 2012
1 parent 99e9a73 commit eb09411
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
2 changes: 1 addition & 1 deletion activesupport/lib/active_support/inflector/methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ def constantize(camel_cased_word) #:nodoc:

names.inject(Object) do |constant, name|
candidate = constant.const_get(name)
if constant.const_defined?(name, false) || !Object.const_defined?(name)
if constant.const_defined?(name, false) || constant == Object || !Object.const_defined?(name)
candidate
else
# Go down the ancestors to check it it's owned
Expand Down
18 changes: 18 additions & 0 deletions activesupport/test/constantize_test_cases.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,29 @@ class Gas
end
end

class Object
module AddtlGlobalConstants
class Case
class Dice
end
end
end
include AddtlGlobalConstants
end

module ConstantizeTestCases
def run_constantize_tests_on
assert_nothing_raised { assert_equal Ace::Base::Case, yield("Ace::Base::Case") }
assert_nothing_raised { assert_equal Ace::Base::Case, yield("::Ace::Base::Case") }
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 Case::Dice, yield("Case::Dice") }
assert_nothing_raised { assert_equal Case::Dice, yield("Object::Case::Dice") }
assert_nothing_raised { assert_equal ConstantizeTestCases, yield("ConstantizeTestCases") }
assert_nothing_raised { assert_equal ConstantizeTestCases, yield("::ConstantizeTestCases") }
assert_nothing_raised { assert_equal Object, yield("") }
assert_nothing_raised { assert_equal Object, yield("::") }
assert_raise(NameError) { yield("UnknownClass") }
assert_raise(NameError) { yield("UnknownClass::Ace") }
assert_raise(NameError) { yield("UnknownClass::Ace::Base") }
Expand All @@ -38,8 +52,12 @@ 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 Case::Dice, yield("Case::Dice") }
assert_nothing_raised { assert_equal Case::Dice, yield("Object::Case::Dice") }
assert_nothing_raised { assert_equal ConstantizeTestCases, yield("ConstantizeTestCases") }
assert_nothing_raised { assert_equal ConstantizeTestCases, yield("::ConstantizeTestCases") }
assert_nothing_raised { assert_equal Object, yield("") }
assert_nothing_raised { assert_equal Object, yield("::") }
assert_nothing_raised { assert_equal nil, yield("UnknownClass") }
assert_nothing_raised { assert_equal nil, yield("UnknownClass::Ace") }
assert_nothing_raised { assert_equal nil, yield("UnknownClass::Ace::Base") }
Expand Down

0 comments on commit eb09411

Please sign in to comment.