Permalink
Browse files

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
  • Loading branch information...
1 parent 4e04ec4 commit 9a97f9a14bb7cce56bceccca8755d42598be2290 @jeremy jeremy committed Jan 2, 2008
Showing with 54 additions and 8 deletions.
  1. +2 −0 activeresource/CHANGELOG
  2. +17 −8 activeresource/lib/active_resource/base.rb
  3. +35 −0 activeresource/test/base/load_test.rb
View
@@ -1,5 +1,7 @@
*SVN*
+* find_or_create_resource_for handles module nesting. #10646 [xavier]
+
* Allow setting ActiveResource::Base#format before #site. [rick]
* Support agnostic formats when calling custom methods. Closes #10635 [joerichsen]
@@ -845,17 +845,26 @@ def find_or_create_resource_for_collection(name)
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
@@ -10,6 +10,29 @@ class Note < ActiveResource::Base
class Comment < ActiveResource::Base
self.site = "http://37s.sunrise.i:3000"
end
+
+ module Deeply
+ module Nested
+
+ class Note < ActiveResource::Base
+ self.site = "http://37s.sunrise.i:3000"
+ end
+
+ class Comment < ActiveResource::Base
+ self.site = "http://37s.sunrise.i:3000"
+ end
+
+ module TestDifferentLevels
+
+ class Note < ActiveResource::Base
+ self.site = "http://37s.sunrise.i:3000"
+ end
+
+ end
+
+ end
+ end
+
end
@@ -108,4 +131,16 @@ def test_nested_collections_within_the_same_namespace
n = Highrise::Note.new(:comments => [{ :name => "1" }])
assert_kind_of Highrise::Comment, n.comments.first
end
+
+ def test_nested_collections_within_deeply_nested_namespace
+ n = Highrise::Deeply::Nested::Note.new(:comments => [{ :name => "1" }])
+ assert_kind_of Highrise::Deeply::Nested::Comment, n.comments.first
+ end
+
+ def test_nested_collections_in_different_levels_of_namespaces
+ n = Highrise::Deeply::Nested::TestDifferentLevels::Note.new(:comments => [{ :name => "1" }])
+ assert_kind_of Highrise::Deeply::Nested::Comment, n.comments.first
+ end
+
+
end

0 comments on commit 9a97f9a

Please sign in to comment.