Module: BMC::TextHelper

Includes:
ActionView::Helpers::NumberHelper, ActionView::Helpers::SanitizeHelper, ActionView::Helpers::TextHelper
Included in:
AllHelpers
Defined in:
app/helpers/bmc/text_helper.rb

Instance Method Summary collapse

Instance Method Details

#boolean_icon(bool) ⇒ Object



54
55
56
57
58
59
60
61
# File 'app/helpers/bmc/text_helper.rb', line 54

def boolean_icon(bool)
  return if bool.nil?

  return fa_s(:check, style: "color: green") if bool == true
  return fa_s(:times, style: "color: red")   if bool == false

  raise "#{bool} is not a boolean"
end

#currency(n, u) ⇒ Object



18
19
20
21
22
23
24
25
# File 'app/helpers/bmc/text_helper.rb', line 18

def currency(n, u)
  return if n.nil?

  I18n.t("number.currency.format.format")
    .gsub("%n", number(n))
    .gsub("%u", u)
    .tr(" ", nbsp)
end

#date(d, **args) ⇒ Object



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

def date(d, **args)
  I18n.l(d, **args) unless d.nil?
end

#euros(n) ⇒ Object



14
15
16
# File 'app/helpers/bmc/text_helper.rb', line 14

def euros(n)
  currency(n, "")
end

#fma(object, attribute, value: nil, default: nil, helper: nil) ⇒ Object

format model attribute



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'app/helpers/bmc/text_helper.rb', line 110

def fma(object, attribute, value: nil, default: nil, helper: nil)
  value = object.public_send(attribute) if value.nil?
  value = default if value != false && value.blank?

  i18n_scope = object.model_name.instance_variable_get(:@klass).i18n_scope
  i18n_key = "#{object.model_name.i18n_key}/#{attribute}"
  nested   = I18n.t("#{i18n_scope}.attributes.#{i18n_key}", default: "").is_a?(Hash)

  value = object.tv(attribute) if nested
  value = send(helper, value)  if helper
  value = t("yes")             if value == true
  value = t("no")              if value == false
  value = number(value)        if value.is_a?(Numeric)
  value = date(value)          if value.is_a?(Date) || value.is_a?(Time)

  value.to_s.presence
end

#info(object, attribute, value: nil, default: nil, helper: nil, tag: :div, separator: " : ", label: object.t(attribute)) ⇒ Object

rubocop:disable Metrics/ParameterLists



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'app/helpers/bmc/text_helper.rb', line 76

def info( # rubocop:disable Metrics/ParameterLists
  object,
  attribute,
  value: nil,
  default: nil,
  helper: nil,
  tag: :div,
  separator: " : ",
  label: object.t(attribute)
)
  if default == :hide
    value = fma(object, attribute, value:, helper:)
    return if value.nil?
  end

  value = fma(object, attribute, value:, default:, helper:)

  separator   = " :<br/>".html_safe if separator == :br
  klass       = object.is_a?(Module) ? object : object.class
  object_type = klass.to_s.split("::").last.underscore

  html_label     = self.tag.strong(class: "info-label") { label }
  span_css_class = "info-value #{object_type}-#{attribute}"
  html_value     = self.tag.span(class: span_css_class) { value }
  separator_html = self.tag.span(class: "info-separator") { separator }

  container_css_class = ["info", ("blank" if value.blank?)]

  (tag, class: container_css_class) do
    [html_label, separator_html, html_value].sum("".html_safe)
  end
end

#lf2br(str) ⇒ Object



70
71
72
73
74
# File 'app/helpers/bmc/text_helper.rb', line 70

def lf2br(str)
  return if str.to_s.blank?

  str.delete("\r").gsub("\n", "<br />").html_safe
end

#nbsp(text = :no_argument) ⇒ Object



6
7
8
9
10
11
12
# File 'app/helpers/bmc/text_helper.rb', line 6

def nbsp(text = :no_argument)
  if text == :no_argument
    "\u00A0"
  else
    text.to_s.gsub(" ", nbsp)
  end
end

#number(n) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'app/helpers/bmc/text_helper.rb', line 33

def number(n)
  return if n.nil?

  opts = {}

  if n.class.to_s.match?(/Float|Decimal/i)
    opts[:precision] = 2
  else
    opts[:precision] = 0
  end

  opts[:delimiter] = I18n.t("number.format.delimiter")
  opts[:separator] = I18n.t("number.format.separator")

  number_with_precision(n, opts).tr(" ", nbsp)
end

#percentage(n) ⇒ Object



27
28
29
30
31
# File 'app/helpers/bmc/text_helper.rb', line 27

def percentage(n)
  return if n.nil?

  number(n) + nbsp + "%"
end

#text2html(str) ⇒ Object



63
64
65
66
67
68
# File 'app/helpers/bmc/text_helper.rb', line 63

def text2html(str)
  return if str.to_s.blank?

  str = str.delete("\r").strip
  strip_tags(str).gsub("\n", "<br />").html_safe
end