Module: Mongoid::DocumentPath

Extended by:
ActiveSupport::Concern
Defined in:
lib/mongoid/document_path.rb,
lib/mongoid/document_path/node.rb,
lib/mongoid/document_path/version.rb

Defined Under Namespace

Classes: Node

Constant Summary collapse

VERSION =
"0.2.0"

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.find(path) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# 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

Instance Method Details

#document_path(node = self, current_relation = nil) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
# 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