Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

associations work better now

  • Loading branch information...
commit 8c76d36157561c19514895e5f2a914fb58e5c0f7 1 parent a360215
Christian Meier authored
31  lib/datamapper4rails/adapters/restful_adapter.rb
@@ -129,12 +129,16 @@ def parse_resource(xml, model, query = nil)
129 129
 #puts "elements"
130 130
 #p elements
131 131
 #p query
132  
-#p model
133  
-        resource = model.load(model.properties.collect do |f|
  132
+#p model.properties
  133
+        props = model.properties.length == 0 ? query.fields : model.properties
  134
+#p props
  135
+        resource = model.load(props.collect do |f| #model.properties.collect do |f|
134 136
                                   elements[f.name]
135 137
                                 end, query)
  138
+#p resource
  139
+#p query
136 140
         resource.send("#{keys_from_query(query)[0].name}=".to_sym, elements[keys_from_query(query)[0].name] )
137  
-# p resource
  141
+#p resource
138 142
 #p associations
139 143
         associations.each do |name, association| 
140 144
 #          puts "asso"
@@ -167,7 +171,7 @@ def parse_resource(xml, model, query = nil)
167 171
                           parse_resource(association, asso_model,
168 172
                                          ::DataMapper::Query.new(query.repository, asso_model ))) unless asso_model.nil?
169 173
           else
170  
-            resource.send(("#{name.to_s.pluralize}<" + "<").to_sym, 
  174
+            resource.send("#{name.to_s.pluralize}".to_sym).send(:<<, 
171 175
                           parse_resource(association, asso_model,
172 176
                                          ::DataMapper::Query.new(query.repository, asso_model ))) unless asso_model.nil?
173 177
           end
@@ -178,12 +182,22 @@ def parse_resource(xml, model, query = nil)
178 182
         many_to_many.each do |name, many|
179 183
           if model.relationships[name]
180 184
             # TODO
  185
+            puts "TODO"
181 186
           else
182 187
  #           p ::Extlib::Inflection.classify(name.to_s.singularize)
183 188
             many_model = Object.const_get(::Extlib::Inflection.classify(name.to_s.singularize))
184  
-            resource.send(name).send(("<" + "<").to_sym, 
185  
-                       parse_resource(many, many_model,
186  
-                                      ::DataMapper::Query.new(query.repository, many_model ))) unless many_model.nil?
  189
+#p resource
  190
+#p many_model
  191
+#puts "many"
  192
+#p(parse_resource(many, many_model,
  193
+#                                      ::DataMapper::Query.new(query.repository, many_model ))) unless many_model.nil?
  194
+            set = resource.send(name)
  195
+ #           p set
  196
+            many.elements.each do |element|
  197
+              set << parse_resource(element, many_model,
  198
+                                    ::DataMapper::Query.new(query.repository, many_model ))
  199
+            end unless many_model.nil?
  200
+  #          p set
187 201
           end
188 202
         end
189 203
         resource.instance_variable_set(:@new_record, false)
@@ -197,6 +211,7 @@ def create_resource(resource)
197 211
         uri = "/#{name.pluralize}.xml"
198 212
         logger.debug { "post #{uri}" }
199 213
         response = http_post(uri, resource.to_xml )
  214
+        logger.debug { response.body.to_s }
200 215
         resource_new = parse_resource(REXML::Document::new(response.body).root, 
201 216
                                   resource.model,
202 217
                                   ::DataMapper::Query.new(resource.repository, 
@@ -211,7 +226,7 @@ def create_resource(resource)
211 226
         resource_new.send(:relationships).each do |key, value|
212 227
           resource.send("#{key}=".to_sym, resource_new.send(key))
213 228
         end
214  
-        resource
  229
+        resource_new
215 230
       end
216 231
 
217 232
       # @see BaseAdapter
27  spec/restful_adapter_spec.rb
@@ -9,7 +9,7 @@ class Item
9 9
   property :id, Serial
10 10
   property :name, String
11 11
 
12  
-  belongs_to :container
  12
+  belongs_to :group
13 13
 end
14 14
 class User
15 15
   include DataMapper::Resource
@@ -25,6 +25,7 @@ class Group
25 25
   property :id, Serial
26 26
 
27 27
   has n, :users, :through => Resource
  28
+  has n, :items
28 29
 end
29 30
 
30 31
 class Container
@@ -33,7 +34,10 @@ class Container
33 34
   property :id, Serial
34 35
 
35 36
   has 1, :user
36  
-  has n, :items
  37
+
  38
+  def items
  39
+    @items ||= []
  40
+  end
37 41
 end
38 42
 
39 43
 def mock_attribute(name)
@@ -164,15 +168,28 @@ def res.kind_of?(clazz)
164 168
 #describe 'associations of ' + DataMapper::Adapters::RestfulAdapter.to_s do
165 169
   it 'should read nested resource (belongs_to)' do
166 170
     @adapter.body = "<item><id>123</id><name>zappa</name>" +
167  
-      "<container><id>342</id>" + #<items tpye='array'><item><id>1234</id><name>frank zappa</name></item></items>" + 
168  
-      "</container>" +
  171
+      "<group><id>342</id>" + #<items tpye='array'><item><id>1234</id><name>frank zappa</name></item></items>" + 
  172
+      "</group>" +
169 173
       "</item>"
170 174
 
171 175
     query = DataMapper::Query.new(Item.new.repository, Item)
172 176
     item = @adapter.read_resource(query)
173 177
     item.id.should == 123
174 178
     item.name.should == 'zappa'
175  
-    item.container.id.should == 342
  179
+    item.group.id.should == 342
  180
+  end
  181
+
  182
+  it 'should read nested resource manual "has n"' do
  183
+    @adapter.body = "<container><id>342</id><items type='array'>" +
  184
+      "<item><id>543</id><name>hmm</name></item>" +
  185
+      "</items></container>"
  186
+
  187
+    query = DataMapper::Query.new(Container.new.repository, Container)
  188
+    c = @adapter.read_resource(query)
  189
+    c.id.should == 342
  190
+    c.items.size.should == 1
  191
+    c.items[0].id.should == 543
  192
+    c.items[0].name.should == 'hmm'
176 193
   end
177 194
 
178 195
 #   it 'should read nested resource (has 1)' do

0 notes on commit 8c76d36

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