From 155a0550629e09c3a96984ab6fe14e2d7931e629 Mon Sep 17 00:00:00 2001 From: Emily Date: Wed, 19 Oct 2016 17:24:36 +0200 Subject: [PATCH] MONGOID-4357 Fix merging selector with objects not responding to | --- lib/origin/selector.rb | 18 +++++++++++++++++- spec/origin/selector_spec.rb | 21 +++++++++++++++++++++ 2 files changed, 38 insertions(+), 1 deletion(-) diff --git a/lib/origin/selector.rb b/lib/origin/selector.rb index 0d663a3..8d0d9f5 100644 --- a/lib/origin/selector.rb +++ b/lib/origin/selector.rb @@ -19,7 +19,13 @@ def merge!(other) other.each_pair do |key, value| if value.is_a?(Hash) && self[key.to_s].is_a?(Hash) value = self[key.to_s].merge(value) do |_key, old_val, new_val| - _key == '$in' ? new_val & old_val : old_val | new_val + if in?(_key) + new_val & old_val + elsif nin?(_key) + (old_val + new_val).uniq + else + new_val + end end end if multi_selection?(key) @@ -188,5 +194,15 @@ def multi_selection?(key) def multi_value?(key) key =~ /\$nin|\$in/ end + + private + + def in?(key) + key =~ /\$in/ + end + + def nin?(key) + key =~ /\$nin/ + end end end diff --git a/spec/origin/selector_spec.rb b/spec/origin/selector_spec.rb index 1368b95..fc630a0 100644 --- a/spec/origin/selector_spec.rb +++ b/spec/origin/selector_spec.rb @@ -135,6 +135,27 @@ end end + context 'when an object does not support the | operator' do + + before do + selector['start'] = selection + selector.merge!(other) + end + + let(:selection) do + { '$lt' => Time.now } + end + + let(:other) do + { 'start' => selection, 'name' => 'test', } + end + + it "merges" do + expect(selector['name']).to eq('test') + expect(selector['start']).to eq(selection) + end + end + context "when the selector contains an $or" do let(:initial) do