Module: Admin::AdminsiteMenuHelper

Defined in:
app/helpers/admin/adminsite_menu_helper.rb

Instance Method Summary collapse

Instance Method Details

#child_controller_active?(child_controller_names) ⇒ Boolean

Returns:

  • (Boolean)


68
69
70
# File 'app/helpers/admin/adminsite_menu_helper.rb', line 68

def child_controller_active?(child_controller_names)
  child_controller_names.include?(controller.controller_name)
end

#content_menu_item(controller_name, admin_menu, klasses, method) ⇒ Object



50
51
52
53
54
55
56
57
58
# File 'app/helpers/admin/adminsite_menu_helper.rb', line 50

def content_menu_item(controller_name, admin_menu, klasses, method )
  if controller_name != controller_name.pluralize
    url = eval("admin_#{controller_name}_index_path")
  else
    url = eval("admin_#{controller_name}_path")
  end
  link = link_to(content_menu_label(url, controller_name), "#{url}?admin_menu=#{admin_menu}", method: method, )
  raw "<li class='#{html_classes(url, controller_name, klasses, admin_menu )}'>#{link}</li>"
end

#content_menu_label(url, controller_name) ⇒ Object



44
45
46
47
48
# File 'app/helpers/admin/adminsite_menu_helper.rb', line 44

def content_menu_label(url, controller_name)
  menu_controller = recognize_path(url)[:controller]
  return controller_name.titlecase if menu_controller.blank?
  eval("#{menu_controller}_controller".classify).content_menu_label
end

#current_admin_menuObject



12
13
14
# File 'app/helpers/admin/adminsite_menu_helper.rb', line 12

def current_admin_menu
  @current_admin_menu ||= params[:admin_menu]
end

#current_url?(url, label = '') ⇒ Boolean

Returns:

  • (Boolean)


60
61
62
63
64
65
66
# File 'app/helpers/admin/adminsite_menu_helper.rb', line 60

def current_url?(url, label = '')
  if label.present?
    return request.fullpath == "#{url}?admin_menu=#{label}"
  else
    return request.fullpath == url
  end
end

#html_classes(url, controller_name, klasses = nil, admin_menu = '', label = '') ⇒ Object



72
73
74
75
76
77
78
# File 'app/helpers/admin/adminsite_menu_helper.rb', line 72

def html_classes(url, controller_name, klasses = nil, admin_menu = '', label = '')
  result = []
  result |= [klasses].flatten if klasses.present?
  result |= ['current'] if current_url?(url, admin_menu)
  result |= ['active']  if current_admin_menu == label || controller_name == controller.controller_name
  result.join(' ')
end


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
# File 'app/helpers/admin/adminsite_menu_helper.rb', line 16

def menu_item(label, url, child_controller_names = [], klasses = nil, method = nil, admin_menu = label)
  result = ''
  child_menus = []

  child_controller_names_authorized = child_controller_names.select do |child_controller_name|
    controller_class_name = "#{child_controller_name}_controller".classify
    controller_class = eval("defined?(Adminsite::" + "#{controller_class_name}) ? Adminsite::#{controller_class_name} : Adminsite::Admin::#{controller_class_name}".classify)
    can?(:read, controller_class.new.authorize_resource_class)
  end

  if current_url?(url, label) || ( child_controller_active?(child_controller_names) && current_admin_menu == admin_menu)
    child_controller_names_authorized.each do |child_controller_name|
      child_menus << content_menu_item(child_controller_name, admin_menu, nil, nil )
    end
  end
  child_menus = child_menus.compact

  if child_controller_names_authorized.count > 0
    html_options = {method: method}
    html_options[:title] = current_adminsite_admin_user.email if klasses == 'log_out'
    link = link_to(label, "#{url}?admin_menu=#{admin_menu}", html_options)
    result = raw "<li class='#{html_classes(url, nil, klasses, admin_menu, label )}'>#{link}</li>"

    content_for(:content_menu, child_menus.join("\n").html_safe )
  end
  result
end

#recognize_path(path) ⇒ Object



3
4
5
6
7
8
9
10
# File 'app/helpers/admin/adminsite_menu_helper.rb', line 3

def recognize_path(path)
  return {} if path.try(:strip).blank?
  begin
    return Adminsite::Engine.routes.recognize_path(path) # '/admin/profiles'
    return Rails.application.routes.recognize_path(path)
  rescue Exception => e
  end
end