Module: RailsAdminTreeviewHelper

Defined in:
app/helpers/rails_admin_treeview_helper.rb

Instance Method Summary collapse

Instance Method Details

#treeview(open = false) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'app/helpers/rails_admin_treeview_helper.rb', line 3

def treeview open = false
  if @model_config && @model_config.treeview
    raise "Your #{@abstract_model.model} model must implement `roots` class method and `children` instance method to use treeview." if
      !@abstract_model.model.respond_to?(:roots) || !@abstract_model.model.public_instance_methods.include?("children")
    res = ''
    res << javascript_include_tag("jquery.treeview.js")
    res << stylesheet_link_tag("jquery.treeview.css")
    res << "<hr>"
    res << "<h3>%s</h3>" % @model_config.label_plural
    res << ( :ul, :id => "treeview" do
      @abstract_model.model.roots.map do |root|
        treeview_draw(root, open)
      end.join.html_safe
    end)
    res << javascript_tag('$(document).ready(function(){$("#treeview").treeview({persist: "location"});});')
    res.html_safe
  end
end

#treeview_draw(node, open) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'app/helpers/rails_admin_treeview_helper.rb', line 22

def treeview_draw node, open
   :li, :class => open ? nil : "closed" do
    res = ''
    name = RailsAdmin.config(node.class).with(:object => node).object_label
    res << (:span, link_to_unless_current(name, { :action => "edit",
                                              :id => node.id,
                                              :host => request.host,
                                              :port => request.port == 80 ? nil : request.port,
                                              :protocol => request.scheme }))
    if node.children.size > 0
      res << ( :ul do
        node.children.map do |child|
          treeview_draw(child, open)
        end.join.html_safe
      end)
    end
    res.html_safe
  end
end