Module: Mongoid::Association::Depending
- Extended by:
- ActiveSupport::Concern
- Included in:
- Mongoid::Association
- 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 collapse
- STRATEGIES =
The valid dependent strategies.
[ :delete_all, :destroy, :nullify, :restrict_with_exception, :restrict_with_error ]
Class Method Summary collapse
-
.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) ⇒ Object
Validates that an association’s dependent strategy is within the allowed enumeration.
Instance Method Summary collapse
-
#apply_destroy_dependencies! ⇒ Object
Perform all cascading deletes, destroys, or nullifies.
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.
57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/mongoid/association/depending.rb', line 57 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) ⇒ Object
Validates that an association’s dependent strategy is within the allowed enumeration.
79 80 81 82 83 84 85 |
# File 'lib/mongoid/association/depending.rb', line 79 def self.validate!(association) unless STRATEGIES.include?(association.dependent) raise Errors::InvalidDependentStrategy.new(association, association.dependent, STRATEGIES) end end |
Instance Method Details
#apply_destroy_dependencies! ⇒ Object
Perform all cascading deletes, destroys, or nullifies. Will delegate to the appropriate strategy to perform the operation.
92 93 94 95 96 97 98 |
# File 'lib/mongoid/association/depending.rb', line 92 def apply_destroy_dependencies! self.class._all_dependents.each do |association| if dependent = association.try(:dependent) send("_dependent_#{dependent}!", association) end end end |