diff options
-rw-r--r-- | activesupport/CHANGELOG | 2 | ||||
-rw-r--r-- | activesupport/lib/active_support/inflector.rb | 4 | ||||
-rw-r--r-- | activesupport/test/inflector_test.rb | 2 |
3 files changed, 6 insertions, 2 deletions
diff --git a/activesupport/CHANGELOG b/activesupport/CHANGELOG index 81d59d512a..6d3fd44368 100644 --- a/activesupport/CHANGELOG +++ b/activesupport/CHANGELOG @@ -1,5 +1,7 @@ *SVN* +* Fix constantize to properly handle names beginning with '::'. [Nicholas Seckar] + * Make String#last return the string instead of nil when it is shorter than the limit [Scott Barron]. * Added delegation support to Module that allows multiple delegations at once (unlike Forwardable in the stdlib) [DHH]. Example: diff --git a/activesupport/lib/active_support/inflector.rb b/activesupport/lib/active_support/inflector.rb index 2e69b74c54..f91a73c7f3 100644 --- a/activesupport/lib/active_support/inflector.rb +++ b/activesupport/lib/active_support/inflector.rb @@ -143,9 +143,9 @@ module Inflector def constantize(camel_cased_word) raise NameError, "#{camel_cased_word.inspect} is not a valid constant name!" unless - camel_cased_word.split("::").all? { |part| /^[A-Z]\w*$/ =~ part } + /^(::)?([A-Z]\w*)(::[A-Z]\w*)*$/ =~ camel_cased_word - camel_cased_word = "::#{camel_cased_word}" unless camel_cased_word[0, 2] == '::' + camel_cased_word = "::#{camel_cased_word}" unless $1 Object.module_eval(camel_cased_word, __FILE__, __LINE__) end diff --git a/activesupport/test/inflector_test.rb b/activesupport/test/inflector_test.rb index d0d7b0bedd..c513abc967 100644 --- a/activesupport/test/inflector_test.rb +++ b/activesupport/test/inflector_test.rb @@ -274,7 +274,9 @@ class InflectorTest < Test::Unit::TestCase def test_constantize assert_equal Ace::Base::Case, Inflector.constantize("Ace::Base::Case") + assert_equal Ace::Base::Case, Inflector.constantize("::Ace::Base::Case") assert_equal InflectorTest, Inflector.constantize("InflectorTest") + assert_equal InflectorTest, Inflector.constantize("::InflectorTest") assert_raises(NameError) { Inflector.constantize("UnknownClass") } assert_raises(NameError) { Inflector.constantize("An invalid string") } end |