Module: FormatHelper

Included in:
DryCrud::Form::BuilderTest, DryCrud::Table::BuilderTest, FormHelperTest, TableHelperTest
Defined in:
app/helpers/format_helper.rb

Overview

Provides uniform formatting of basic data types, based on Ruby class (#f) or database column type (#format_attr). If other helpers define methods with names like ‘format_class_attr’, these methods are used for formatting.

Futher helpers standartize the layout of multiple attributes (#render_attrs), values with labels (#labeled) and simple lists.

Instance Method Summary collapse

Instance Method Details

#captionize(text, clazz = nil) ⇒ Object

Transform the given text into a form as used by labels or table headers.



68
69
70
71
72
73
74
75
76
# File 'app/helpers/format_helper.rb', line 68

def captionize(text, clazz = nil)
  text = text.to_s
  if clazz.respond_to?(:human_attribute_name)
    text_without_id = text.end_with?('_ids') ? text[0..-5].pluralize : text
    clazz.human_attribute_name(text_without_id)
  else
    text.humanize.titleize
  end
end

#f(value) ⇒ Object

Formats a basic value based on its Ruby class.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'app/helpers/format_helper.rb', line 11

def f(value)
  case value
  when Float, BigDecimal
    number_with_precision(value, precision: t('number.format.precision'),
                                 delimiter: t('number.format.delimiter'))
  when Integer
    number_with_delimiter(value, delimiter: t('number.format.delimiter'))
  when Date   then l(value)
  when Time   then "#{l(value.to_date)} #{l(value, format: :time)}"
  when true   then t('global.yes')
  when false  then t('global.no')
  when nil    then UtilityHelper::EMPTY_STRING
  else value.to_s
  end
end

#format_attr(obj, attr) ⇒ Object

Formats an arbitrary attribute of the given ActiveRecord object. If no specific format_class_attr or format_attr method is found, formats the value as follows: If the value is an associated model, renders the label of this object. Otherwise, calls format_type.



32
33
34
35
36
# File 'app/helpers/format_helper.rb', line 32

def format_attr(obj, attr)
  format_with_helper(obj, attr) ||
    format_association(obj, attr) ||
    format_type(obj, attr)
end

#labeled(label, content = nil, &block) ⇒ Object

Renders an arbitrary content with the given label. Used for uniform presentation.



62
63
64
65
# File 'app/helpers/format_helper.rb', line 62

def labeled(label, content = nil, &block)
  content = capture(&block) if block_given?
  render('shared/labeled', label: label, content: content)
end

#labeled_attr(obj, attr) ⇒ Object

Renders the formatted content of the given attribute with a label.



56
57
58
# File 'app/helpers/format_helper.rb', line 56

def labeled_attr(obj, attr)
  labeled(captionize(attr, obj.class), format_attr(obj, attr))
end

#render_attrs(obj, *attrs) ⇒ Object

Renders a list of attributes with label and value for a given object. Optionally surrounded with a div.



49
50
51
52
53
# File 'app/helpers/format_helper.rb', line 49

def render_attrs(obj, *attrs)
  (:dl, attrs, class: 'dl-horizontal') do |a|
    labeled_attr(obj, a)
  end
end

#simple_list(items, **ul_options) ⇒ Object

Renders a simple unordered list, which will simply render all passed items or yield them to your block.



41
42
43
44
45
# File 'app/helpers/format_helper.rb', line 41

def simple_list(items, **ul_options)
  (:ul, items, **ul_options) do |item|
    tag.li(block_given? ? yield(item) : f(item))
  end
end