lib/builder/mongo_helpers.rb in sinatra_resource-0.3.4 vs lib/builder/mongo_helpers.rb in sinatra_resource-0.3.5

- old
+ new

@@ -230,11 +230,11 @@ documents = if child_model.embeddable? children = parent.send(child_assoc) if params.empty? children else - children.all(make_conditions(params, child_model)) + select_by(children, make_conditions(params, child_model)) end else children = if params.empty? child_model.all else @@ -348,9 +348,40 @@ # a class that includes MongoMapper::Document # # @return [Hash] def sanitize(conditions, model) conditions # TODO: incomplete + end + + # Select +children+ that match +conditions+. + # + # This method is needed because MongoMapper does not have +all+ + # defined on the proxy for an embedded document many association. + # + # It does not currently support conditions such as the following: + # :value => { '$gte' => 3 } + # :value => { '$in' => [24, 36, 48] } + # + # @params [<MongoMapper::EmbeddedDocument>] children + # + # @params [Hash] conditions + # + # @return [<MongoMapper::EmbeddedDocument>] + def select_by(children, conditions) + children.select do |child| + match = true + conditions.each_pair do |key, value| + match &&= case value + when String, Integer + child[key] == value + when Regexp + child[key] =~ value + else raise Error, "embedded document search does not " + + "support: #{value.inspect}" + end + end + match + end end # Select only the +children+ that are related to the +parent+ by # way of the +association+. #