Skip to content

Commit

Permalink
MONGOID-4357 Fix merging selector with objects not responding to |
Browse files Browse the repository at this point in the history
  • Loading branch information
estolfo committed Oct 19, 2016
1 parent e714caa commit 155a055
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
18 changes: 17 additions & 1 deletion lib/origin/selector.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
21 changes: 21 additions & 0 deletions spec/origin/selector_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 155a055

Please sign in to comment.