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.

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

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


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

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(tag(:span, class: 'nav-link-toggle'))
    end)
    concat((:div, class: nav_css.join(' '), id: name.to_s, &block))
  end
end


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

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
# File 'app/helpers/core_nav_bar_helper.rb', line 66

def nav_link(name, action_path, badge_count: 0, classes: [])
  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(name, badge_count: badge_count))
  end
end


105
106
107
108
109
110
111
112
113
# File 'app/helpers/core_nav_bar_helper.rb', line 105

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


101
102
103
# File 'app/helpers/core_nav_bar_helper.rb', line 101

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

Parameters:

  • name (String)
    • Name of the badge

  • options (Hash) (defaults to: {})
    • Options for the badge, values



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[:new_badge_count]
  return if badge_count.blank? || badge_count <= 0

  (:span, class: 'badge', title: "#{badge_count} New #{name.pluralize}") { badge_count.to_s }
end