Module: Mongoid::Hierarchy
Defined Under Namespace
Modules: ClassMethods
Instance Method Summary collapse
-
#_children ⇒ Array<Document>
Get all child
Documents
to thisDocument
, going n levels deep if necessary. -
#_root ⇒ Document
Return the root document in the object graph.
-
#_root? ⇒ true, false
Is this document the root document of the hierarchy?.
-
#collect_children ⇒ Array<Document>
Collect all the children of this document.
-
#flag_children_persisted ⇒ Array<Document>
Marks all children as being persisted.
-
#hereditary? ⇒ true, false
Determines if the document is a subclass of another document.
-
#parentize(document) ⇒ Document
Sets up a child/parent association.
-
#remove_child(child) ⇒ Object
Remove a child document from this parent.
-
#reset_persisted_children ⇒ Array<Document>
After children are persisted we can call this to move all their changes and flag them as persisted in one call.
Instance Method Details
#_children ⇒ Array<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.
21 22 23 |
# File 'lib/mongoid/hierarchy.rb', line 21 def _children @_children ||= collect_children end |
#_root ⇒ Document
Return the root document in the object graph. If the current document is the root object in the graph it will return self.
128 129 130 131 132 |
# File 'lib/mongoid/hierarchy.rb', line 128 def _root object = self while (object._parent) do object = object._parent; end object || self end |
#_root? ⇒ true, false
Is this document the root document of the hierarchy?
142 143 144 |
# File 'lib/mongoid/hierarchy.rb', line 142 def _root? _parent ? false : true end |
#collect_children ⇒ Array<Document>
Collect all the children of this document.
33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/mongoid/hierarchy.rb', line 33 def collect_children children = [] .each_pair do |name, | without_autobuild do child = send(name) Array.wrap(child).each do |doc| children.push(doc) children.concat(doc._children) unless .versioned? end if child end end children end |
#flag_children_persisted ⇒ Array<Document>
Marks all children as being persisted.
55 56 57 58 59 |
# File 'lib/mongoid/hierarchy.rb', line 55 def flag_children_persisted _children.each do |child| child.new_record = false end end |
#hereditary? ⇒ true, false
Determines if the document is a subclass of another document.
67 68 69 |
# File 'lib/mongoid/hierarchy.rb', line 67 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.
80 81 82 |
# File 'lib/mongoid/hierarchy.rb', line 80 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.
95 96 97 98 99 100 101 102 103 |
# File 'lib/mongoid/hierarchy.rb', line 95 def remove_child(child) name = child. if child. remove_ivar(name) else relation = send(name) relation.send(:delete_one, child) end end |
#reset_persisted_children ⇒ Array<Document>
After children are persisted we can call this to move all their changes and flag them as persisted in one call.
114 115 116 117 118 119 |
# File 'lib/mongoid/hierarchy.rb', line 114 def reset_persisted_children _children.each do |child| child.move_changes child.new_record = false end end |