Module: MongoidSortableTreeHelper

Defined in:
app/helpers/mongoid_sortable_tree_helper.rb

Constant Summary collapse

TREE_RENDERERS =

Default renderers

{
  :sortable => RenderSortableTreeHelper
}

Instance Method Summary collapse

Instance Method Details

#build_server_tree(tree, options: {}) ⇒ Object



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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'app/helpers/mongoid_sortable_tree_helper.rb', line 23

def build_server_tree(tree, options: {})
  result = ''
  tree   = Array.wrap tree
  opts   = {
    # base options
    :type  => :sortable,    # tree type
    :id    => :id,          # id field
    :node  => nil,          # node
    :title => :text,        # title field
    :li_attr => :li_attr,
    :a_attr => :a_attr,
    :root  => false,    # is it root node?
    :level => 0,        # recursion level
    :boost => {} 
  }.merge!(options)

   # Basic vars
  root = opts[:root]
  node = opts[:node]

  # namespace prepare [Rails require]
  opts[:namespace] = Array.wrap opts[:namespace]

  # Module with **Render** class
  opts[:render_module] = TREE_RENDERERS[opts[:type]] unless opts[:render_module]
  
  opts[:klass] = define_class_of_elements_of(tree) unless opts[:klass]

  if opts[:boost].empty?
    tree.each do |item|
      num = item.parent_id || 0
      opts[:boost][num.to_s] = [] unless opts[:boost][num.to_s]
      opts[:boost][num.to_s].push item
    end
  end

  unless node
    # RENDER ROOTS
    roots = opts[:boost]['0']

    # define roots, if it's need
    if roots.nil? && !tree.empty?
      min_parent_ids = tree.map(&:parent_ids).min { |x,y| x.size <=> y.size }
      roots = tree.select{ |elem| elem.parent_ids == min_parent_ids }
    end

    # children rendering
    if roots
      roots.each do |root|
        _opts  =  opts.merge({ :node => root, :root => true, :level => opts[:level].next, :boost => opts[:boost] })
        result << build_server_tree(tree, options: _opts)
      end
    end
  else
    # RENDER NODE'S CHILDREN
    children_res = ''
    # has parent_id which mean it has children
    children = opts[:boost][node.id.to_s]

    opts.merge!({ :has_children => children.blank? })

    unless children.nil?
      children.each do |elem|
        _opts        =  opts.merge({ :node => elem, :root => false, :level => opts[:level].next, :boost => opts[:boost] })
        children_res << build_server_tree(tree, options: _opts)
      end
    end

    result << build_tree_html(self, opts[:render_module], opts.merge({ :root => root, :node => node, :children => children_res }))
  end
  raw result
end

#build_tree_html(tree, render_module, options = {}) ⇒ Object



19
20
21
# File 'app/helpers/mongoid_sortable_tree_helper.rb', line 19

def build_tree_html tree, render_module, options = {}
  render_module::Render::render_node(tree, options)
end

#define_class_of_elements_of(node) ⇒ Object

Common Base Methods



11
12
13
14
15
16
17
# File 'app/helpers/mongoid_sortable_tree_helper.rb', line 11

def define_class_of_elements_of node
  case
    when node.is_a?(Array) then node.first.class.to_s.underscore.downcase
    when node.nil?       then 'tag'
    else node.class.to_s.underscore.downcase
  end
end