Module: SinatraResource::Builder::MongoHelpers
- Defined in:
- lib/builder/mongo_helpers.rb
Instance Method Summary collapse
-
#check_related?(parent, child_assoc, child_id) ⇒ MongoMapper::Document
Make sure that
parent
document is related to thechild
document by way ofassociation
. -
#count_documents(model) ⇒ Integer
Find
model
documents: find all documents if no params, otherwise find selected documents. - #count_nested_documents(parent, child_assoc, child_model) ⇒ Object
-
#create_document!(model) ⇒ MongoMapper::Document
Create a document from params.
-
#create_nested_document!(parent, child_assoc, child_model) ⇒ MongoMapper::Document
Create a nested document from params.
-
#delete_document!(model, id) ⇒ MongoMapper::Document
Delete a document with
id
. -
#delete_nested_document!(parent, child_assoc, child_model, child_id) ⇒ MongoMapper::Document
Delete a nested document.
-
#find_child(children, child_id) ⇒ MongoMapper::EmbeddedDocument
Find document with
child_id
inchildren
. -
#find_child!(children, child_id) ⇒ MongoMapper::EmbeddedDocument
Find document with
child_id
inchildren
or raise 404. -
#find_document!(model, id) ⇒ MongoMapper::Document
Find a document with
id
. -
#find_documents!(model, page, items_per_page) ⇒ Array<MongoMapper::Document>
Find
model
documents: find all documents if no params, otherwise find selected documents. -
#find_nested_document(parent, child_assoc, child_model, child_id) ⇒ MongoMapper::Document
Find a nested document.
-
#find_nested_document!(parent, child_assoc, child_model, child_id) ⇒ MongoMapper::Document
Find a nested document.
-
#find_nested_documents!(parent, child_assoc, child_model, page, items_per_page) ⇒ Array<MongoMapper::Document>
Find nested
child_model
documents: find all documents if no params, otherwise find selected documents. -
#make_related(parent, child, resource_config) ⇒ MongoMapper::Document
Delegates to application, who should use custom logic to relate
parent
andchild
. -
#update_document!(model, id) ⇒ MongoMapper::Document
Update a document with
id
from params. -
#update_nested_document!(parent, child_assoc, child_model, child_id) ⇒ MongoMapper::Document
Update a nested document with params.
Instance Method Details
#check_related?(parent, child_assoc, child_id) ⇒ MongoMapper::Document
Make sure that parent
document is related to the child
document by way of association
. If not, return 404 Not Found.
19 20 21 22 23 |
# File 'lib/builder/mongo_helpers.rb', line 19 def (parent, child_assoc, child_id) children = parent.send(child_assoc) find_child!(children, child_id) true end |
#count_documents(model) ⇒ Integer
Find model
documents: find all documents if no params, otherwise find selected documents.
32 33 34 35 |
# File 'lib/builder/mongo_helpers.rb', line 32 def count_documents(model) conditions = params.empty? ? {} : make_conditions(params, model) model.count(conditions) end |
#count_nested_documents(parent, child_assoc, child_model) ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/builder/mongo_helpers.rb', line 37 def count_nested_documents(parent, child_assoc, child_model) # # This code needs significant improvement. # It is copy and pasted from find_nested_documents! # documents = if child_model. children = parent.send(child_assoc) if params.empty? children else select_by(children, make_conditions(params, child_model)) end else children = if params.empty? child_model.all else child_model.all(make_conditions(params, child_model)) end (parent, child_assoc, children) end documents.length end |
#create_document!(model) ⇒ MongoMapper::Document
Create a document from params. If not valid, returns 400.
66 67 68 69 70 71 72 73 74 75 |
# File 'lib/builder/mongo_helpers.rb', line 66 def create_document!(model) document = model.new(params) unless document.valid? error 400, convert(body_for(:invalid_document, document)) end unless document.save error 400, convert(body_for(:internal_server_error)) end document end |
#create_nested_document!(parent, child_assoc, child_model) ⇒ MongoMapper::Document
Create a nested document from params. If not valid, returns 400.
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/builder/mongo_helpers.rb', line 87 def create_nested_document!(parent, child_assoc, child_model) child = child_model.new(params) if child_model. parent.send(child_assoc) << child unless parent.valid? error 400, convert(body_for(:invalid_document, child)) end unless parent.save error 400, convert(body_for(:internal_server_error)) end else unless child.valid? error 400, convert(body_for(:invalid_document, child)) end unless child.save error 400, convert(body_for(:internal_server_error)) end end child end |
#delete_document!(model, id) ⇒ MongoMapper::Document
Delete a document with id
.
116 117 118 119 120 |
# File 'lib/builder/mongo_helpers.rb', line 116 def delete_document!(model, id) document = find_document!(model, id) document.destroy document end |
#delete_nested_document!(parent, child_assoc, child_model, child_id) ⇒ MongoMapper::Document
Delete a nested document.
137 138 139 140 141 142 143 144 145 146 147 148 149 |
# File 'lib/builder/mongo_helpers.rb', line 137 def delete_nested_document!(parent, child_assoc, child_model, child_id) if child_model. children = parent.send(child_assoc) child = find_child!(children, child_id) children.delete(child) unless parent.save error 400, convert(body_for(:internal_server_error)) end child else delete_document!(child_model, child_id) end end |
#find_child(children, child_id) ⇒ MongoMapper::EmbeddedDocument
Find document with child_id
in children
.
158 159 160 161 162 |
# File 'lib/builder/mongo_helpers.rb', line 158 def find_child(children, child_id) raise Error, "children not true" unless children child_id = child_id.to_s children.detect { |x| x.id.to_s == child_id } end |
#find_child!(children, child_id) ⇒ MongoMapper::EmbeddedDocument
Find document with child_id
in children
or raise 404.
171 172 173 174 175 |
# File 'lib/builder/mongo_helpers.rb', line 171 def find_child!(children, child_id) child = find_child(children, child_id) error 404, convert(body_for(:not_found)) unless child child end |
#find_document!(model, id) ⇒ MongoMapper::Document
Find a document with id
. If not found, returns 404.
185 186 187 188 189 190 191 |
# File 'lib/builder/mongo_helpers.rb', line 185 def find_document!(model, id) document = model.find_by_id(id) unless document error 404, convert(body_for(:not_found)) end document end |
#find_documents!(model, page, items_per_page) ⇒ Array<MongoMapper::Document>
Find model
documents: find all documents if no params, otherwise find selected documents.
245 246 247 248 249 250 251 |
# File 'lib/builder/mongo_helpers.rb', line 245 def find_documents!(model, page, items_per_page) conditions = params.empty? ? {} : make_conditions(params, model) model.all(conditions.merge({ :skip => items_per_page * (page - 1), :limit => items_per_page })) end |
#find_nested_document(parent, child_assoc, child_model, child_id) ⇒ MongoMapper::Document
Find a nested document. If not found, returns nil.
207 208 209 210 211 212 213 214 |
# File 'lib/builder/mongo_helpers.rb', line 207 def find_nested_document(parent, child_assoc, child_model, child_id) if child_model. children = parent.send(child_assoc) find_child(children, child_id) else child_model.find_by_id(child_id) end end |
#find_nested_document!(parent, child_assoc, child_model, child_id) ⇒ MongoMapper::Document
Find a nested document. If not found, returns 404.
230 231 232 233 234 235 236 |
# File 'lib/builder/mongo_helpers.rb', line 230 def find_nested_document!(parent, child_assoc, child_model, child_id) document = find_nested_document(parent, child_assoc, child_model, child_id) unless document error 404, convert(body_for(:not_found)) end document end |
#find_nested_documents!(parent, child_assoc, child_model, page, items_per_page) ⇒ Array<MongoMapper::Document>
Find nested child_model
documents: find all documents if no params, otherwise find selected documents.
267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 |
# File 'lib/builder/mongo_helpers.rb', line 267 def find_nested_documents!(parent, child_assoc, child_model, page, items_per_page) # # TODO: handle page parameter # TODO: handle items_per_page parameter # documents = if child_model. children = parent.send(child_assoc) if params.empty? children else select_by(children, make_conditions(params, child_model)) end else children = if params.empty? child_model.all else child_model.all(make_conditions(params, child_model)) end (parent, child_assoc, children) end end |
#make_related(parent, child, resource_config) ⇒ MongoMapper::Document
Delegates to application, who should use custom logic to relate parent
and child
.
300 301 302 303 304 |
# File 'lib/builder/mongo_helpers.rb', line 300 def (parent, child, resource_config) proc = resource_config[:relation][:create] proc.call(parent, child) if proc child end |
#update_document!(model, id) ⇒ MongoMapper::Document
Update a document with id
from params. If not valid, returns 400.
314 315 316 317 318 319 320 |
# File 'lib/builder/mongo_helpers.rb', line 314 def update_document!(model, id) document = model.update(id, params) unless document.valid? error 400, convert(body_for(:invalid_document, document)) end document end |
#update_nested_document!(parent, child_assoc, child_model, child_id) ⇒ MongoMapper::Document
Update a nested document with params. If not valid, returns 400.
337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 |
# File 'lib/builder/mongo_helpers.rb', line 337 def update_nested_document!(parent, child_assoc, child_model, child_id) if child_model. children = parent.send(child_assoc) child = find_child!(children, child_id) child_index = children.index(child) child.attributes = params children[child_index] = child unless parent.save error 400, convert(body_for(:internal_server_error)) end child else update_document!(child_model, child_id) end end |