aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport/lib/active_support/inflector/methods.rb
diff options
context:
space:
mode:
authorAndrew White <andyw@pixeltrix.co.uk>2012-05-19 15:44:19 +0100
committerAndrew White <andyw@pixeltrix.co.uk>2012-05-19 15:47:35 +0100
commit3d0e4895cdc82bee9fb6ccc1208baa93fc5101ef (patch)
tree4a4ebb849e59f3f4cac2e417248e4ac43051a38b /activesupport/lib/active_support/inflector/methods.rb
parenteb09411460d11c7b1b1b54272ba345a1e1cf472d (diff)
downloadrails-3d0e4895cdc82bee9fb6ccc1208baa93fc5101ef.tar.gz
rails-3d0e4895cdc82bee9fb6ccc1208baa93fc5101ef.tar.bz2
rails-3d0e4895cdc82bee9fb6ccc1208baa93fc5101ef.zip
Handle case where ancestor is not the end of the chain
Diffstat (limited to 'activesupport/lib/active_support/inflector/methods.rb')
-rw-r--r--activesupport/lib/active_support/inflector/methods.rb19
1 files changed, 12 insertions, 7 deletions
diff --git a/activesupport/lib/active_support/inflector/methods.rb b/activesupport/lib/active_support/inflector/methods.rb
index 11f3c4c46f..e5b0cf4f9e 100644
--- a/activesupport/lib/active_support/inflector/methods.rb
+++ b/activesupport/lib/active_support/inflector/methods.rb
@@ -198,21 +198,26 @@ module ActiveSupport
#
# 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