From 9bff40d3438af67e6c84f0e2859af5bee9d4e3a8 Mon Sep 17 00:00:00 2001 From: chenzww Date: Sat, 10 Jun 2017 16:07:43 +0800 Subject: [PATCH] Remove unnecessary key check when pass a document parameter. --- lib/mongoid/criteria/queryable/pipeline.rb | 10 ++++------ .../criteria/queryable/pipeline_spec.rb | 18 ++++++++---------- 2 files changed, 12 insertions(+), 16 deletions(-) diff --git a/lib/mongoid/criteria/queryable/pipeline.rb b/lib/mongoid/criteria/queryable/pipeline.rb index b33e001eb8..8e57846749 100644 --- a/lib/mongoid/criteria/queryable/pipeline.rb +++ b/lib/mongoid/criteria/queryable/pipeline.rb @@ -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 diff --git a/spec/mongoid/criteria/queryable/pipeline_spec.rb b/spec/mongoid/criteria/queryable/pipeline_spec.rb index c1fdb33262..fdcd6b72a9 100644 --- a/spec/mongoid/criteria/queryable/pipeline_spec.rb +++ b/spec/mongoid/criteria/queryable/pipeline_spec.rb @@ -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