Module: CoreFlashToastHelper

Defined in:
app/helpers/core_flash_toast_helper.rb

Overview

Helper methods for rendering the flash information received from the server and displaying it as toast messages

Instance Method Summary collapse

Instance Method Details

#toast_containerObject

This method is abstract.

render the whole toast container if there are toast messages to render

Returns HTML - To render in the page.

Returns:

  • HTML - To render in the page



7
8
9
10
11
12
13
# File 'app/helpers/core_flash_toast_helper.rb', line 7

def toast_container
  return if flash.blank?

  (:div, class: 'toast-container position-fixed top-0 start-50 translate-middle-x mt-5') do
    flash.each { |key, value| concat(toast_flash_message(key, value)) unless 'timedout'.eql?(key) }
  end
end

#toast_flash_color(key) ⇒ Object

This method is abstract.

map the flash key to an icon

Returns String - name of the icon to render.

Returns:

  • String - name of the icon to render



36
37
38
39
40
41
42
43
44
45
# File 'app/helpers/core_flash_toast_helper.rb', line 36

def toast_flash_color(key)
  case key.downcase
  when 'alert', 'error'
    'text-danger'
  when 'warning'
    'text-warning'
  else
    'text-info'
  end
end

#toast_flash_header(key) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
# File 'app/helpers/core_flash_toast_helper.rb', line 22

def toast_flash_header(key)
  (:div, class: 'toast-header') do
    concat((:i, class: [toast_flash_icon(key), 'me-2', toast_flash_color(key)].join(' ')) {})
    concat((:div, style: ['me-auto', 'fw-medium', toast_flash_color(key)].join(' ')) { key.humanize })
    concat(tag(:button,
               class: 'btn-close',
               type: :button,
               data: { 'bs-dismiss': :toast },
               aria: { label: 'Close' }))
  end
end

#toast_flash_icon(key) ⇒ Object

This method is abstract.

map the flash key to a color scheme

Returns String - color mapped to the flash message type.

Returns:

  • String - color mapped to the flash message type



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

def toast_flash_icon(key)
  case key
  when 'alert', 'error'
    'ri-alert-fill'
  when 'warning'
    'ri-error-warning-fill'
  else
    'ri-information-fill'
  end
end

#toast_flash_message(key, value) ⇒ Object



15
16
17
18
19
20
# File 'app/helpers/core_flash_toast_helper.rb', line 15

def toast_flash_message(key, value)
  (:div, class: 'toast', role: :alert, aria: { live: :assertive, atomic: true }) do
    concat(toast_flash_header(key))
    concat((:div, class: 'toast-body') { value })
  end
end