Skip to content

Commit

Permalink
Remove unnecessary key check when pass a document parameter.
Browse files Browse the repository at this point in the history
  • Loading branch information
chenzww committed Jun 10, 2017
1 parent c94fc1a commit 9bff40d
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 16 deletions.
10 changes: 4 additions & 6 deletions lib/mongoid/criteria/queryable/pipeline.rb
Original file line number Diff line number Diff line change
Expand Up @@ -82,15 +82,13 @@ def project(entry)
# @return [ Pipeline ] The pipeline.
#
# @since 2.0.0
def unwind(field)
unless field.respond_to? :keys
normalized = field.to_s
def unwind(field_or_doc)
unless field_or_doc.respond_to? :keys
normalized = field_or_doc.to_s
name = aliases[normalized] || normalized
push("$unwind" => name.__mongo_expression__)
else
permit_keys = ['path', 'includeArrayIndex', 'preserveNullAndEmptyArrays'].freeze
normalized = BSON::Document.new(field.select { |k| permit_keys.include?(k.to_s) })
push("$unwind" => normalized)
push("$unwind" => field_or_doc)
end
end

Expand Down
18 changes: 8 additions & 10 deletions spec/mongoid/criteria/queryable/pipeline_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -197,17 +197,15 @@
end
end

context "when provided a hash value" do
context "unnecessary keys should be removed" do
before do
pipeline.unwind(path: "$foo", "includeArrayIndex" => "index", preserveNullAndEmptyArrays: true, foo: "bar")
end
context "when provided a hash" do
before do
pipeline.unwind(path: "$author", "includeArrayIndex" => "author_index", preserveNullAndEmptyArrays: true)
end

it "only reserve permit keys" do
expect(pipeline).to eq([
{ "$unwind" => { "path" => "$foo", "includeArrayIndex" => "index", "preserveNullAndEmptyArrays" => true } }
])
end
it "sets the hash" do
expect(pipeline).to eq([
{ "$unwind" => { path: "$author", "includeArrayIndex" => "author_index", preserveNullAndEmptyArrays: true } }
])
end
end
end
Expand Down

0 comments on commit 9bff40d

Please sign in to comment.