module Mongoid::DocumentPath

Constants

VERSION

Public Class Methods

find(path) click to toggle source
# File lib/mongoid/document_path.rb, line 9
def self.find(path)
  return nil if path.blank?

  root = path.first.type.constantize.find(path.first.document_id)
  return root if path.one?

  path.reduce(root) do |current, node|
    relation_match = if node.matches?(current)
                       current
                     elsif current.respond_to?(:detect)
                       current.detect { |d| node.matches?(d) }
                     end

    if node.terminal?
      return relation_match
    else
      relation_match.send(node.relation)
    end
  end
end

Public Instance Methods

document_path(node = self, current_relation = nil) click to toggle source
# File lib/mongoid/document_path.rb, line 30
def document_path(node = self, current_relation = nil)
  relation = node.embedded? ? node.association_name : nil
  list = node._parent ? document_path(node._parent, relation) : []

  list << Node.new(
    type: node.class.name,
    document_id: node.id,
    relation: current_relation
  )

  list
end