Module: Mongoid::Hierarchy::InstanceMethods

Defined in:
lib/mongoid/hierarchy.rb

Overview

:nodoc:

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:



35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/mongoid/hierarchy.rb', line 35

def _children
  relations.inject([]) do |children, (name, )|
    children.tap do |kids|
      if .embedded? && name != "versions"
        child = send(name)
        child.to_a.each do |doc|
          kids.push(doc).concat(doc._children)
        end unless child.blank?
      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.



78
79
80
81
82
# File 'lib/mongoid/hierarchy.rb', line 78

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.



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

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:



67
68
69
# File 'lib/mongoid/hierarchy.rb', line 67

def parentize(document)
  self._parent = document
end