Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Fixed updating

  • Loading branch information...
commit 56f1ee56ebb32cf8b881b43e7897ac8c188c867c 1 parent f17be98
Mark authored
Showing with 51 additions and 7 deletions.
  1. +5 −6 lib/mongodb_adapter.rb
  2. +46 −1 spec/mongodb_adapter_spec.rb
View
11 lib/mongodb_adapter.rb
@@ -54,7 +54,7 @@ def read(query)
(document['__discriminators'] || []).each do |key|
document[key] = eval(document[key])
end
- document['_id'] = document['_id'].to_s
+ #document['_id'] = document['_id'].to_s
result << document
end
result
@@ -66,9 +66,8 @@ def update(attributes,resources)
doc = resource.attributes(:field)
doc = stringify_bignums(doc)
doc = stringify_discriminators(doc)
- doc['_id'] = get_mongo_id(resource)
- doc[:_id] = doc['_id']
- collection.save(doc, :safe => true)
+ _id = get_mongo_id(resource)
+ collection.update({'_id' => _id}, doc)
end
resources.length
end
@@ -121,9 +120,9 @@ def get_mongo_id(resource)
end
else
serial = resource.model.serial(name).get(resource)
- id = Mongo::ObjectID.from_string(serial.to_s(16))
+ id = serial.to_s(16)
end
- id
+ id.to_s
end
def conditions_to_hash(conditions, hash = {}, negate = false)
View
47 spec/mongodb_adapter_spec.rb
@@ -12,11 +12,56 @@
:port => 27017,
:database => 'dm-mongodb-test'
)
+
+ ::Mongo::Connection.new.db('dm-mongodb-test')['heffalumps'].drop
- Mongo::Connection.new.db('dm-mongodb-test')['heffalumps'].remove
+ class ::Foo
+ include DataMapper::Resource
+ property :_id, Serial, :key => false
+ property :id, String, :key => true
+ property :text, String
+ property :things, Array, :default => []
+ end
end
+
+ before :each do
+ ::Mongo::Connection.new.db('dm-mongodb-test')['foos'].drop
+ end
it_should_behave_like 'An Adapter'
+
+ it "should insert Arrays on create" do
+ Foo.create(:id => "foo", :text => "Foo", :things => ["Zero", "One"])
+ things = Foo.get("foo").things
+ things[0].should == "Zero"
+ things[1].should == "One"
+ end
+
+ it "should insert Arrays on new and save" do
+ @foo = Foo.new(:id => "foo", :text => "Foo", :things => ["Zero", "One"])
+ @foo.save
+ things = Foo.get("foo").things
+ things[0].should == "Zero"
+ things[1].should == "One"
+ end
+
+ it "should update things using the foo.update(hash) method" do
+ Foo.create(:id => "foo", :text => "Foo")
+ @foo = Foo.get("foo")
+ hash = { :text => "Bar" }
+ @foo.update(hash)
+ Foo.get("foo").text.should == "Bar"
+ end
+
+ it "should update Arrays" do
+ @foo = Foo.create(:id => "foo", :text => "Foo", :things => ["Zero", "One"])
+ things = Array.new(@foo.things)
+ things << "Two"
+ hash = {:things => things}
+ @foo.update(hash)
+ things = Foo.get("foo").things
+ things[2].should == "Two"
+ end
end
Please sign in to comment.
Something went wrong with that request. Please try again.