Module: AwesomeNestedSet::Tools::Helper

Defined in:
lib/awesome_nested_set/tools/helper.rb

Instance Method Summary collapse

Instance Method Details

#nested_li(objects, options = {}, &block) ⇒ Object



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
# File 'lib/awesome_nested_set/tools/helper.rb', line 4

def nested_li(objects, options = {}, &block)
  options.reverse_merge!({
    tree_css_classes: 'list-group',
    item_css_classes: 'list-group-item'
  })

  objects = objects.order(:lft) if objects.is_a? Class

  return '' if objects.size == 0

  if objects.first.root?
    output = "<ul class=\"#{options[:tree_css_classes]}\"><li class=\"#{options[:item_css_classes]}\">"
  else
    output = ''
  end
  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
        output << "</li><li class=\"#{options[:item_css_classes]}\">"
      else
        path << o.parent_id
        if i == 0 && !objects.first.root?
          output << "<ul class=\"#{options[:tree_css_classes]}\"><li class=\"#{options[:item_css_classes]}\">"
        else
          output << "<ul><li class=\"#{options[:item_css_classes]}\">"
        end
      end
    elsif i != 0
      output << "</li><li class=\"#{options[:item_css_classes]}\">"
    end
    o = o.first if o.is_a? Array
    output << capture(o, path.size - 1, &block)
  end

  if objects.first.root?
    output << '</li></ul>' * path.length
  else
    output << '</li></ul>' * (path.length - 1)
  end
  output.html_safe
end

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



54
55
56
# File 'lib/awesome_nested_set/tools/helper.rb', line 54

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