Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

associations work better now

  • Loading branch information...
commit 8c76d36157561c19514895e5f2a914fb58e5c0f7 1 parent a360215
Christian Meier authored
31 lib/datamapper4rails/adapters/restful_adapter.rb
View
@@ -129,12 +129,16 @@ def parse_resource(xml, model, query = nil)
#puts "elements"
#p elements
#p query
-#p model
- resource = model.load(model.properties.collect do |f|
+#p model.properties
+ props = model.properties.length == 0 ? query.fields : model.properties
+#p props
+ resource = model.load(props.collect do |f| #model.properties.collect do |f|
elements[f.name]
end, query)
+#p resource
+#p query
resource.send("#{keys_from_query(query)[0].name}=".to_sym, elements[keys_from_query(query)[0].name] )
-# p resource
+#p resource
#p associations
associations.each do |name, association|
# puts "asso"
@@ -167,7 +171,7 @@ def parse_resource(xml, model, query = nil)
parse_resource(association, asso_model,
::DataMapper::Query.new(query.repository, asso_model ))) unless asso_model.nil?
else
- resource.send(("#{name.to_s.pluralize}<" + "<").to_sym,
+ resource.send("#{name.to_s.pluralize}".to_sym).send(:<<,
parse_resource(association, asso_model,
::DataMapper::Query.new(query.repository, asso_model ))) unless asso_model.nil?
end
@@ -178,12 +182,22 @@ def parse_resource(xml, model, query = nil)
many_to_many.each do |name, many|
if model.relationships[name]
# TODO
+ puts "TODO"
else
# p ::Extlib::Inflection.classify(name.to_s.singularize)
many_model = Object.const_get(::Extlib::Inflection.classify(name.to_s.singularize))
- resource.send(name).send(("<" + "<").to_sym,
- parse_resource(many, many_model,
- ::DataMapper::Query.new(query.repository, many_model ))) unless many_model.nil?
+#p resource
+#p many_model
+#puts "many"
+#p(parse_resource(many, many_model,
+# ::DataMapper::Query.new(query.repository, many_model ))) unless many_model.nil?
+ set = resource.send(name)
+ # p set
+ many.elements.each do |element|
+ set << parse_resource(element, many_model,
+ ::DataMapper::Query.new(query.repository, many_model ))
+ end unless many_model.nil?
+ # p set
end
end
resource.instance_variable_set(:@new_record, false)
@@ -197,6 +211,7 @@ def create_resource(resource)
uri = "/#{name.pluralize}.xml"
logger.debug { "post #{uri}" }
response = http_post(uri, resource.to_xml )
+ logger.debug { response.body.to_s }
resource_new = parse_resource(REXML::Document::new(response.body).root,
resource.model,
::DataMapper::Query.new(resource.repository,
@@ -211,7 +226,7 @@ def create_resource(resource)
resource_new.send(:relationships).each do |key, value|
resource.send("#{key}=".to_sym, resource_new.send(key))
end
- resource
+ resource_new
end
# @see BaseAdapter
27 spec/restful_adapter_spec.rb
View
@@ -9,7 +9,7 @@ class Item
property :id, Serial
property :name, String
- belongs_to :container
+ belongs_to :group
end
class User
include DataMapper::Resource
@@ -25,6 +25,7 @@ class Group
property :id, Serial
has n, :users, :through => Resource
+ has n, :items
end
class Container
@@ -33,7 +34,10 @@ class Container
property :id, Serial
has 1, :user
- has n, :items
+
+ def items
+ @items ||= []
+ end
end
def mock_attribute(name)
@@ -164,15 +168,28 @@ def res.kind_of?(clazz)
#describe 'associations of ' + DataMapper::Adapters::RestfulAdapter.to_s do
it 'should read nested resource (belongs_to)' do
@adapter.body = "<item><id>123</id><name>zappa</name>" +
- "<container><id>342</id>" + #<items tpye='array'><item><id>1234</id><name>frank zappa</name></item></items>" +
- "</container>" +
+ "<group><id>342</id>" + #<items tpye='array'><item><id>1234</id><name>frank zappa</name></item></items>" +
+ "</group>" +
"</item>"
query = DataMapper::Query.new(Item.new.repository, Item)
item = @adapter.read_resource(query)
item.id.should == 123
item.name.should == 'zappa'
- item.container.id.should == 342
+ item.group.id.should == 342
+ end
+
+ it 'should read nested resource manual "has n"' do
+ @adapter.body = "<container><id>342</id><items type='array'>" +
+ "<item><id>543</id><name>hmm</name></item>" +
+ "</items></container>"
+
+ query = DataMapper::Query.new(Container.new.repository, Container)
+ c = @adapter.read_resource(query)
+ c.id.should == 342
+ c.items.size.should == 1
+ c.items[0].id.should == 543
+ c.items[0].name.should == 'hmm'
end
# it 'should read nested resource (has 1)' do
Please sign in to comment.
Something went wrong with that request. Please try again.