Module: PureAdmin::MenuHelper

Defined in:
app/helpers/pure_admin/menu_helper.rb

Overview

Helper methods to assist in building the Pure Admin menu.

Instance Method Summary collapse

Instance Method Details

#current_menu_item?(url) ⇒ Boolean

Check if the menu item is currently active

Parameters:

  • url (String)

    the menu item’s URL.

Returns:

  • (Boolean)


88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'app/helpers/pure_admin/menu_helper.rb', line 88

def current_menu_item?(url)
  return false unless url

  begin
    menu_item_path = URI.parse(url).try(:path)
  rescue
    menu_item_path = nil
  end

  return false unless menu_item_path

  # only match roots on exact match
  return request.original_fullpath == menu_item_path
end

#current_parent?(url) ⇒ Boolean

Check if the menu item is currently active. Is active if it is parent or current.

Parameters:

  • url (String)

    the menu item’s URL.

Returns:

  • (Boolean)


106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'app/helpers/pure_admin/menu_helper.rb', line 106

def current_parent?(url)
  return false unless url

  begin
    menu_item_path = URI.parse(url).try(:path)
  rescue
    menu_item_path = nil
  end

  return false unless menu_item_path

  # match all other items to start of original_fullpath
  return request.original_fullpath =~ /^#{Regexp.escape(menu_item_path)}(\/|$)/
end

Renders a pure menu wrapper to the view.

Parameters:

  • options (Hash) (defaults to: {})

    a container for options to be passed to menus and menu lists.

  • options (:menu_html) (defaults to: {})

    (Hash) all options that can be passed to content_tag are respected here.

  • options (:list_html) (defaults to: {})

    (Hash) all options that can be passed to content_tag are respected here.

Yields:

  • The contents of the menu panel



11
12
13
14
15
16
17
18
19
20
21
# File 'app/helpers/pure_admin/menu_helper.rb', line 11

def menu(options = {}, &block)
  menu_html = options.delete(:menu_html) || {}
  menu_html[:class] = merge_html_classes('pure-menu', menu_html[:class])

  list_html = options.delete(:list_html) || {}
  list_html[:class] = merge_html_classes('pure-menu-list', list_html[:class])

  (:nav, menu_html) do
    (:ul, '', list_html, &block)
  end
end

Renders a “menu item” to the view.

Parameters:

  • options (Hash) (defaults to: {})

    all options that can be passed to content_tag are respected here.



51
52
53
54
55
56
57
58
59
# File 'app/helpers/pure_admin/menu_helper.rb', line 51

def menu_item(options = {}, &block)
  options[:class] = merge_html_classes('pure-menu-item', options[:class])
  options[:class] << 'current' if options.delete(:current)

  condition = options.key?(:if) ? options.delete(:if) : true
  condition = !options.delete(:unless) if options.key?(:unless)

  (:li, capture(&block), options) if condition
end

Renders a “menu item” and “menu link” to the view.

Parameters:

  • name (String, Symbol) (defaults to: nil)
  • url (String, Array) (defaults to: nil)
  • options (Hash) (defaults to: nil)

    a container for options to be passed to menu items and links.

  • options (:item_html) (defaults to: nil)

    (Hash) all options that can be passed to content_tag are respected here.

  • options (:link_html) (defaults to: nil)

    (Hash) all options that can be passed to link_to are respected here.



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'app/helpers/pure_admin/menu_helper.rb', line 68

def menu_item_and_link(name = nil, url = nil, options = nil, &block)
  options, url, name = url, name, capture(&block) if block_given?
  options ||= {}

  item_html = options.delete(:item_html) || {}
  item_html[:class] = merge_html_classes('pure-menu-item', item_html[:class])
  item_html[:if] = options[:if] if options.key?(:if)
  item_html[:unless] = options[:unless] if options.key?(:unless)
  item_html[:current] = true if current_menu_item?(url)

  link_html = options.delete(:link_html) || {}

  menu_item(item_html) do
    menu_link(name, url, link_html)
  end
end

Renders a “menu link” to the view.

Parameters:

  • name (String, Symbol) (defaults to: nil)
  • url (String, Array) (defaults to: nil)
  • options (Hash) (defaults to: nil)

    all options that can be passed to link_to are respected here.



37
38
39
40
41
42
43
44
45
46
# File 'app/helpers/pure_admin/menu_helper.rb', line 37

def menu_link(name = nil, url = nil, options = nil, &block)
  options, url, name = url, name, capture(&block) if block_given?
  options ||= {}

  name = name.to_s.titleize unless block_given? || name.respond_to?(:titleize)

  options[:class] = merge_html_classes('pure-menu-link', options[:class])

  url.present? ? link_to(name, url, options) : (:span, name, options)
end

Renders a pure sub-menu to the view.

Parameters:

  • options (Hash) (defaults to: {})

    all options that can be passed to content_tag are respected here.

Yields:

  • The contents of the menu panel



27
28
29
30
# File 'app/helpers/pure_admin/menu_helper.rb', line 27

def sub_menu(options = {}, &block)
  options[:class] = merge_html_classes('pure-menu-list sub-menu', options[:class])
  (:ul, '', options, &block)
end