Skip to content

Commit

Permalink
Merge pull request mongomapper#459 from jnunemaker/support-string-ids…
Browse files Browse the repository at this point in the history
…-in-modifiers

Support non-ObjectID ids being given to modifiers.
  • Loading branch information
greatuserongithub committed Oct 2, 2012
2 parents 94e9825 + 73f30d9 commit e3521dc
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 6 deletions.
11 changes: 5 additions & 6 deletions lib/mongo_mapper/plugins/modifiers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,14 +77,13 @@ def modifier_update(modifier, args)
def criteria_and_keys_from_args(args)
if args[0].is_a?(Hash)
criteria = args[0]
updates = args[1]
options = args[2]
updates = args[1]
options = args[2]
else
split_args = args.partition{|a| a.is_a?(BSON::ObjectId)}
criteria = {:id => split_args[0]}
updates = split_args[1].first
options = split_args[1].last
criteria, (updates, options) = args.partition { |a| !a.is_a?(Hash) }
criteria = { :id => criteria }
end

[criteria_hash(criteria).to_hash, updates, options]
end
end
Expand Down
14 changes: 14 additions & 0 deletions test/functional/test_modifiers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,13 @@ def assert_keys_removed(page, *keys)
assert_page_counts @page, 1, 2, 3
assert_page_counts @page2, 1, 2, 3
end

should "work with ids given as strings" do
@page_class.increment(@page.id.to_s, @page2.id.to_s, :day_count => 1, :week_count => 2, :month_count => 3)

assert_page_counts @page, 1, 2, 3
assert_page_counts @page2, 1, 2, 3
end
end

context "decrement" do
Expand Down Expand Up @@ -137,6 +144,13 @@ def assert_keys_removed(page, *keys)
assert_page_counts @page, 0, 0, 0
assert_page_counts @page2, 0, 0, 0
end

should "work with ids given as strings" do
@page_class.decrement(@page.id.to_s, @page2.id.to_s, :day_count => -1, :week_count => 2, :month_count => -3)

assert_page_counts @page, 0, 0, 0
assert_page_counts @page2, 0, 0, 0
end
end

context "set" do
Expand Down

0 comments on commit e3521dc

Please sign in to comment.