Module: ApplicationHelper

Includes:
Pagy::Frontend
Defined in:
app/helpers/application_helper.rb

Overview

This helper contain the methods shared for all views

  • include Pagy::Frontend

Instance Method Summary collapse

Instance Method Details

#fas_icon(fa_style, span_style: nil, style: false, text: '', tooltip: false) ⇒ Object

make a div for the font-awesome icons


10
11
12
13
14
15
16
17
# File 'app/helpers/application_helper.rb', line 10

def fas_icon(fa_style, span_style: nil, style: false, text: '', tooltip: false)
  # "<span class='icon #{span_style}' #{"style='#{style}'" if style.present?} data-tooltip=\"#{tooltip if tooltip.present?}\" ><i class='fas fa-#{fa_style}' aria-hidden='true'></i></span> #{"<span>#{text}</span>" if text.present?}".html_safe
  tag_i = (:i, '', class: "fas fa-#{fa_style}", aria: { hidden: 'true' })
  span = (:span, tag_i, class: "icon #{span_style}", style: style, data: { tooltip: tooltip })
  return span if text.blank?

  span + (:span, text)
end

#l_date(obj = nil) ⇒ String

Localize a DateTime with format :date if #obj is present

Parameters:

  • obj (DateTime, Date) (defaults to: nil)

Returns:

  • (String)

    localized and formatted date


64
65
66
# File 'app/helpers/application_helper.rb', line 64

def l_date(obj = nil)
  l(obj, format: :date) if obj.present?
end

#l_long(obj = nil) ⇒ String

Localize a DateTime with format :long if #obj is present

Parameters:

  • obj (DateTime) (defaults to: nil)

Returns:

  • (String)

    localized and formatted date


50
51
52
# File 'app/helpers/application_helper.rb', line 50

def l_long(obj = nil)
  l(obj, format: :long) if obj.present?
end

#l_time(obj = nil) ⇒ String

Localize a DateTime with format :time if #obj is present

Parameters:

  • obj (DateTime) (defaults to: nil)

Returns:

  • (String)

    localized and formatted date


57
58
59
# File 'app/helpers/application_helper.rb', line 57

def l_time(obj = nil)
  l(obj, format: :time) if obj.present?
end

#notificationsObject

map of flash and rub notify() for each flash


20
21
22
# File 'app/helpers/application_helper.rb', line 20

def notifications
  flash.to_h.extract!('success', 'notice', 'warning', 'alert').map { |type, text| notify(text, type: type) }.join
end

#notify(text, type: 'alert', timeout: 3000, hidden: true) ⇒ String

Returns Make a div with for the notification.

Parameters:

  • text (String)

    text of notification

  • type (String) (defaults to: 'alert')

    type of notification, default: 'alert'

  • timeout (String) (defaults to: 3000)

    set the timeout for javascript action, default: 3000

  • hidden (Boolean) (defaults to: true)

    set visibility class, default true

Returns:

  • (String)

    Make a div with for the notification


35
36
37
# File 'app/helpers/application_helper.rb', line 35

def notify(text, type: 'alert', timeout: 3000, hidden: true)
  (:div, text.to_s, class: "notification is-#{type} #{'is-hidden' if hidden}", data: { controller: 'noty', noty_type: type, noty_timeout: timeout })
end

#notify_status(status = {}) ⇒ Object

map of status and run notify() for each status

Parameters:

  • status (Hash) (defaults to: {})

26
27
28
# File 'app/helpers/application_helper.rb', line 26

def notify_status(status = {})
  status.map { |k, v| notify(v, type: k) }.join.html_safe if status.present?
end

#t_enum(list = {}, scope = '') ⇒ List

generate a list for a select from an enum

Parameters:

  • list (Hash) (defaults to: {})

    , enum option list, default {}

  • scope (String) (defaults to: '')

    scope of localization, default ''

Returns:

  • (List)

43
44
45
# File 'app/helpers/application_helper.rb', line 43

def t_enum(list = {}, scope = '')
  list.map { |k, _| [t(k, scope: scope), k] }
end