aboutsummaryrefslogtreecommitdiffstats
path: root/activeresource/lib
diff options
context:
space:
mode:
authorJeremy Kemper <jeremy@bitsweat.net>2008-01-02 09:14:34 +0000
committerJeremy Kemper <jeremy@bitsweat.net>2008-01-02 09:14:34 +0000
commit9a97f9a14bb7cce56bceccca8755d42598be2290 (patch)
tree45529a4adac2fabfbe6f43c47ec11ed39ada57d3 /activeresource/lib
parent4e04ec4adb4bae4e880a73a6f9ce1851d444b058 (diff)
downloadrails-9a97f9a14bb7cce56bceccca8755d42598be2290.tar.gz
rails-9a97f9a14bb7cce56bceccca8755d42598be2290.tar.bz2
rails-9a97f9a14bb7cce56bceccca8755d42598be2290.zip
find_or_create_resource_for handles module nesting. Closes #10646.
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@8525 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'activeresource/lib')
-rw-r--r--activeresource/lib/active_resource/base.rb25
1 files changed, 17 insertions, 8 deletions
diff --git a/activeresource/lib/active_resource/base.rb b/activeresource/lib/active_resource/base.rb
index 76f4ca03db..25776265e9 100644
--- a/activeresource/lib/active_resource/base.rb
+++ b/activeresource/lib/active_resource/base.rb
@@ -845,17 +845,26 @@ module ActiveResource
find_or_create_resource_for(name.to_s.singularize)
end
+ # Tries to find a resource in a non empty list of nested modules
+ # Raises a NameError if it was not found in any of the given nested modules
+ def find_resource_in_modules(resource_name, module_names)
+ receiver = Object
+ namespaces = module_names[0, module_names.size-1].map do |module_name|
+ receiver = receiver.const_get(module_name)
+ end
+ if namespace = namespaces.reverse.detect { |ns| ns.const_defined?(resource_name) }
+ return namespace.const_get(resource_name)
+ else
+ raise NameError
+ end
+ end
+
# Tries to find a resource for a given name; if it fails, then the resource is created
def find_or_create_resource_for(name)
resource_name = name.to_s.camelize
-
- # FIXME: Make it generic enough to support any depth of module nesting
- if (ancestors = self.class.name.split("::")).size > 1
- begin
- ancestors.first.constantize.const_get(resource_name)
- rescue NameError
- self.class.const_get(resource_name)
- end
+ ancestors = self.class.name.split("::")
+ if ancestors.size > 1
+ find_resource_in_modules(resource_name, ancestors)
else
self.class.const_get(resource_name)
end