Module: Trestle::FormatHelper

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

Instance Method Summary collapse

Instance Method Details

#autoformat_value(value, **options) ⇒ Object

Auto-formats a value based on its type.

The current implementation of this helper supports Arrays, Time/Datetime, Date, true/false values, nil, String (with optional truncation) or model instance types (using the ‘display` helper).

value - Value to format (multiple types supported) options - Options hash, usage dependent on value type. Currently supported:

:blank    - text to display for nil values (e.g. "N/A")
:truncate - passed to truncate helper for String values

Returns a HTML-safe String.



53
54
55
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
# File 'app/helpers/trestle/format_helper.rb', line 53

def autoformat_value(value, **options)
  case value
  when Array
    tag.ol(safe_join(value.map { |v|
      tag.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
      tag.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, format: :auto, **options) ⇒ Object

Formats a value, either with an explicit format (given the :format option) or automatically based on its type using the ‘autoformat_value` helper.

This helper is most commonly called when rendering content within a table cell, but is available to use from any view context.

value - Value to format format - Symbol representing format type. Currently supported:

:auto, nil - Auto-formats (default)
:currency - Formats as currency using `number_to_currency`
:tags - Formats an array of Strings as a tag list

options - Options hash to pass to ‘autoformat_value` helper

Examples

<%= format_value(123.45, format: :currency) %>
<%= format_value(article.tags, format: :tags) %>

<%= format_value(Time.current) %>
<%= format_value(nil, blank: "None") %>
<%= format_value(true) %>

Returns a HTML-safe String. Raises ArgumentError if an invalid format is given.



27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'app/helpers/trestle/format_helper.rb', line 27

def format_value(value, format: :auto, **options)
  case format
  when :auto, nil
    autoformat_value(value, **options)
  when :currency
    number_to_currency(value)
  when :tags
    tags = Array(value).map { |t| tag.span(t, class: "tag tag-primary") }
    tag.div(safe_join(tags), class: "tag-list")
  else
    raise ArgumentError, "unknown format: #{format}"
  end
end