Module: Mongoid::Persistence::ClassMethods
- Defined in:
- lib/mongoid/persistence.rb
Overview
:nodoc:
Instance Method Summary collapse
-
#create(attributes = nil, options = {}, &block) ⇒ Document+
Create a new document.
-
#create!(attributes = {}, options = {}, &block) ⇒ Document+
Create a new document.
-
#delete_all(conditions = nil) ⇒ Integer
Delete all documents given the supplied conditions.
-
#destroy_all(conditions = nil) ⇒ Integer
Delete all documents given the supplied conditions.
-
#fail_callback!(document, method) ⇒ Object
Raise an error if a callback failed.
-
#fail_validate!(document) ⇒ Object
Raise an error if validation failed.
Instance Method Details
#create(attributes = nil, options = {}, &block) ⇒ Document+
Create a new document. This will instantiate a new document and insert it in a single call. Will always return the document whether save passed or not.
245 246 247 248 249 250 251 252 253 254 255 |
# File 'lib/mongoid/persistence.rb', line 245 def create(attributes = nil, = {}, &block) _creating do if attributes.is_a?(::Array) attributes.map { |attrs| create(attrs, , &block) } else doc = new(attributes, , &block) doc.save doc end end end |
#create!(attributes = {}, options = {}, &block) ⇒ Document+
Create a new document. This will instantiate a new document and insert it in a single call. Will always return the document whether save passed or not, and if validation fails an error will be raise.
276 277 278 279 280 281 282 283 284 285 286 287 |
# File 'lib/mongoid/persistence.rb', line 276 def create!(attributes = {}, = {}, &block) _creating do if attributes.is_a?(::Array) attributes.map { |attrs| create!(attrs, , &block) } else doc = new(attributes, , &block) fail_validate!(doc) unless doc.insert.errors.empty? fail_callback!(doc, :create!) if doc.new_record? doc end end end |
#delete_all(conditions = nil) ⇒ Integer
Delete all documents given the supplied conditions. If no conditions are passed, the entire collection will be dropped for performance benefits. Does not fire any callbacks.
302 303 304 305 306 307 308 309 310 |
# File 'lib/mongoid/persistence.rb', line 302 def delete_all(conditions = nil) conds = conditions || {} selector = conds[:conditions] || conds selector.merge!(_type: name) if hereditary? coll = collection deleted = coll.find(selector).count coll.find(selector).remove_all deleted end |
#destroy_all(conditions = nil) ⇒ Integer
Delete all documents given the supplied conditions. If no conditions are passed, the entire collection will be dropped for performance benefits. Fires the destroy callbacks if conditions were passed.
325 326 327 328 329 330 331 |
# File 'lib/mongoid/persistence.rb', line 325 def destroy_all(conditions = nil) conds = conditions || {} documents = where(conds[:conditions] || conds) destroyed = documents.count documents.each { |doc| doc.destroy } destroyed end |
#fail_callback!(document, method) ⇒ Object
Raise an error if a callback failed.
352 353 354 |
# File 'lib/mongoid/persistence.rb', line 352 def fail_callback!(document, method) raise Errors::Callback.new(document.class, method) end |
#fail_validate!(document) ⇒ Object
Raise an error if validation failed.
339 340 341 |
# File 'lib/mongoid/persistence.rb', line 339 def fail_validate!(document) raise Errors::Validations.new(document) end |