Skip to content
This repository
Browse code

Fixes parsing deep nested resources from XML. [#380 state:resolved]

  • Loading branch information...
commit 225065709c43dacd57e0904aef2075024ccf2744 1 parent 16a9787
authored June 09, 2008 jeremy committed June 09, 2008
6  activeresource/lib/active_resource/base.rb
@@ -988,7 +988,11 @@ def find_or_create_resource_for(name)
988 988
           self.class.const_get(resource_name)
989 989
         end
990 990
       rescue NameError
991  
-        resource = self.class.const_set(resource_name, Class.new(ActiveResource::Base))
  991
+        if self.class.const_defined?(resource_name)
  992
+          resource = self.class.const_get(resource_name)
  993
+        else
  994
+          resource = self.class.const_set(resource_name, Class.new(ActiveResource::Base))
  995
+        end
992 996
         resource.prefix = self.class.prefix
993 997
         resource.site   = self.class.site
994 998
         resource
48  activeresource/test/base_test.rb
... ...
@@ -1,5 +1,6 @@
1 1
 require 'abstract_unit'
2 2
 require "fixtures/person"
  3
+require "fixtures/customer"
3 4
 require "fixtures/street_address"
4 5
 require "fixtures/beast"
5 6
 
@@ -15,6 +16,37 @@ def setup
15 16
     @people_david = [{ :id => 2, :name => 'David' }].to_xml(:root => 'people')
16 17
     @addresses = [{ :id => 1, :street => '12345 Street' }].to_xml(:root => 'addresses')
17 18
 
  19
+    # - deep nested resource -
  20
+    # - Luis (Customer)
  21
+    #   - JK (Customer::Friend)
  22
+    #     - Mateo (Customer::Friend::Brother)
  23
+    #       - Edith (Customer::Friend::Brother::Child)
  24
+    #       - Martha (Customer::Friend::Brother::Child)
  25
+    #     - Felipe (Customer::Friend::Brother)
  26
+    #       - Bryan (Customer::Friend::Brother::Child)
  27
+    #       - Luke (Customer::Friend::Brother::Child)
  28
+    #   - Eduardo (Customer::Friend)
  29
+    #     - Sebas (Customer::Friend::Brother)
  30
+    #       - Andres (Customer::Friend::Brother::Child)
  31
+    #       - Jorge (Customer::Friend::Brother::Child)
  32
+    #     - Elsa (Customer::Friend::Brother)
  33
+    #       - Natacha (Customer::Friend::Brother::Child)
  34
+    #     - Milena (Customer::Friend::Brother)
  35
+    #
  36
+    @luis = {:id => 1, :name => 'Luis',
  37
+              :friends => [{:name => 'JK',
  38
+                            :brothers => [{:name => 'Mateo',
  39
+                                           :children => [{:name => 'Edith'},{:name => 'Martha'}]},
  40
+                                          {:name => 'Felipe',
  41
+                                           :children => [{:name => 'Bryan'},{:name => 'Luke'}]}]},
  42
+                           {:name => 'Eduardo',
  43
+                            :brothers => [{:name => 'Sebas',
  44
+                                           :children => [{:name => 'Andres'},{:name => 'Jorge'}]},
  45
+                                          {:name => 'Elsa',
  46
+                                           :children => [{:name => 'Natacha'}]},
  47
+                                          {:name => 'Milena',
  48
+                                           :children => []}]}]}.to_xml(:root => 'customer')
  49
+
18 50
     ActiveResource::HttpMock.respond_to do |mock|
19 51
       mock.get    "/people/1.xml",                {}, @matz
20 52
       mock.get    "/people/2.xml",                {}, @david
@@ -46,6 +78,8 @@ def setup
46 78
       mock.head   "/people/1/addresses/2.xml",    {}, nil, 404
47 79
       mock.head   "/people/2/addresses/1.xml",    {}, nil, 404
48 80
       mock.head   "/people/Greg/addresses/1.xml", {}, nil, 200
  81
+      # customer
  82
+      mock.get    "/customers/1.xml",             {}, @luis
49 83
     end
50 84
 
51 85
     Person.user = nil
@@ -788,4 +822,18 @@ def test_to_param_quacks_like_active_record
788 822
     matz = Person.find(1)
789 823
     assert_equal '1', matz.to_param
790 824
   end
  825
+
  826
+  def test_parse_deep_nested_resources
  827
+    luis = Customer.find(1)
  828
+    assert_kind_of Customer, luis
  829
+    luis.friends.each do |friend|
  830
+      assert_kind_of Customer::Friend, friend
  831
+      friend.brothers.each do |brother|
  832
+        assert_kind_of Customer::Friend::Brother, brother
  833
+        brother.children.each do |child|
  834
+          assert_kind_of Customer::Friend::Brother::Child, child
  835
+        end
  836
+      end
  837
+    end
  838
+  end
791 839
 end
3  activeresource/test/fixtures/customer.rb
... ...
@@ -0,0 +1,3 @@
  1
+class Customer < ActiveResource::Base
  2
+  self.site = "http://37s.sunrise.i:3000"
  3
+end

0 notes on commit 2250657

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