Method: Tenon::TreeHelper#nested_li

Defined in:
app/helpers/tenon/tree_helper.rb

#nested_li(objects, &block) ⇒ Object



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
50
51
52
53
54
55
56
# File 'app/helpers/tenon/tree_helper.rb', line 3

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' data-record-id="#{objects.first.id}">\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 << %(
</li>
</ul>)
        end

        if o.parent_id
          output << %(\n</li>\n<li data-record-id="#{o.id}" class="subpage">\n)
        else
          output << %(\n</li>\n<li data-record-id="#{o.id}" class="top">\n<div class="item-content">\n)
        end
      else
        path << o.parent_id
        output << %(\n<ul class="subpages">\n<li class="subpage" data-record-id="#{o.id}">\n)
      end
    elsif i != 0
      if o.parent_id
        output << %(\n</li>\n<li data-record-id="#{o.id}" class="subpage">\n)
      else
        output << %(\n</li>\n<li data-record-id="#{o.id}" 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 << %(
</li>
</ul>)
    else
      output << %(
</div>
</li>)
    end
  end

  output.html_safe
end