module Mongoid::Persistable::Deletable

Defines behavior for persistence operations that delete documents.

Public Instance Methods

delete(options = {}) click to toggle source

Remove the document from the database.

@example Remove the document.

document.remove

@param [ Hash ] options The options. @option options [ true | false ] :persist Whether to persist

the delete action.

@option options [ true | false ] :suppress Whether to update

the parent document in-memory when deleting an embedded document.

@return [ TrueClass ] True.

# File lib/mongoid/persistable/deletable.rb, line 23
def delete(options = {})
  prepare_delete do
    unless options[:persist] == false
      if embedded?
        delete_as_embedded(options)
      else
        delete_as_root
      end
    end
  end
end
Also aliased as: remove
remove(options = {})
Alias for: delete

Private Instance Methods

atomic_deletes() click to toggle source

Get the atomic deletes for the operation.

@api private

@example Get the atomic deletes.

document.atomic_deletes

@return [ Hash ] The atomic deletes.

# File lib/mongoid/persistable/deletable.rb, line 46
def atomic_deletes
  { atomic_delete_modifier => { atomic_path => _index ? { "_id" => _id } : true }}
end
delete_as_embedded(options = {}) click to toggle source

Delete the embedded document.

@api private

@example Delete the embedded document.

document.delete_as_embedded

@param [ Hash ] options The deletion options.

@return [ true ] If the operation succeeded.

# File lib/mongoid/persistable/deletable.rb, line 60
def delete_as_embedded(options = {})
  _parent.remove_child(self) if notifying_parent?(options)
  if _parent.persisted?
    selector = _parent.atomic_selector
    _root.collection.find(selector).update_one(
        positionally(selector, atomic_deletes),
        session: _session)
  end
  true
end
delete_as_root() click to toggle source

Delete the root document.

@api private

@example Delete the root document.

document.delete_as_root

@return [ true ] If the document was removed.

# File lib/mongoid/persistable/deletable.rb, line 79
def delete_as_root
  collection.find(atomic_selector).delete_one(session: _session)
  true
end
notifying_parent?(options = {}) click to toggle source

Are we needing to notify the parent document of the deletion.

@api private

@example Are we notifying the parent.

document.notifying_parent?(suppress: true)

@param [ Hash ] options The delete options.

@return [ true | false ] If the parent should be notified.

# File lib/mongoid/persistable/deletable.rb, line 94
def notifying_parent?(options = {})
  !options.delete(:suppress)
end
prepare_delete() { |self| ... } click to toggle source

Prepare the delete operation.

@api private

@example Prepare the delete operation.

document.prepare_delete do
  collection.find(atomic_selector).remove
end

@return [ true ] If the object was deleted successfully.

# File lib/mongoid/persistable/deletable.rb, line 108
def prepare_delete
  raise Errors::ReadonlyDocument.new(self.class) if readonly?
  yield(self)
  freeze
  self.destroyed = true
end