Skip to content
Browse files

safe_constantize should handle wrong constant name NameErrors Fixes #…

…4710

Signed-off-by: José Valim <jose.valim@gmail.com>
  • Loading branch information...
1 parent 70cbb1c commit 6c367a0d787705746f262d0bd5ad8c4f13a8c809 @atambo atambo committed with josevalim
View
2 activesupport/lib/active_support/inflector/methods.rb
@@ -258,7 +258,7 @@ def safe_constantize(camel_cased_word)
begin
constantize(camel_cased_word)
rescue NameError => e
- raise unless e.message =~ /uninitialized constant #{const_regexp(camel_cased_word)}$/ ||
+ raise unless e.message =~ /(uninitialized constant|wrong constant name) #{const_regexp(camel_cased_word)}$/ ||
e.name.to_s == camel_cased_word.to_s
rescue ArgumentError => e
raise unless e.message =~ /not missing constant #{const_regexp(camel_cased_word)}\!$/
View
5 activesupport/test/constantize_test_cases.rb
@@ -19,7 +19,7 @@ def run_constantize_tests_on
assert_raise(NameError) { yield("Ace::ConstantizeTestCases") }
assert_raise(NameError) { yield("Ace::Base::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") }
@@ -33,5 +33,6 @@ 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("#<Class:0x7b8b718b>::Nested_1") }
end
-end
+end

0 comments on commit 6c367a0

Please sign in to comment.
Something went wrong with that request. Please try again.