Module: CoreNavBarHelper

Defined in:
app/helpers/core_nav_bar_helper.rb

Overview

Helpful methods for navigation bar on the side

Instance Method Summary collapse

Instance Method Details

This method is abstract.

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

Returns Array - a collection of css parameters based on current controllers.



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'app/helpers/core_nav_bar_helper.rb', line 38

def nav_bar_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

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

<li>read-only -  the menu is greyed out and disabled</li>
<li>locked - a lock icon appears</li>
<li>active - if the current controller matches either the name or names</li>

</ul>



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'app/helpers/core_nav_bar_helper.rb', line 16

def nav_css(names, read_only = false, feature_enabled = true, feature_visible = true)
  states = []
  states << 'read-only' if read_only
  states << 'locked' unless feature_enabled
  states << 'hidden' unless feature_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.join(' ')
end


84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'app/helpers/core_nav_bar_helper.rb', line 84

def nav_item_group(name, classes: [], badge_count: 0, &block)
  lic = [classes, 'nav-item'].flatten.compact.join(' ')
  ac = [classes, 'nav-link'].flatten.compact.join(' ')
  open_close = 'false'
  nav_css = %w[nav nav-vertical collapse]
  if classes.include?('active')
    open_close = 'true'
    nav_css << 'show'
  end
  (:div, class: lic) do
    concat((:a, class: ac, data: { bs_toggle: :collapse }, role: :button, href: "##{name}", aria: { expanded: open_close, controls: :nav_group_settings }) do
      concat((:span, class: 'nav-link-icon d-md-none d-lg-inline-block') { svg_icon(name) })
      concat((:span, class: 'nav-link-title') { nav_menu_title(name) })
      concat(new_count_badge(nav_menu_title(name), badge_count: badge_count))
      concat(tag(:span, class: 'nav-link-toggle'))
    end)
    concat((:div, class: nav_css.join(' '), id: name.to_s, &block))
  end
end


77
78
79
80
81
82
# File 'app/helpers/core_nav_bar_helper.rb', line 77

def nav_item_link(name, action_path, badge_count: 0, classes: [])
  lic = [classes, 'nav-item',].compact.join(' ')
  (:div, class: lic) do
    concat(nav_link(name, action_path, badge_count: badge_count))
  end
end


66
67
68
69
70
71
72
73
74
75
# File 'app/helpers/core_nav_bar_helper.rb', line 66

def nav_link(name, action_path, badge_count: 0, classes: [])
  return if classes.include?('d-none')

  ac = [classes, 'truncate', 'nav-link'].compact.join(' ')
  (:a, href: action_path, class: ac) do
    concat((:span, class: 'nav-link-icon d-md-none d-lg-inline-block') { svg_icon(name) })
    concat((:span, class: 'nav-link-title') { nav_menu_title(name) })
    concat(new_count_badge(nav_menu_title(name), badge_count: badge_count))
  end
end


108
109
110
111
112
113
114
115
116
# File 'app/helpers/core_nav_bar_helper.rb', line 108

def nav_localized(type, name)
  if I18n.exists?("nav.#{type}.#{name}")
    I18n.t("nav.#{type}.#{name}")
  else
    name.to_s.titleize
  end
rescue StandardError
  name
end


104
105
106
# File 'app/helpers/core_nav_bar_helper.rb', line 104

def nav_menu_title(name)
  nav_localized(:titles, name)
end

#new_count_badge(name, options = {}) ⇒ Object

This method is abstract.

Add a badge to the current element



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

def new_count_badge(name, options = {})
  return if options.blank?

  badge_count = options[:badge_count] || 0
  return if badge_count.blank? || badge_count.to_i <= 0

  (:span, class: 'badge z-3 me-2 bg-green-lt', title: "#{badge_count} #{name.pluralize}") { badge_count.to_s }
end