Module: Mongoid::Persistence::ClassMethods
- Defined in:
- lib/mongoid/persistence.rb
Overview
:nodoc:
Instance Method Summary collapse
-
#create(attributes = {}, 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 = {}) ⇒ 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 = {}, 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.
170 171 172 173 174 |
# File 'lib/mongoid/persistence.rb', line 170 def create(attributes = {}, = {}, &block) _creating do new(attributes, , &block).tap { |doc| doc.save } 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.
189 190 191 192 193 194 195 196 |
# File 'lib/mongoid/persistence.rb', line 189 def create!(attributes = {}, = {}, &block) _creating do new(attributes, , &block).tap do |doc| fail_validate!(doc) if doc.insert.errors.any? fail_callback!(doc, :create!) if doc.new? 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.
211 212 213 214 215 216 217 218 |
# File 'lib/mongoid/persistence.rb', line 211 def delete_all(conditions = nil) selector = (conditions || {})[:conditions] || {} selector.merge!(:_type => name) if hereditary? collection.find(selector).count.tap do collection.remove(selector, Safety.) Threaded. end end |
#destroy_all(conditions = {}) ⇒ 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.
233 234 235 236 237 238 |
# File 'lib/mongoid/persistence.rb', line 233 def destroy_all(conditions = {}) documents = all(conditions) documents.count.tap do documents.each { |doc| doc.destroy } end end |
#fail_callback!(document, method) ⇒ Object
Raise an error if a callback failed.
259 260 261 |
# File 'lib/mongoid/persistence.rb', line 259 def fail_callback!(document, method) raise Errors::Callback.new(document.class, method) end |
#fail_validate!(document) ⇒ Object
Raise an error if validation failed.
246 247 248 |
# File 'lib/mongoid/persistence.rb', line 246 def fail_validate!(document) raise Errors::Validations.new(document) end |