Skip to content

Commit c4adc40

Browse files
ebeigartsdurran
authored andcommitted
In#matches? and Nin#matches? should work with Array fields. Fixes #889.
1 parent 8046299 commit c4adc40

File tree

3 files changed

+52
-2
lines changed

3 files changed

+52
-2
lines changed

lib/mongoid/matchers/in.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class In < Default
1414
#
1515
# @return [ true, false ] If a value exists.
1616
def matches?(value)
17-
value.values.first.include?(@attribute)
17+
Array.wrap(@attribute).any? { |e| value.values.first.include?(e) }
1818
end
1919
end
2020
end

lib/mongoid/matchers/nin.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class Nin < Default
1414
#
1515
# @return [ true, false ] If a value exists.
1616
def matches?(value)
17-
!value.values.first.include?(@attribute)
17+
Array.wrap(@attribute).none? { |e| value.values.first.include?(e) }
1818
end
1919
end
2020
end

spec/unit/mongoid/matchers_spec.rb

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,31 @@
208208
end
209209
end
210210

211+
context "with an $in selector on Array" do
212+
213+
context "when the attributes match" do
214+
215+
let(:selector) do
216+
{ :services => { "$in" => [ "first" ] } }
217+
end
218+
219+
it "returns true" do
220+
document.matches?(selector).should be_true
221+
end
222+
end
223+
224+
context "when the attributes do not match" do
225+
226+
let(:selector) do
227+
{ :number => { "$in" => [ "none" ] } }
228+
end
229+
230+
it "returns false" do
231+
document.matches?(selector).should be_false
232+
end
233+
end
234+
end
235+
211236
context "with an $in selector" do
212237

213238
context "when the attributes match" do
@@ -308,6 +333,31 @@
308333
end
309334
end
310335

336+
context "with a $nin selector on Array" do
337+
338+
context "when the attributes match" do
339+
340+
let(:selector) do
341+
{ :services => { "$nin" => [ "none" ] } }
342+
end
343+
344+
it "returns true" do
345+
document.matches?(selector).should be_true
346+
end
347+
end
348+
349+
context "when the attributes do not match" do
350+
351+
let(:selector) do
352+
{ :services => { "$nin" => [ "first" ] } }
353+
end
354+
355+
it "returns false" do
356+
document.matches?(selector).should be_false
357+
end
358+
end
359+
end
360+
311361
context "with a $nin selector" do
312362

313363
context "when the attributes match" do

0 commit comments

Comments
 (0)