Class: AncestorListing

Inherits:
Object
  • Object
show all
Defined in:
backend/app/model/ancestor_listing.rb

Defined Under Namespace

Classes: Ancestor

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(node_class, objs) ⇒ AncestorListing

Returns a new instance of AncestorListing.



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'backend/app/model/ancestor_listing.rb', line 31

def initialize(node_class, objs)
  @node_model = node_class
  @root_model = node_class.root_model

  @ancestors = {}
  @root_records = {}

  objs.each do |obj|
    @ancestors[obj.id] = {:id => obj.id,
                          :level => level_value(obj.level, obj.other_level),
                          :parent_id => obj.parent_id,
                          :root_record_id => obj.root_record_id}
  end

  build_ancestor_links
end

Class Method Details

.add_ancestors(objs, jsons) ⇒ Object

For a given set of Sequel objs and their corresponding jsons records, walk back up the tree enumerating their ancestor records and add them as refs to the ancestors JSON property.



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'backend/app/model/ancestor_listing.rb', line 6

def self.add_ancestors(objs, jsons)
  return if objs.empty?

  node_class = objs[0].class
  ancestors = new(node_class, objs)

  jsons.zip(objs).each do |json, obj|
    json['ancestors'] = []

    current = obj
    while (parent = ancestors.parent_of(current.id))
      json['ancestors'] << {'ref' => parent.uri,
                            'level' => parent.level}
      current = parent
    end

    root = ancestors.root_record(obj[:root_record_id])
    json['ancestors'] << {
      'ref' => root.uri,
      'level' => root.level
    }
  end
end

Instance Method Details

#parent_of(node_id) ⇒ Object

The parent of the given node



49
50
51
52
53
54
55
56
57
58
59
# File 'backend/app/model/ancestor_listing.rb', line 49

def parent_of(node_id)
  parent_id = @ancestors.fetch(node_id)[:parent_id]

  if parent_id
    ancestor = @ancestors.fetch(parent_id)

    Ancestor.new(ancestor[:id],
                 @node_model.uri_for(@node_model.my_jsonmodel.record_type, ancestor[:id]),
                 ancestor[:level])
  end
end

#root_record(root_record_id) ⇒ Object

The root record of the given node



62
63
64
65
66
67
68
# File 'backend/app/model/ancestor_listing.rb', line 62

def root_record(root_record_id)
  root_record = @root_records.fetch(root_record_id)

  Ancestor.new(root_record[:id],
               @root_model.uri_for(@root_model.my_jsonmodel.record_type, root_record[:id]),
               root_record[:level])
end