Permalink
Browse files

Merge pull request #1934 from i0rek/expand

Expand nested complex criteria inside an array.
  • Loading branch information...
2 parents f8d87da + 8d5f2b2 commit c974b4fbfdd8ea2dcb43b0f14105a29ccb239d33 @durran durran committed Apr 20, 2012
@@ -16,12 +16,13 @@ module CriteriaHelpers
# @since 1.0.0
def expand_complex_criteria
{}.tap do |hsh|
- each_pair do |k,v|
+ each_pair do |k, v|
if k.respond_to?(:key) && k.respond_to?(:to_mongo_query)
hsh[k.key] ||= {}
v = v.expand_complex_criteria if v.is_a?(::Hash)
hsh[k.key].merge!(k.to_mongo_query(v))
else
+ v.map!{|e| e.is_a?(::Hash) ? e.expand_complex_criteria : e } if v.is_a?(::Array)
hsh[k] = v
end
end
@@ -18,5 +18,10 @@
hash = { "person.videos".to_sym.matches => { :year.gt => 2000 } }
hash.expand_complex_criteria.should eq({:"person.videos"=>{"$elemMatch"=>{:year=>{"$gt"=>2000}}}})
end
+
+ it "expands nested complex criteria in an array to form a valid `where` hash" do
+ hash = { "$or" => [{:year.gt => 2000}]}
+ hash.expand_complex_criteria.should eq({"$or" => [:year => {"$gt" => 2000}]})
+ end
end
end

0 comments on commit c974b4f

Please sign in to comment.