# encoding: utf-8
require "mongoid/commands/create"
require "mongoid/commands/deletion"
require "mongoid/commands/delete"
require "mongoid/commands/delete_all"
require "mongoid/commands/destroy"
require "mongoid/commands/destroy_all"
require "mongoid/commands/save"
module Mongoid #:nodoc:
# This module is included in the +Document+ to provide all the persistence
# methods required on the +Document+ object and class.
module Commands
extend ActiveSupport::Concern
module InstanceMethods
# Delete the +Document+ from the database. This method is an optimized
# delete that does not force any callbacks.
#
# Example:
#
# document.delete
#
# Returns: true unless an error occurs.
def delete
Delete.execute(self)
end
# Destroy the +Document+. This will delete the document from the database
# and run the before and after destroy callbacks.
#
# Example:
#
# document.destroy
#
# Returns: true unless an error occurs.
def destroy
Destroy.execute(self)
end
# Save the +Document+. If the document is new, then the before and after
# create callbacks will get executed as well as the save callbacks.
# Otherwise only the save callbacks will run.
#
# Options:
#
# validate: Run validations or not. Defaults to true.
#
# Example:
#
# document.save # save with validations
# document.save(false) # save without validations
#
# Returns: true if validation passes, false if not.
def save(validate = true)
save = lambda { Save.execute(self, validate) }
new_record? ? run_callbacks(:create, &save) : save.call
end
# Save the +Document+, dangerously. Before and after save callbacks will
# get run. If validation fails an error will get raised.
#
# Example:
#
# document.save!
#
# Returns: true if validation passes
def save!
save(true) || (raise Errors::Validations.new(self.errors))
end
# Update the document attributes and persist the document to the
# database. Will delegate to save with all callbacks.
#
# Example:
#
# document.update_attributes(:title => "Test")
def update_attributes(attrs = {})
set_attributes(attrs); save
end
# Update the document attributes and persist the document to the
# database. Will delegate to save!
#
# Example:
#
# document.update_attributes!(:title => "Test")
def update_attributes!(attrs = {})
set_attributes(attrs); save!
end
protected
def set_attributes(attrs = {})
run_callbacks(:update) { write_attributes(attrs) }
end
end
module ClassMethods
# Create a new +Document+. This will instantiate a new document and save
# it in a single call. Will always return the document whether save
# passed or not.
#
# Example:
#
# Person.create(:title => "Mr")
#
# Returns: the +Document+.
def create(attributes = {})
document = new(attributes)
Create.execute(document)
document
end
# Create a new +Document+. This will instantiate a new document and save
# it in a single call. Will always return the document whether save
# passed or not. Will raise an error if validation fails.
#
# Example:
#
# Person.create!(:title => "Mr")
#
# Returns: the +Document+.
def create!(attributes = {})
document = Create.execute(new(attributes), true)
raise Errors::Validations.new(document.errors) unless document.errors.empty?
document
end
# 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.
#
# Example:
#
# Person.delete_all(:conditions => { :title => "Sir" })
# Person.delete_all
#
# Returns: true or raises an error.
def delete_all(conditions = {})
DeleteAll.execute(self, conditions)
end
# 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.
#
# Example:
#
# Person.destroy_all(:conditions => { :title => "Sir" })
# Person.destroy_all
#
# Returns: true or raises an error.
def destroy_all(conditions = {})
DestroyAll.execute(self, conditions)
end
end
end
end