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
-
#errors_for(errors, scope: '') ⇒ String
Render errors.
-
#fas_icon(fa_style, span_style: nil, style: false, text: '', tooltip: false, title: '') ⇒ String
make a div for the font-awesome icons.
-
#form_errors_for(resource) ⇒ String
Render errors form.
-
#format_qta(qta: 0, unit: 'pz', with_unit: true) ⇒ String
Format qta into Integer if unit is equal pz.
-
#l_date(obj = nil) ⇒ String
Localize a DateTime with format :date if #obj is present.
-
#l_long(obj = nil) ⇒ String
Localize a DateTime with format :long if #obj is present.
-
#l_time(obj = nil) ⇒ String
Localize a DateTime with format :time if #obj is present.
-
#strip_trailing_zero(number, precision: 2) ⇒ String
Remove zeros.
-
#t_enum(list = {}, scope = '') ⇒ List
generate a list for a select from an enum.
-
#t_field(field_label = nil, obj = '') ⇒ String
Localize a fieldName if #obj is present.
-
#t_formula(text = '') ⇒ String
Convert string into Math formula Require MathJax.js.
- #t_value(text = '') ⇒ String
-
#to_plain_text(body) ⇒ String
Remove HTML tags.
Instance Method Details
#errors_for(errors, scope: '') ⇒ String
Render errors
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.}" } end |
#fas_icon(fa_style, span_style: nil, style: false, text: '', tooltip: false, title: '') ⇒ String
make a div for the font-awesome icons
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: '') content_tag_i = tag.i('', class: "fas fa-#{fa_style}", aria: { hidden: 'true' }) span = if tooltip.present? tag.span(content_tag_i, class: "icon #{span_style}", style: style, title: title, data: { tooltip: tooltip }) else tag.span(content_tag_i, 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
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
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
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
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
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
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
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
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
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
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
114 115 116 |
# File 'app/helpers/application_helper.rb', line 114 def to_plain_text(body) Nokogiri::HTML(body).text.strip end |