Module: ActsAsTree::Presentation

Defined in:
lib/acts_as_tree.rb

Instance Method Summary collapse

Instance Method Details

#tree_view(label_method = :to_s, node = nil, level = -1)) ⇒ Object

show records in a tree view Example: root

|_ child1
|    |_ subchild1
|    |_ subchild2
|_ child2
     |_ subchild3
     |_ subchild4

122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/acts_as_tree.rb', line 122

def tree_view(label_method = :to_s,  node = nil, level = -1)
  if node.nil?
    puts "root"
    nodes = roots
  else
    label = "|_ #{node.send(label_method)}"
    if level == 0
      puts " #{label}"
    else
      puts " |#{"    "*level}#{label}"
    end
    nodes = node.children
  end
  nodes.each do |child|
    tree_view(label_method, child, level+1)
  end
end