Skip to content

Commit

Permalink
Allow passing update options for set and unset modifiers as documente…
Browse files Browse the repository at this point in the history
…d in #416
  • Loading branch information
brianhempel committed May 23, 2012
1 parent b965105 commit e582032
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 6 deletions.
14 changes: 8 additions & 6 deletions lib/mongo_mapper/plugins/modifiers.rb
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
54 changes: 54 additions & 0 deletions test/functional/test_modifiers.rb
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit e582032

Please sign in to comment.