Skip to content

Commit

Permalink
Merge pull request mongodb#4429 from chenzww/add_document_parameter_s…
Browse files Browse the repository at this point in the history
…upport_to_unwind

Add pass document parameter to unwind method support since new specif…
  • Loading branch information
estolfo authored Jun 15, 2017
2 parents 1223a68 + dc47d13 commit a26341e
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 5 deletions.
15 changes: 10 additions & 5 deletions lib/mongoid/criteria/queryable/pipeline.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,16 +72,21 @@ def project(entry)
#
# @example Add the unwind.
# pipeline.unwind(:field)
# pipeline.unwind(document)
#
# @param [ String, Symbol ] field The name of the field.
# @param [ String, Symbol, Hash ] field_or_doc The name of the field or a document.
#
# @return [ Pipeline ] The pipeline.
#
# @since 2.0.0
def unwind(field)
normalized = field.to_s
name = aliases[normalized] || normalized
push("$unwind" => name.__mongo_expression__)
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
push("$unwind" => field_or_doc)
end
end

private
Expand Down
12 changes: 12 additions & 0 deletions spec/mongoid/criteria/queryable/pipeline_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -196,5 +196,17 @@
end
end
end

context "when provided a hash" do
before do
pipeline.unwind(path: "$author", "includeArrayIndex" => "author_index", preserveNullAndEmptyArrays: true)
end

it "sets the hash" do
expect(pipeline).to eq([
{ "$unwind" => { path: "$author", "includeArrayIndex" => "author_index", preserveNullAndEmptyArrays: true } }
])
end
end
end
end

0 comments on commit a26341e

Please sign in to comment.