3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
# File 'lib/ancestry/treeview/instance_methods.rb', line 3
def tree(params = {})
return '' unless self
subtree = (params[:to_depth].present? ? self.subtree(to_depth: params[:to_depth]) : self.subtree).order('path ASC')
if params[:conditions].is_a?(Array) || params[:conditions].is_a?(Hash)
subtree = subtree.where(params[:conditions])
end
html = '<div class="ancestry-treeview">'
open_ul = 0
prev_depth = self.depth - 1
subtree.each do |node|
curr_depth = node.depth
diff = curr_depth - prev_depth
if curr_depth > prev_depth
html += '<ul><li style="list-style:none">' * (diff - 1).abs
html += node.is_childless? ? '<ul><li class="leaf">' : '<ul><li>'
open_ul += diff
elsif curr_depth == prev_depth
html += node.is_childless? ? '</li><li class="leaf">' : '</li><li>'
elsif curr_depth < prev_depth
html += '</li></ul>' * diff.abs
html += node.is_childless? ? '</li><li class="leaf">' : '</li><li>'
open_ul -= diff
end
prev_depth = curr_depth
html += block_given? ? yield(node) : "<a href=#>#{node.id}</a>"
end
html += '</li></ul>' * open_ul.abs
html += '</div>'
html
end
|