Module: Mongoid::Association::Depending
| Relationships & Source Files | |
| Extension / Inclusion / Inheritance Descendants | |
|
Included In:
| |
| Super Chains via Extension / Inclusion / Inheritance | |
|
Class Chain:
self,
ActiveSupport::Concern
|
|
| Defined in: | lib/mongoid/association/depending.rb |
Overview
This module defines the behavior for setting up cascading deletes and nullifies for associations, and how to delegate to the appropriate strategy.
Constant Summary
-
STRATEGIES =
# File 'lib/mongoid/association/depending.rb', line 38
The valid dependent strategies.
%i[ delete_all destroy nullify restrict_with_exception restrict_with_error ]
Class Method Summary
-
.define_dependency!(association) ⇒ Class
Attempt to add the cascading information for the document to know how to handle associated documents on a removal.
-
.validate!(association)
Validates that an association’s dependent strategy is within the allowed enumeration.
Instance Method Summary
-
#apply_destroy_dependencies!
Perform all cascading deletes, destroys, or nullifies.
- #_dependent_delete_all!(association) private
- #_dependent_destroy!(association) private
- #_dependent_nullify!(association) private
- #_dependent_restrict_with_error!(association) private
- #_dependent_restrict_with_exception!(association) private
DSL Calls
included
[ GitHub ]10 11 12 13 14 15 16 17 18
# File 'lib/mongoid/association/depending.rb', line 10
included do class_attribute :dependents # @api private class_attribute :dependents_owner self.dependents = [] self.dependents_owner = self end
Class Method Details
.define_dependency!(association) ⇒ Class
Attempt to add the cascading information for the document to know how to handle associated documents on a removal.
# 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 klass.dependents.push(association) if association.dependent && !klass.dependents.include?(association) end end
.validate!(association)
Validates that an association’s dependent strategy is within the allowed enumeration.
# File 'lib/mongoid/association/depending.rb', line 75
def self.validate!(association) return if STRATEGIES.include?(association.dependent) raise Errors::InvalidDependentStrategy.new(association, association.dependent, STRATEGIES) end
Instance Method Details
#_dependent_delete_all!(association) (private)
[ GitHub ]# File 'lib/mongoid/association/depending.rb', line 98
def _dependent_delete_all!(association) return unless 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
#_dependent_destroy!(association) (private)
[ GitHub ]# File 'lib/mongoid/association/depending.rb', line 108
def _dependent_destroy!(association) return unless relation = send(association.name) if relation.is_a?(Enumerable) relation.entries relation.each { |doc| doc.destroy } else relation.destroy end end
#_dependent_nullify!(association) (private)
[ GitHub ]# File 'lib/mongoid/association/depending.rb', line 119
def _dependent_nullify!(association) return unless relation = send(association.name) relation.nullify end
#_dependent_restrict_with_error!(association) (private)
[ GitHub ]# File 'lib/mongoid/association/depending.rb', line 131
def _dependent_restrict_with_error!(association) return unless (relation = send(association.name)) && !relation.blank? errors.add(association.name, :destroy_restrict_with_error_dependencies_exist) throw(:abort, false) end
#_dependent_restrict_with_exception!(association) (private)
[ GitHub ]# File 'lib/mongoid/association/depending.rb', line 125
def _dependent_restrict_with_exception!(association) if (relation = send(association.name)) && !relation.blank? raise Errors::DeleteRestriction.new(relation, association.name) end end
#apply_destroy_dependencies!
Perform all cascading deletes, destroys, or nullifies. Will delegate to the appropriate strategy to perform the operation.
# File 'lib/mongoid/association/depending.rb', line 88
def apply_destroy_dependencies! self.class._all_dependents.each do |association| if dependent = association.try(:dependent) send("_dependent_#{dependent}!", association) end end end