Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

change so embedded query is better

  • Loading branch information...
commit 1ef2b4166c4e232d27557a6ef5b0618a4b571840 1 parent 8a9ecfc
@codedmart authored
Showing with 43 additions and 3 deletions.
  1. +6 −2 lib/mongoid/matchers.rb
  2. +37 −1 spec/unit/mongoid/matchers_spec.rb
View
8 lib/mongoid/matchers.rb
@@ -18,8 +18,12 @@ module Matchers
# @return [ true, false ] True if matches, false if not.
def matches?(selector)
selector.each_pair do |key, value|
- unless Strategies.matcher(self, key, value).matches?(value)
- return false
+ if value.is_a?(Hash)
+ value.each do |item|
+ return false unless Strategies.matcher(self, key, Hash[*item]).matches?(Hash[*item])
+ end
+ else
+ return false unless Strategies.matcher(self, key, value).matches?(value)
end
end
return true
View
38 spec/unit/mongoid/matchers_spec.rb
@@ -4,6 +4,42 @@
describe "#matches?" do
+ context "when document is embeded" do
+
+ let(:document) do
+ Address.new(:street => "Clarkenwell Road")
+ end
+
+ before do
+ document.locations << Location.new(:name => 'No.1')
+ end
+
+ context "when the attributes do not match" do
+
+ let(:selector) do
+ { :name => { "$in" => ["No.2"], "$ne" => nil } }
+ end
+
+ it "returns false" do
+ document.locations.first.matches?(selector).should be_false
+ end
+
+ context "when just change the selector order" do
+
+ let(:selector) do
+ { :name => { "$ne" => nil, "$in" => ["No.2"] } }
+ end
+
+ it "returns false " do
+ document.locations.first.matches?(selector).should be_false
+ end
+
+ end
+
+ end
+
+ end
+
context "when performing simple matching" do
let(:document) do
@@ -315,4 +351,4 @@
end
end
end
-end
+end
Please sign in to comment.
Something went wrong with that request. Please try again.