Skip to content

Commit

Permalink
Change "orderby" to "sort" in map_reduce
Browse files Browse the repository at this point in the history
Change "orderby" to "sort" in map_reduce according to MongoDB MapReduce documentation
Add map_reduce specs for "sort" and "limit" options
  • Loading branch information
Alex Tsibulya committed Sep 19, 2012
1 parent 69afa1a commit 7b7a77f
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 7 deletions.
2 changes: 1 addition & 1 deletion lib/mongoid/contextual/map_reduce.rb
Expand Up @@ -251,7 +251,7 @@ def inspect
def apply_criteria_options
command[:query] = criteria.selector
if sort = criteria.options[:sort]
command[:orderby] = sort
command[:sort] = sort
end
if limit = criteria.options[:limit]
command[:limit] = limit
Expand Down
36 changes: 30 additions & 6 deletions spec/mongoid/contextual/map_reduce_spec.rb
Expand Up @@ -42,13 +42,37 @@
described_class.new(collection, criteria, map, reduce)
end

let(:base_command) do
{
mapreduce: "bands",
map: map,
reduce: reduce,
query: {}
}
end

it "returns the db command" do
map_reduce.command.should eq({
mapreduce: "bands",
map: map,
reduce: reduce,
query: {}
})
map_reduce.command.should eq(base_command)
end

context "with sort" do
let(:criteria) do
Band.order_by(name: -1)
end

it "returns the db command with a sort option" do
map_reduce.command.should eq(base_command.merge(sort: {'name' => -1}))
end
end

context "with limit" do
let(:criteria) do
Band.limit(10)
end

it "returns the db command with a limit option" do
map_reduce.command.should eq(base_command.merge(limit: 10))
end
end
end

Expand Down
5 changes: 5 additions & 0 deletions spec/mongoid/contextual/mongo_spec.rb
Expand Up @@ -969,6 +969,11 @@

context "when sorting is provided" do

before do
Band.index(name: -1)
Band.create_indexes
end

let(:criteria) do
Band.desc(:name)
end
Expand Down

0 comments on commit 7b7a77f

Please sign in to comment.