module Mongoid::Association::Depending

This module defines the behavior for setting up cascading deletes and nullifies for associations, and how to delegate to the appropriate strategy.

Constants

STRATEGIES

The valid dependent strategies.

@since 7.0

Public Class Methods

define_dependency!(association) click to toggle source

Attempt to add the cascading information for the document to know how to handle associated documents on a removal.

@example Set up cascading information

Mongoid::Association::Depending.define_dependency!(association)

@param [ Association ] association The association metadata.

@return [ Class ] The class of the document.

@since 2.0.0.rc.1

# File lib/mongoid/association/depending.rb, line 55
def self.define_dependency!(association)
  validate!(association)
  association.inverse_class.tap do |klass|
    if klass.dependents_owner != klass
      klass.dependents = []
      klass.dependents_owner = klass
    end

    if association.dependent && !klass.dependents.include?(association)
      klass.dependents.push(association)
    end
  end
end
validate!(association) click to toggle source
# File lib/mongoid/association/depending.rb, line 69
def self.validate!(association)
  unless STRATEGIES.include?(association.dependent)
    raise Errors::InvalidDependentStrategy.new(association,
                                               association.dependent,
                                               STRATEGIES)
  end
end

Public Instance Methods

_all_dependents() click to toggle source

@api private

# File lib/mongoid/association/depending.rb, line 24
def _all_dependents
  superclass_dependents = superclass.respond_to?(:_all_dependents) ? superclass._all_dependents : []
  dependents + superclass_dependents.reject do |new_dep|
    dependents.any? do |old_dep| old_dep.name == new_dep.name
    end
  end
end
apply_destroy_dependencies!() click to toggle source

Perform all cascading deletes, destroys, or nullifies. Will delegate to the appropriate strategy to perform the operation.

@example Execute cascades.

document.apply_destroy_dependencies!

@since 2.0.0.rc.1

# File lib/mongoid/association/depending.rb, line 84
def apply_destroy_dependencies!
  self.class._all_dependents.each do |association|
    if dependent = association.try(:dependent)
      send("_dependent_#{dependent}!", association)
    end
  end
end

Private Instance Methods

_dependent_delete_all!(association) click to toggle source
# File lib/mongoid/association/depending.rb, line 94
def _dependent_delete_all!(association)
  if relation = send(association.name)
    if relation.respond_to?(:dependents) && relation.dependents.blank?
      relation.clear
    else
      ::Array.wrap(send(association.name)).each { |rel| rel.delete }
    end
  end
end
_dependent_destroy!(association) click to toggle source
# File lib/mongoid/association/depending.rb, line 104
def _dependent_destroy!(association)
  if relation = send(association.name)
    if relation.is_a?(Enumerable)
      relation.entries
      relation.each { |doc| doc.destroy }
    else
      relation.destroy
    end
  end
end
_dependent_nullify!(association) click to toggle source
# File lib/mongoid/association/depending.rb, line 115
def _dependent_nullify!(association)
  if relation = send(association.name)
    relation.nullify
  end
end
_dependent_restrict_with_error!(association) click to toggle source
# File lib/mongoid/association/depending.rb, line 127
def _dependent_restrict_with_error!(association)
  if (relation = send(association.name)) && !relation.blank?
    errors.add(association.name, :destroy_restrict_with_error_dependencies_exist)
    throw(:abort, false)
  end
end
_dependent_restrict_with_exception!(association) click to toggle source
# File lib/mongoid/association/depending.rb, line 121
def _dependent_restrict_with_exception!(association)
  if (relation = send(association.name)) && !relation.blank?
    raise Errors::DeleteRestriction.new(relation, association.name)
  end
end