Module: Dorsale::TextHelper

Includes:
ActionView::Helpers::SanitizeHelper, ActionView::Helpers::TextHelper
Included in:
AllHelpers, Flyboy::Roadmap
Defined in:
app/helpers/dorsale/text_helper.rb

Instance Method Summary collapse

Instance Method Details

#date(d) ⇒ Object



35
36
37
38
# File 'app/helpers/dorsale/text_helper.rb', line 35

def date(d)
  return if d.nil?
  I18n.l(d)
end

#euros(n) ⇒ Object



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

def euros(n)
  return if n.nil?

  number(n) + ""
end

#hours(n) ⇒ Object



40
41
42
43
44
45
46
47
# File 'app/helpers/dorsale/text_helper.rb', line 40

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, text_or_options = nil, options = {}) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'app/helpers/dorsale/text_helper.rb', line 56

def info(object, attribute, text_or_options = nil, options = {})
  if text_or_options.is_a?(Hash)
    text    = nil
    options = text_or_options
  else
    text = text_or_options
  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?(Class) ? object : object.class
  object_type = klass.to_s.split("::").last.underscore

  value = text
  value = object.public_send(attribute)     if value.nil?
  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 }

  (tag, class: "info") do
    [html_label, separator, html_value].join.html_safe
  end
end

#number(n) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'app/helpers/dorsale/text_helper.rb', line 18

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)
end

#percentage(n) ⇒ Object



12
13
14
15
16
# File 'app/helpers/dorsale/text_helper.rb', line 12

def percentage(n)
  return if n.nil?

  number(n) + " %"
end

#text2html(str) ⇒ Object



49
50
51
52
53
54
# File 'app/helpers/dorsale/text_helper.rb', line 49

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

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