Module: CoreMenuHelper

Defined in:
app/helpers/core_menu_helper.rb

Overview

Helpful methods for menu bar on the side

Instance Method Summary collapse

Instance Method Details

#expandable_menu_item(name, classes: [], icon_name: '', badge_count: 0, &block) ⇒ Object



56
57
58
59
60
61
62
63
64
# File 'app/helpers/core_menu_helper.rb', line 56

def expandable_menu_item(name, classes: [], icon_name: '', badge_count: 0, &block)
  classes << 'menu-item'
  classes << 'open' if classes.include?('active')
  (:li, class: classes.join(' ')) do
    classes << 'menu-toggle'
    concat(menu_link(name, '#', classes: ['menu-toggle'], badge_count: badge_count, icon_name: icon_name))
    concat((:ul, class: 'menu-sub') { yield block })
  end
end


66
67
68
69
70
71
72
# File 'app/helpers/core_menu_helper.rb', line 66

def main_menu(&block)
  (:div, id: 'main-menu') do
    concat((:ul, class: 'menu-inner py-1') do
      yield block
    end)
  end
end
This method is abstract.

Add a badge to the current element



75
76
77
78
79
# File 'app/helpers/core_menu_helper.rb', line 75

def menu_count_badge(name, badge_count)
  (:span,
              class: 'badge badge-center bg-white text-primary rounded-pill ms-1',
              title: "#{badge_count} New #{name.to_s.pluralize}") { badge_count.to_s }
end
This method is abstract.

Determines which css attributes should be assigned to the current menu, options are

Parameters:

  • names (Array, String, Symbol)
    • make active it the current controller matches

  • read_only (Boolean) (defaults to: false)
    • the menu is greyed out and disabled

  • enabled (Boolean) (defaults to: true)
    • a lock icon appears

  • visible (Boolean) (defaults to: true)
    • If this menu should be visible at all



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'app/helpers/core_menu_helper.rb', line 14

def menu_css(names, read_only: false, enabled: true, visible: true)
  states = []
  states << 'read-only' if read_only
  states << 'locked' unless enabled
  states << 'd-none' unless visible
  case names
  when String
    states << 'active' if names.eql?(controller.controller_name)
  when Array
    states << 'active' if names.include?(controller.controller_name)
  else
    states << 'active' if names.to_s.eql?(controller.controller_name)
  end
  states
end
This method is abstract.

Produce a new navigational link The name and action path are required, additional options are

  • icon_name - name of the icon to add to the link

  • classes - css styles to apply to the navigational link

  • new_count_badge - Content for badge



49
50
51
52
53
54
# File 'app/helpers/core_menu_helper.rb', line 49

def menu_item(name, action_path, classes: [], icon_name: '', badge_count: 0)
  classes << 'menu-item'
  (:li, class: classes.join(' ')) do
    menu_link(name, action_path, badge_count: badge_count, icon_name: icon_name)
  end
end


34
35
36
37
38
39
40
41
# File 'app/helpers/core_menu_helper.rb', line 34

def menu_link(name, action_path, classes: [], badge_count: 0, icon_name: '')
  classes << 'menu-link'
  (:a, href: action_path, class: classes.join(' ')) do
    concat(menu_remix_icon(icon_name)) if icon_name.present?
    concat(menu_name(name))
    concat(menu_count_badge(name, badge_count)) if badge_count.positive?
  end
end


30
31
32
# File 'app/helpers/core_menu_helper.rb', line 30

def menu_name(name)
  (:div, data: { i18n: name }) { name }
end