Class: StructureGraph

Inherits:
ActiveRecord::Base
  • Object
show all
Includes:
CoreBase
Defined in:
app/models/structure_graph.rb

Overview

Attributes:

Associations:

Instance Method Summary (collapse)

Instance Method Details

- (Object) graph_as_tree



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'app/models/structure_graph.rb', line 18

def graph_as_tree

  # rather than use recursive queries, get the entire list and set the child_structures
  # lists all at once

  id_hash = {}

  slist = Structure.find(:all, :conditions => "graph_id = #{id}", :order => "graph_order")
  root = slist[0]

  # index the keys
  slist.each do |s|
    # create a handy little accessor for children
    def s.children
      @child_structures ||= []
    end
    id_hash[s.id] = s
  end

  # populate the child list
  slist.each do |s|
    parent = id_hash[s.parent_structure_id]
    next if parent.nil?
    parent.children << s
  end
  
  # return the root
  root
end