Module: Mongoid::Hierarchy

Extended by:
ActiveSupport::Concern
Included in:
Components
Defined in:
lib/mongoid/hierarchy.rb

Overview

:nodoc

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#_childrenArray<Document>

Get all child Documents to this Document, going n levels deep if necessary. This is used when calling update persistence operations from the root document, where changes in the entire tree need to be determined. Note that persistence from the embedded documents will always be preferred, since they are optimized calls… This operation can get expensive in domains with large hierarchies.

Examples:

Get all the document’s children.

person._children

Returns:



21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/mongoid/hierarchy.rb', line 21

def _children
  @_children ||=
    [].tap do |children|
      relations.each_pair do |name, |
        if .embedded?
          child = send(name)
          Array.wrap(child).each do |doc|
            children.push(doc)
            children.concat(doc._children) unless .versioned?
          end if child
        end
      end
    end
end

#_rootDocument

Return the root document in the object graph. If the current document is the root object in the graph it will return self.

Examples:

Get the root document in the hierarchy.

document._root

Returns:

  • (Document)

    The root document in the hierarchy.



103
104
105
106
107
# File 'lib/mongoid/hierarchy.rb', line 103

def _root
  object = self
  while (object._parent) do object = object._parent; end
  object || self
end

#hereditary?true, false

Determines if the document is a subclass of another document.

Examples:

Check if the document is a subclass

Square.new.hereditary?

Returns:

  • (true, false)

    True if hereditary, false if not.



42
43
44
# File 'lib/mongoid/hierarchy.rb', line 42

def hereditary?
  self.class.hereditary?
end

#parentize(document) ⇒ Document

Sets up a child/parent association. This is used for newly created objects so they can be properly added to the graph.

Examples:

Set the parent document.

document.parentize(parent)

Parameters:

  • document (Document)

    The parent document.

Returns:



55
56
57
# File 'lib/mongoid/hierarchy.rb', line 55

def parentize(document)
  self._parent = document
end

#remove_child(child) ⇒ Object

Remove a child document from this parent. If an embeds one then set to nil, otherwise remove from the embeds many.

This is called from the RemoveEmbedded persistence command.

Examples:

Remove the child.

document.remove_child(child)

Parameters:

  • child (Document)

    The child (embedded) document to remove.

Since:

  • 2.0.0.beta.1



70
71
72
73
74
75
76
77
78
# File 'lib/mongoid/hierarchy.rb', line 70

def remove_child(child)
  name = child..name
  if child.embedded_one?
    remove_ivar(name)
  else
    relation = send(name)
    relation.send(:delete_one, child)
  end
end

#reset_persisted_childrenArray<Document>

After children are persisted we can call this to move all their changes and flag them as persisted in one call.

Examples:

Reset the children.

document.reset_persisted_children

Returns:

Since:

  • 2.1.0



89
90
91
92
93
94
# File 'lib/mongoid/hierarchy.rb', line 89

def reset_persisted_children
  _children.each do |child|
    child.move_changes
    child.new_record = false
  end
end