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

#errors_for(errors, scope: '') ⇒ String

Render errors

Parameters:

  • model (Class)

    resource

Returns:

  • (String)

    with error tags



130
131
132
# File 'app/helpers/application_helper.rb', line 130

def errors_for(errors, scope: '')
  errors.map { |e| "#{t_field(e.attribute, scope)} #{e.message}" }
end

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

make a div for the font-awesome icons

Parameters:

  • fa_style (String)

    style of icon

  • span_style (String) (defaults to: nil)

    other style for container

  • style (String) (defaults to: false)

    extra css params

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

    inside container

  • tooltip (String) (defaults to: false)

    on mouseover

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

    other mouseover tooltip

Returns:

  • (String)


17
18
19
20
21
22
23
24
25
26
27
# File 'app/helpers/application_helper.rb', line 17

def fas_icon(fa_style, span_style: nil, style: false, text: '', tooltip: false, title: '')
   = tag.i('', class: "fas fa-#{fa_style}", aria: { hidden: 'true' })
  span = if tooltip.present?
           tag.span(, class: "icon #{span_style}", style: style, title: title, data: { tooltip: tooltip })
         else
           tag.span(, class: "icon #{span_style}", style: style, title: title)
         end
  return span if text.blank?

  span + tag.span(text)
end

#form_errors_for(resource) ⇒ String

Render errors form

Parameters:

  • model (Class)

    resource

Returns:

  • (String)

    with error tags



121
122
123
124
125
# File 'app/helpers/application_helper.rb', line 121

def form_errors_for(resource)
  errors_for(resource.errors, scope: resource.class.table_name.singularize) if resource.errors.present?
rescue
  ''
end

#format_qta(qta: 0, unit: 'pz', with_unit: true) ⇒ String

Format qta into Integer if unit is equal pz

Parameters:

  • qta (Integer) (defaults to: 0)
  • unit (String) (defaults to: 'pz')

    default ā€˜pzā€™

  • with_unit (Bool) (defaults to: true)

    default true

Returns:

  • (String)


94
95
96
97
98
99
100
101
# File 'app/helpers/application_helper.rb', line 94

def format_qta(qta: 0, unit: 'pz', with_unit: true)
  new_qta = if unit == 'pz'
              qta.try(:to_i)
            else
              strip_trailing_zero(qta)
            end
  "#{new_qta}#{unit if with_unit}"
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



54
55
56
# File 'app/helpers/application_helper.rb', line 54

def l_date(obj = nil)
  obj.present? ? l(obj.try(:to_date), format: :date) : '-'
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



40
41
42
# File 'app/helpers/application_helper.rb', line 40

def l_long(obj = nil)
  l(obj.try(:to_time), 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



47
48
49
# File 'app/helpers/application_helper.rb', line 47

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

#strip_trailing_zero(number, precision: 2) ⇒ String

Remove zeros

Parameters:

  • number (Integer)

Returns:

  • (String)


106
107
108
109
# File 'app/helpers/application_helper.rb', line 106

def strip_trailing_zero(number, precision: 2)
  # number.to_s.sub(/\.?0+$/, '')
  number_with_precision(number, precision: precision, strip_insignificant_zeros: true)
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)


33
34
35
# File 'app/helpers/application_helper.rb', line 33

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

#t_field(field_label = nil, obj = '') ⇒ String

Localize a fieldName if #obj is present

Parameters:

  • field_label (Text) (defaults to: nil)
  • obj (Text) (defaults to: '')

Returns:

  • (String)

    localized



62
63
64
65
66
67
68
69
70
71
72
73
# File 'app/helpers/application_helper.rb', line 62

def t_field(field_label = nil, obj = '')
  return '' if field_label.blank?

  case obj
  when Class
    t(field_label, scope: "activerecord.attributes.#{obj.class}")
  when String
    t(field_label, scope: "activerecord.attributes.#{obj}")
  else
    t(field_label, default: field_label)
  end
end

#t_formula(text = '') ⇒ String

Convert string into Math formula Require MathJax.js

Parameters:

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

Returns:

  • (String)

    localized



79
80
81
# File 'app/helpers/application_helper.rb', line 79

def t_formula(text = '')
  text.present? ? tag.span("$#{ActionView::Base.full_sanitizer.sanitize(text)}$", class: 'is-inline is-formula') : '-'
end

#t_value(text = '') ⇒ String

Parameters:

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

Returns:

  • (String)


85
86
87
# File 'app/helpers/application_helper.rb', line 85

def t_value(text = '')
  text.presence || '-'
end

#to_plain_text(body) ⇒ String

Remove HTML tags

Parameters:

  • text (String)

Returns:

  • (String)


114
115
116
# File 'app/helpers/application_helper.rb', line 114

def to_plain_text(body)
  Nokogiri::HTML(body).text.strip
end