Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Make constantize look down the ancestor chain (excluding Object)

  • Loading branch information...
commit 99e9a733c8852f4ff065f2ede9c9cd03475411a2 1 parent 1551de3
@marcandre marcandre authored pixeltrix committed
View
14 activesupport/lib/active_support/inflector/methods.rb
@@ -203,7 +203,19 @@ def constantize(camel_cased_word) #:nodoc:
names.shift if names.empty? || names.first.empty?
names.inject(Object) do |constant, name|
- constant.const_get(name, false)
+ candidate = constant.const_get(name)
+ if constant.const_defined?(name, false) || !Object.const_defined?(name)
+ candidate
+ else
+ # 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)
+ end
+ # owner is in Object, so raise
+ constant.const_get(name, false)
+ end
end
end
View
17 activesupport/test/constantize_test_cases.rb
@@ -1,7 +1,14 @@
module Ace
module Base
class Case
+ class Dice
+ end
end
+ class Fase < Case
+ end
+ end
+ class Gas
+ include Base
end
end
@@ -9,6 +16,9 @@ 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 ConstantizeTestCases, yield("ConstantizeTestCases") }
assert_nothing_raised { assert_equal ConstantizeTestCases, yield("::ConstantizeTestCases") }
assert_raise(NameError) { yield("UnknownClass") }
@@ -18,11 +28,16 @@ def run_constantize_tests_on
assert_raise(NameError) { yield("InvalidClass\n") }
assert_raise(NameError) { yield("Ace::ConstantizeTestCases") }
assert_raise(NameError) { yield("Ace::Base::ConstantizeTestCases") }
+ assert_raise(NameError) { yield("Ace::Gas::Base") }
+ assert_raise(NameError) { yield("Ace::Gas::ConstantizeTestCases") }
end
def run_safe_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 ConstantizeTestCases, yield("ConstantizeTestCases") }
assert_nothing_raised { assert_equal ConstantizeTestCases, yield("::ConstantizeTestCases") }
assert_nothing_raised { assert_equal nil, yield("UnknownClass") }
@@ -33,6 +48,8 @@ def run_safe_constantize_tests_on
assert_nothing_raised { assert_equal nil, yield("blargle") }
assert_nothing_raised { assert_equal nil, yield("Ace::ConstantizeTestCases") }
assert_nothing_raised { assert_equal nil, yield("Ace::Base::ConstantizeTestCases") }
+ assert_nothing_raised { assert_equal nil, yield("Ace::Gas::Base") }
+ assert_nothing_raised { assert_equal nil, yield("Ace::Gas::ConstantizeTestCases") }
assert_nothing_raised { assert_equal nil, yield("#<Class:0x7b8b718b>::Nested_1") }
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.