Class: Mongoid::Relations::Cascading::Destroy

Inherits:
Object
  • Object
show all
Defined in:
lib/mongoid/relations/cascading/destroy.rb

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (Strategy) initialize(document, metadata)

Initialize the new cascade strategy, which will set up the relation and the metadata.

Examples:

Instantiate the strategy

Strategy.new(document, )

Parameters:

  • document (Document)

    The document to cascade from.

  • metadata (Metadata)

    The relation's metadata.



19
20
21
22
# File 'lib/mongoid/relations/cascading/destroy.rb', line 19

def initialize(document, )
  @document, @metadata = document, 
  @relation = document.send(.name)
end

Instance Attribute Details

- (Object) document

Returns the value of attribute document



7
8
9
# File 'lib/mongoid/relations/cascading/destroy.rb', line 7

def document
  @document
end

- (Object) metadata

Returns the value of attribute metadata



7
8
9
# File 'lib/mongoid/relations/cascading/destroy.rb', line 7

def 
  @metadata
end

- (Object) relation

Returns the value of attribute relation



7
8
9
# File 'lib/mongoid/relations/cascading/destroy.rb', line 7

def relation
  @relation
end

Instance Method Details

- (Object) cascade

Execute the cascading deletion for the relation if it already exists. This should be optimized in the future potentially not to load all objects from the db.

Examples:

Perform the cascading destroy.

strategy.cascade


30
31
32
33
34
35
36
37
38
39
# File 'lib/mongoid/relations/cascading/destroy.rb', line 30

def cascade
  if relation
    if relation.is_a?(Enumerable)
      relation.entries
      relation.each { |doc| doc.destroy }
    else
      relation.destroy
    end
  end
end