Module: RailsAdminNestedSet::Helper

Defined in:
lib/rails_admin_nested_set/helper.rb

Instance Method Summary collapse

Instance Method Details



39
40
41
42
43
# File 'lib/rails_admin_nested_set/helper.rb', line 39

def action_links(model)
   :ul, class: 'inline actions' do
    menu_for :member, @abstract_model, model, true
  end
end

#max_depthObject



35
36
37
# File 'lib/rails_admin_nested_set/helper.rb', line 35

def max_depth
  @nested_set_conf.options[:max_depth] || '0'
end

#rails_admin_nested_set(tree, opts = {}) ⇒ Object



3
4
5
6
7
8
9
10
# File 'lib/rails_admin_nested_set/helper.rb', line 3

def rails_admin_nested_set(tree, opts= {})
  tree = tree.to_a.sort_by { |m| m.lft }
  roots = tree.select{|elem| elem.parent_id.nil?}
  id = "ns_#{rand(100_000_000..999_999_999)}"
  content = (:ol, rails_admin_nested_set_builder(roots, tree), id: id, class: 'dd-list')
  js = "rails_admin_nested_set({id: '#{id}', max_depth: #{max_depth}, update_url: '#{nested_set_path(model_name: @abstract_model)}'});"
  content + (:script, js.html_safe, type: 'text/javascript')
end

#rails_admin_nested_set_builder(nodes, tree) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/rails_admin_nested_set/helper.rb', line 13

def rails_admin_nested_set_builder(nodes, tree)
  nodes.map do |node|
    li_classes = 'dd-item dd3-item'

     :li, class: li_classes, :'data-id' => node.id do
      output =  :div, 'drag', class: 'dd-handle dd3-handle'
      output+=  :div, class: 'dd3-content' do
        content = link_to @model_config.with(object: node).object_label, edit_path(@abstract_model, node.id)
        content + (:div, action_links(node), class: 'pull-right links')
      end

      children = tree.select{|elem| elem.parent_id == node.id}
      output = (:div, output)
      if children.any?
         output += (:ol, rails_admin_nested_set_builder(children, tree), class: 'dd-list')
      end

      output
    end
  end.join.html_safe
end