123456789_123456789_123456789_123456789_123456789_

Module: Mongoid::Persistable::Deletable

Relationships & Source Files
Namespace Children
Modules:
Extension / Inclusion / Inheritance Descendants
Included In:
Super Chains via Extension / Inclusion / Inheritance
Class Chain:
self, ActiveSupport::Concern
Defined in: lib/mongoid/persistable/deletable.rb

Overview

Defines behavior for persistence operations that delete documents.

Instance Method Summary

Instance Method Details

#atomic_deletesHash (private)

This method is for internal use only.

Get the atomic deletes for the operation.

Examples:

Get the atomic deletes.

document.atomic_deletes

Returns:

  • (Hash)

    The atomic deletes.

[ GitHub ]

  
# File 'lib/mongoid/persistable/deletable.rb', line 46

def atomic_deletes
  { atomic_delete_modifier => { atomic_path => _index ? { "_id" => _id } : true }}
end

#delete(options = {}) ⇒ TrueClass Also known as: #remove

Remove the document from the database.

Examples:

Remove the document.

document.remove

Parameters:

  • options (Hash) (defaults to: {})

    The options.

Options Hash (options):

  • :persist (true | false)

    Whether to persist the delete action.

  • :suppress (true | false)

    Whether to update the parent document in-memory when deleting an embedded document.

Returns:

  • (TrueClass)

    True.

[ GitHub ]

  
# 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

#delete_as_embedded(options = {}) ⇒ true (private)

This method is for internal use only.

Delete the embedded document.

Examples:

Delete the embedded document.

document.delete_as_embedded

Parameters:

  • options (Hash) (defaults to: {})

    The deletion options.

Returns:

  • (true)

    If the operation succeeded.

[ GitHub ]

  
# 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_roottrue (private)

This method is for internal use only.

Delete the root document.

Examples:

Delete the root document.

document.delete_as_root

Returns:

  • (true)

    If the document was removed.

[ GitHub ]

  
# 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 = {}) ⇒ true | false (private)

This method is for internal use only.

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

Examples:

Are we notifying the parent.

document.notifying_parent?(suppress: true)

Parameters:

  • options (Hash) (defaults to: {})

    The delete options.

Returns:

  • (true | false)

    If the parent should be notified.

[ GitHub ]

  
# File 'lib/mongoid/persistable/deletable.rb', line 94

def notifying_parent?(options = {})
  !options.delete(:suppress)
end

#prepare_delete {|_self| ... } ⇒ true (private)

This method is for internal use only.

Prepare the delete operation.

Examples:

Prepare the delete operation.

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

Yields:

  • (_self)

Yield Parameters:

  • _self (Deletable)

    the object that the method was called on

Returns:

  • (true)

    If the object was deleted successfully.

Raises:

[ GitHub ]

  
# 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

#remove(options = {})

Alias for #delete.

[ GitHub ]

  
# File 'lib/mongoid/persistable/deletable.rb', line 34

alias :remove :delete