Module: TheSortableTreeHelper

Defined in:
app/helpers/the_sortable_tree_helper.rb

Constant Summary collapse

TREE_RENDERERS =

Default renderers

{
  :tree     => RenderTreeHelper,
  :sortable => RenderSortableTreeHelper,
  :expandable => RenderExpandableTreeHelper,
  :nested_options => RenderNestedOptionsHelper
}

Instance Method Summary collapse

Instance Method Details

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

Server Side Render Tree Helper



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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'app/helpers/the_sortable_tree_helper.rb', line 56

def build_server_tree(tree, options= {})
  result = ''
  tree   = Array.wrap tree
  opts   = {
    # node and base node params
    :id    => :id,      # node id field
    :title => :title,   # name of title fileld
    :node  => nil,      # node

    # base options
    :type  => :tree,    # tree type
    :root  => false,    # is it root node?
    :level => 0,        # recursion level
    :namespace => [],   # :admin

    # BOOST! hash
    :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]

  # Define tree class
  opts[:klass] = define_class_of_elements_of(tree) unless opts[:klass]

  # BOOST PATCH (BUILD ONCE)
  # solution of main perfomance problem
  # magick index-hash, which made me happy!

  # Performance comparison
  # Boost OFF: 8000 nodes, 3 levels => (Views: 176281.9ms | ActiveRecord: 189.8ms)
  # Boost  ON: 8000 nodes, 3 levels => (Views: 8987.8ms   | ActiveRecord: 141.6ms)

  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']

    # Boost OFF
    # roots = tree.select{ |node| node.parent_id.blank? }

    # define roots, if it's need
    if roots.nil? && !tree.empty?
      min_parent_id = tree.map(&:parent_id).compact.min
      roots = tree.select{ |elem| elem.parent_id == min_parent_id }
    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, _opts)
      end
    end
  else
    # RENDER NODE'S CHILDREN
    children_res = ''
    children = opts[:boost][node.id.to_s]

    # Boost OFF
    # children = tree.select{ |_node| _node.parent_id == node.id } 

    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, _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(context, render_module, options = {}) ⇒ Object



28
29
30
# File 'app/helpers/the_sortable_tree_helper.rb', line 28

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

#define_class_of_elements_of(tree) ⇒ Object

Common Base Methods



20
21
22
23
24
25
26
# File 'app/helpers/the_sortable_tree_helper.rb', line 20

def define_class_of_elements_of tree
  case
    when tree.is_a?(ActiveRecord::Relation) then tree.name.to_s.underscore.downcase
    when tree.empty?                        then nil
    else tree.first.class.to_s.underscore.downcase
  end
end

#expandable_tree(tree, options = {}) ⇒ Object



48
49
50
# File 'app/helpers/the_sortable_tree_helper.rb', line 48

def expandable_tree tree, options = {}
  build_server_tree(tree, { :type => :expandable }.merge!(options))
end

#just_tree(tree, options = {}) ⇒ Object

Shortcuts



36
37
38
# File 'app/helpers/the_sortable_tree_helper.rb', line 36

def just_tree tree, options = {}
  build_server_tree(tree, { :type => :tree }.merge!(options))
end

#nested_options(tree, options = {}) ⇒ Object



44
45
46
# File 'app/helpers/the_sortable_tree_helper.rb', line 44

def nested_options tree, options = {}
  build_server_tree(tree, { :type => :nested_options }.merge!(options))
end

#sortable_tree(tree, options = {}) ⇒ Object



40
41
42
# File 'app/helpers/the_sortable_tree_helper.rb', line 40

def sortable_tree tree, options = {}
  build_server_tree(tree, { :type => :sortable }.merge!(options))
end