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

#flash_color(key) ⇒ Object



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

def flash_color(key)
  case key.to_sym
  when :notice then 'text-success'
  when :alert then 'text-danger'
  when :error then 'text-danger'
  when :warning then 'text-warning'
  else 'text-info'
  end
end

#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



17
18
19
20
21
22
23
# File 'app/helpers/core_flash_toast_helper.rb', line 17

def toast_container
  return if flash.blank?

  (:div, class: 'toast-container position-fixed top-25 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



44
45
46
47
48
49
50
51
# File 'app/helpers/core_flash_toast_helper.rb', line 44

def toast_flash_color(key)
  case key.to_sym
  when :notice then 'text-success'
  when :alert, :error then 'text-danger'
  when :warning then 'text-warning'
  else 'text-info'
  end
end

#toast_flash_header(key) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
# File 'app/helpers/core_flash_toast_helper.rb', line 32

def toast_flash_header(key)
  (:div, class: 'toast-header') do
    concat(svg_icon(key, classes: [toast_flash_color(key), 'me-2'].join(' '), size: 24))
    concat((:div, class: ['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_message(key, value) ⇒ Object



25
26
27
28
29
30
# File 'app/helpers/core_flash_toast_helper.rb', line 25

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