Class: CollectionLayout::TreeData

Inherits:
Object
  • Object
show all
Defined in:
lib/collection_layout/tree_data.rb

Instance Method Summary collapse

Constructor Details

#initialize(building) ⇒ TreeData

Returns a new instance of TreeData.

Parameters:



11
12
13
# File 'lib/collection_layout/tree_data.rb', line 11

def initialize(building)
  @building = building
end

Instance Method Details

#building_node(container, children) ⇒ Object (private)



40
41
42
43
44
45
46
47
# File 'lib/collection_layout/tree_data.rb', line 40

def building_node(container, children)
  {
    id: container.id,
    name: container.name.presence || "Building #{container.id}",
    type: container.type,
    children: children
  }
end

#container_node(container, children) ⇒ Object (private)



49
50
51
52
53
54
55
56
# File 'lib/collection_layout/tree_data.rb', line 49

def container_node(container, children)
  {
    id: container.id,
    name: container.name.presence || container.type.demodulize,
    type: container.type,
    children: children
  }
end

#leaf_node(container) ⇒ Object (private)



58
59
60
61
62
63
64
65
66
# File 'lib/collection_layout/tree_data.rb', line 58

def leaf_node(container)
  {
    id: container.id,
    name: container.name.presence || container.type.demodulize,
    type: container.type,
    value: 1,
    children: []
  }
end

#to_json_treeHash

Returns nested treemap-compatible structure.

Returns:

  • (Hash)

    nested treemap-compatible structure



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/collection_layout/tree_data.rb', line 16

def to_json_tree
  ci = ContainerItem.find_by(contained_object: @building)
  return building_node(@building, []) if ci.nil?

  room_nodes = ci.children.map do |ci_room|
    room = ci_room.contained_object
    cabinet_nodes = ci_room.children.map do |ci_cabinet|
      cabinet = ci_cabinet.contained_object

      drawer_nodes = ci_cabinet.children.map do |ci_drawer|
        drawer = ci_drawer.contained_object
        leaf_node(drawer)
      end

      container_node(cabinet, drawer_nodes)
    end
    container_node(room, cabinet_nodes)
  end

  building_node(@building, room_nodes)
end