Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Allow passing update options for set and unset modifiers as documente…

…d in #416
  • Loading branch information...
commit e582032136d44e803eac7a8a27858a430c250724 1 parent b965105
@brianhempel brianhempel authored
View
14 lib/mongo_mapper/plugins/modifiers.rb
@@ -21,20 +21,22 @@ def set(*args)
updates.each do |key, value|
updates[key] = keys[key.to_s].set(value) if key?(key)
end
- collection.update(criteria, {'$set' => updates}, :multi => true)
+ modifier_update('$set', [criteria, updates, options])
end
def unset(*args)
if args[0].is_a?(Hash)
criteria, keys = args.shift, args
+ options = keys.last.is_a?(Hash) ? keys.pop : {}
else
keys, ids = args.partition { |arg| arg.is_a?(Symbol) }
+ options = ids.last.is_a?(Hash) ? ids.pop : {}
criteria = {:id => ids}
end
- criteria = criteria_hash(criteria).to_hash
- modifiers = keys.inject({}) { |hash, key| hash[key] = 1; hash }
- collection.update(criteria, {'$unset' => modifiers}, :multi => true)
+ criteria = criteria_hash(criteria).to_hash
+ updates = keys.inject({}) { |hash, key| hash[key] = 1; hash }
+ modifier_update('$unset', [criteria, updates, options])
end
def push(*args)
@@ -87,8 +89,8 @@ def criteria_and_keys_from_args(args)
end
end
- def unset(*keys)
- self.class.unset(id, *keys)
+ def unset(*args)
+ self.class.unset(id, *args)
end
def increment(hash, options=nil)
View
54 test/functional/test_modifiers.rb
@@ -43,6 +43,33 @@ def assert_keys_removed(page, *keys)
assert_keys_removed @page, :title, :tags
assert_keys_removed @page2, :title, :tags
end
+
+ context "additional options (upsert & safe)" do
+ should "be able to pass upsert option" do
+ new_key_value = DateTime.now.to_s
+ @page_class.unset({:title => new_key_value, :tags => %w(foo bar)}, :tags, {:upsert => true})
+ @page_class.count(:title => new_key_value).should == 1
+ @page_class.first(:title => new_key_value).tags.should == []
+ end
+
+ should "be able to pass safe option" do
+ @page_class.create(:title => "Better Be Safe than Sorry")
+
+ Mongo::Collection.any_instance.expects(:update).with(
+ {:title => "Better Be Safe than Sorry"},
+ {'$unset' => {:tags => 1}},
+ {:safe => true, :multi => true}
+ )
+ @page_class.unset({:title => "Better Be Safe than Sorry"}, :tags, {:safe => true})
+ end
+
+ should "be able to pass both safe and upsert options" do
+ new_key_value = DateTime.now.to_s
+ @page_class.unset({:title => new_key_value, :tags => %w(foo bar)}, :tags, {:upsert => true, :safe => true})
+ @page_class.count(:title => new_key_value).should == 1
+ @page_class.first(:title => new_key_value).tags.should == []
+ end
+ end
end
context "increment" do
@@ -141,6 +168,33 @@ def assert_keys_removed(page, *keys)
@page.reload
@page[:colors].should == %w[red green]
end
+
+ context "additional options (upsert & safe)" do
+ should "be able to pass upsert option" do
+ new_key_value = DateTime.now.to_s
+ @page_class.set({:title => new_key_value}, {:day_count => 1}, {:upsert => true})
+ @page_class.count(:title => new_key_value).should == 1
+ @page_class.first(:title => new_key_value).day_count.should == 1
+ end
+
+ should "be able to pass safe option" do
+ @page_class.create(:title => "Better Be Safe than Sorry")
+
+ Mongo::Collection.any_instance.expects(:update).with(
+ {:title => "Better Be Safe than Sorry"},
+ {'$set' => {:title => "I like safety."}},
+ {:safe => true, :multi => true}
+ )
+ @page_class.set({:title => "Better Be Safe than Sorry"}, {:title => "I like safety."}, {:safe => true})
+ end
+
+ should "be able to pass both safe and upsert options" do
+ new_key_value = DateTime.now.to_s
+ @page_class.set({:title => new_key_value}, {:day_count => 1}, {:upsert => true, :safe => true})
+ @page_class.count(:title => new_key_value).should == 1
+ @page_class.first(:title => new_key_value).day_count.should == 1
+ end
+ end
end
context "push" do
Please sign in to comment.
Something went wrong with that request. Please try again.