Module: Trestle::FormatHelper

Defined in:
app/helpers/trestle/format_helper.rb

Instance Method Summary collapse

Instance Method Details

#autoformat_value(value, options = {}) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'app/helpers/trestle/format_helper.rb', line 22

def autoformat_value(value, options={})
  case value
  when Array
    (:ol, safe_join(value.map { |v|
      (:li, v.is_a?(Array) ? v : autoformat_value(v, options)) },
    "\n"))
  when Time, DateTime
    timestamp(value)
  when Date
    datestamp(value)
  when TrueClass, FalseClass
    status_tag(icon("fa fa-check"), :success) if value
  when NilClass
    blank = options.key?(:blank) ? options[:blank] : I18n.t("admin.format.blank")
    if blank.respond_to?(:call)
      instance_exec(&blank)
    else
      (:span, blank, class: "blank")
    end
  when String
    if value.html_safe? || options[:truncate] == false
      value
    else
      truncate(value, options[:truncate] || {})
    end
  when ->(value) { value.respond_to?(:id) }
    display(value)
  else
    value
  end
end

#format_value(value, options = {}) ⇒ Object



3
4
5
6
7
8
9
# File 'app/helpers/trestle/format_helper.rb', line 3

def format_value(value, options={})
  if options.key?(:format)
    format_value_from_options(value, options)
  else
    autoformat_value(value, options)
  end
end

#format_value_from_options(value, options = {}) ⇒ Object



11
12
13
14
15
16
17
18
19
20
# File 'app/helpers/trestle/format_helper.rb', line 11

def format_value_from_options(value, options={})
  case options[:format]
  when :currency
    number_to_currency(value)
  when :tags
    safe_join(Array(value).map { |tag| (:span, tag, class: "tag") })
  else
    value
  end
end