Module: Forge::TreeHelper

Defined in:
lib/forge/app/helpers/forge/tree_helper.rb

Instance Method Summary collapse

Instance Method Details

#nested_li(objects, &block) ⇒ Object



2
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/forge/app/helpers/forge/tree_helper.rb', line 2

def nested_li(objects, &block)
  # This code is a frightmare.  I didn't write it I swear.
  objects = objects.order(:lft) if objects.is_a? Class

  return '' if objects.size == 0

  output = %{<li class="top">\n<div class="item-content">\n}
  path = [nil]

  objects.each_with_index do |o, i|
    if o.parent_id != path.last
      # We are on a new level, did we decend or ascend?
      if path.include?(o.parent_id)
        # Remove wrong wrong tailing paths elements
        while path.last != o.parent_id
          path.pop
          output << %{\n</li>\n</ul>}
        end

        if o.parent_id
          output << %{\n</li>\n<li class="subpage">\n}
        else
          output << %{\n</li>\n<li class="top">\n<div class="item-content">\n}
        end
      else
        path << o.parent_id
        output << %{\n<ul class="subpages">\n<li class="subpage">\n}
      end
    elsif i != 0
      if o.parent_id
          output << %{\n</li>\n<li class="subpage">\n}
        else
          output << %{\n</li>\n<li class="top">\n<div class="item-content">\n}
        end
    end
    output << capture(o, path.size - 1, &block)
  end

  path.each do |p|
    if p
      output << %{\n</li>\n</ul>}
    else
      output << %{\n</div>\n</li>}
    end
  end

  output.html_safe
end

#sorted_nested_li(objects, order, &block) ⇒ Object



51
52
53
# File 'lib/forge/app/helpers/forge/tree_helper.rb', line 51

def sorted_nested_li(objects, order, &block)
  nested_li sort_list(objects, order), &block
end