lib/builder/mongo_helpers.rb in sinatra_resource-0.4.21 vs lib/builder/mongo_helpers.rb in sinatra_resource-0.4.22
- old
+ new
@@ -1,10 +1,9 @@
-gem 'query_string_filter', '>= 0.1.4'
require 'query_string_filter'
module SinatraResource
-
+
class Builder
module MongoHelpers
# Make sure that +parent+ document is related to the +child+ document
@@ -32,11 +31,11 @@
# @return [Integer]
def count_documents(model)
conditions = params.empty? ? {} : make_conditions(params, model)
model.count(conditions)
end
-
+
def count_nested_documents(parent, child_assoc, child_model)
#
# This code needs significant improvement.
# It is copy and pasted from find_nested_documents!
#
@@ -55,11 +54,11 @@
end
select_related(parent, child_assoc, children)
end
documents.length
end
-
+
# Create a document from params. If not valid, returns 400.
#
# @param [Class] model
# a class that includes MongoMapper::Document
#
@@ -103,11 +102,11 @@
error 400, convert(body_for(:internal_server_error))
end
end
child
end
-
+
# Delete a document with +id+.
#
# @param [Class] model
# a class that includes MongoMapper::Document
#
@@ -211,11 +210,11 @@
find_child(children, child_id)
else
child_model.find_by_id(child_id)
end
end
-
+
# Find a nested document. If not found, returns 404.
#
# @param [MongoMapper::Document] parent_document
#
# @param [Symbol] association
@@ -233,11 +232,11 @@
unless document
error 404, convert(body_for(:not_found))
end
document
end
-
+
# Find +model+ documents: find all documents if no params, otherwise
# find selected documents.
#
# @param [Class] model
# a class that includes MongoMapper::Document
@@ -284,11 +283,11 @@
child_model.all(make_conditions(params, child_model))
end
select_related(parent, child_assoc, children)
end
end
-
+
# Delegates to application, who should use custom logic to relate
# +parent+ and +child+.
#
# @param [MongoMapper::Document] parent
# a class that includes MongoMapper::Document
@@ -301,11 +300,11 @@
def make_related(parent, child, resource_config)
proc = resource_config[:relation][:create]
proc.call(parent, child) if proc
child
end
-
+
# Update a document with +id+ from params. If not valid, returns 400.
#
# @param [Class] model
# a class that includes MongoMapper::Document
#
@@ -317,11 +316,11 @@
unless document.valid?
error 400, convert(body_for(:invalid_document, document))
end
document
end
-
+
# Update a nested document with params. If not valid, returns 400.
#
# @param [MongoMapper::Document] parent
#
# @param [Symbol] child_assoc
@@ -348,15 +347,15 @@
child
else
update_document!(child_model, child_id)
end
end
-
+
protected
-
+
QS_FILTER = QueryStringFilter.new
-
+
# Build conditions hash based on +params+.
#
# @param [Hash] params
#
# @param [Class] model
@@ -380,10 +379,10 @@
end
else
{}
end
end
-
+
# Filter out +conditions+ that do not have corresponding keys in
# +model+. This is part of the process that prevents a user from
# searching for parameters that they do not have access to.
#
# TODO: in order for this model to do the job stated above, it will