Module: Agilibox::TextHelper

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

Instance Method Summary collapse

Instance Method Details

#boolean_icon(bool) ⇒ Object



46
47
48
49
50
51
52
53
# File 'app/helpers/agilibox/text_helper.rb', line 46

def boolean_icon(bool)
  return if bool.nil?

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

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

#currency(n, u) ⇒ Object



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

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

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

#date(d, *args) ⇒ Object



42
43
44
# File 'app/helpers/agilibox/text_helper.rb', line 42

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

#euros(n) ⇒ Object



6
7
8
# File 'app/helpers/agilibox/text_helper.rb', line 6

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

#hours(n) ⇒ Object



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

def hours(n)
  return if n.nil?

  number = number_with_precision(n, precision: 2)
  text   = I18n.t("datetime.prompts.hour").downcase
  text   = text.pluralize if n > 1
  "#{number} #{text}"
end

#info(object, attribute, value_or_options = nil, options = {}) ⇒ Object

rubocop:disable Metrics/MethodLength



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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'app/helpers/agilibox/text_helper.rb', line 78

def info(object, attribute, value_or_options = nil, options = {})
  if value_or_options.nil?
    value = object.public_send(attribute)
  elsif value_or_options.is_a?(Hash)
    value = object.public_send(attribute)
    options = value_or_options
  else
    value = value_or_options
  end

  if value.blank?
    value = options[:default]
    return if value == :hide
  end

  label       = options[:label]     || object.t(attribute)
  tag         = options[:tag]       || :div
  separator   = options[:separator] || " : "
  helper      = options[:helper]
  i18n_key    = "#{object.class.to_s.tableize.singularize}/#{attribute}"
  nested      = I18n.t("activerecord.attributes.#{i18n_key}").is_a?(Hash)
  klass       = object.is_a?(Module) ? object : object.class
  object_type = klass.to_s.split("::").last.underscore

  value = t("yes")                          if value == true
  value = t("no")                           if value == false
  value = object.t("#{attribute}.#{value}") if nested
  value = send(helper, value)               if helper
  value = number(value)                     if value.is_a?(Numeric)
  value = l(value)                          if value.is_a?(Time)
  value = l(value)                          if value.is_a?(Date)
  value = value.to_s

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

  if value.blank?
    container_css_class = "info blank"
  else
    container_css_class = "info"
  end

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

#lf2br(str) ⇒ Object



71
72
73
74
75
# File 'app/helpers/agilibox/text_helper.rb', line 71

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

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

#number(n) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'app/helpers/agilibox/text_helper.rb', line 25

def number(n)
  return if n.nil?

  opts = {}

  if n.class.to_s =~ /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(" ", "\u00A0")
end

#percentage(n) ⇒ Object



19
20
21
22
23
# File 'app/helpers/agilibox/text_helper.rb', line 19

def percentage(n)
  return if n.nil?

  (number(n) + " %").tr(" ", "\u00A0")
end

#tags(object) ⇒ Object

rubocop:enable Metrics/MethodLength



128
129
130
131
132
133
134
135
136
# File 'app/helpers/agilibox/text_helper.rb', line 128

def tags(object)
  return "" if object.tag_list.empty?

  object.tag_list.map { |tag|
    (:span, class: "tag label label-primary") {
      "#{icon :tag} #{tag}".html_safe
    }
  }.join(" ").html_safe
end

#text2html(str) ⇒ Object



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

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

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