Module: Mongoid::Traversable

Extended by:
ActiveSupport::Concern
Included in:
Composable
Defined in:
lib/mongoid/traversable.rb

Overview

Mixin module included in Mongoid::Document to provide behavior around traversing the document graph.

Defined Under Namespace

Modules: ClassMethods, DiscriminatorAssignment, DiscriminatorRetrieval

Instance Method Summary collapse

Instance Method Details

#_children(reset: false) ⇒ Array<Document>

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Get all child Documents to this Document

Returns:

  • (Array<Document>)

    All child documents in the hierarchy.



198
199
200
201
202
203
204
205
206
207
208
209
# File 'lib/mongoid/traversable.rb', line 198

def _children(reset: false)
  # See discussion above for the `_parent` method, as to why the variable
  # here needs to have two underscores.
  #
  # rubocop:disable Naming/MemoizedInstanceVariableName
  if reset
    @__children = nil
  else
    @__children ||= collect_children
  end
  # rubocop:enable Naming/MemoizedInstanceVariableName
end

#_descendants(reset: false) ⇒ Array<Document>

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Get all descendant Documents of this Document recursively. 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.

Returns:

  • (Array<Document>)

    All descendant documents in the hierarchy.



221
222
223
224
225
226
227
228
229
230
231
232
# File 'lib/mongoid/traversable.rb', line 221

def _descendants(reset: false)
  # See discussion above for the `_parent` method, as to why the variable
  # here needs to have two underscores.
  #
  # rubocop:disable Naming/MemoizedInstanceVariableName
  if reset
    @__descendants = nil
  else
    @__descendants ||= collect_descendants
  end
  # rubocop:enable Naming/MemoizedInstanceVariableName
end

#_parentMongoid::Document | nil

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Retrieves the parent document of this document.

Returns:



69
70
71
# File 'lib/mongoid/traversable.rb', line 69

def _parent
  @__parent || nil
end

#_parent=(document) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Sets the parent document of this document.

Parameters:



81
82
83
# File 'lib/mongoid/traversable.rb', line 81

def _parent=(document)
  @__parent = document
end

#_reset_memoized_descendants!nil

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Resets the memoized descendants on the object. Called internally when an embedded array changes size.

Returns:

  • (nil)

    nil.



348
349
350
351
352
# File 'lib/mongoid/traversable.rb', line 348

def _reset_memoized_descendants!
  _parent&._reset_memoized_descendants!
  _children reset: true
  _descendants reset: true
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.



361
362
363
364
365
# File 'lib/mongoid/traversable.rb', line 361

def _root
  object = self
  object = object._parent while object._parent
  object
end

#_root?true | false

Is this document the root document of the hierarchy?

Examples:

Is the document the root?

document._root?

Returns:

  • (true | false)

    If the document is the root.



373
374
375
# File 'lib/mongoid/traversable.rb', line 373

def _root?
  _parent ? false : true
end

#collect_childrenArray<Document>

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Collect all the children of this document.

Returns:



239
240
241
242
243
244
245
246
247
248
# File 'lib/mongoid/traversable.rb', line 239

def collect_children
  [].tap do |children|
    embedded_relations.each_pair do |name, _association|
      without_autobuild do
        child = send(name)
        children.concat(Array.wrap(child)) if child
      end
    end
  end
end

#collect_descendantsArray<Document>

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Collect all the descendants of this document.

Returns:

  • (Array<Document>)

    The descendants.



255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
# File 'lib/mongoid/traversable.rb', line 255

def collect_descendants
  children = []
  to_expand = _children
  expanded = {}

  until to_expand.empty?
    expanding = to_expand
    to_expand = []
    expanding.each do |child|
      next if expanded[child]

      # Don't mark expanded if _id is nil, since documents are compared by
      # their _ids, multiple embedded documents with nil ids will compare
      # equally, and some documents will not be expanded.
      expanded[child] = true if child._id
      children << child
      to_expand += child._children
    end
  end

  children
end

#flag_descendants_persistedArray<Document>

Marks all descendants as being persisted.

Returns:

  • (Array<Document>)

    The flagged descendants.



281
282
283
284
285
# File 'lib/mongoid/traversable.rb', line 281

def flag_descendants_persisted
  _descendants.each do |child|
    child.new_record = false
  end
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.



293
294
295
# File 'lib/mongoid/traversable.rb', line 293

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:



306
307
308
# File 'lib/mongoid/traversable.rb', line 306

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.



319
320
321
322
323
324
325
326
327
328
# File 'lib/mongoid/traversable.rb', line 319

def remove_child(child)
  name = child.association_name
  if child.embedded_one?
    attributes.delete(child._association.store_as)
    remove_ivar(name)
  else
    relation = send(name)
    relation._remove(child)
  end
end

#reset_persisted_descendantsArray<Document>

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

Returns:

  • (Array<Document>)

    The descendants.



334
335
336
337
338
339
340
# File 'lib/mongoid/traversable.rb', line 334

def reset_persisted_descendants
  _descendants.each do |child|
    child.move_changes
    child.new_record = false
  end
  _reset_memoized_descendants!
end