Module: Decidim::Admin::AttributesDisplayHelper

Defined in:
decidim-admin/app/helpers/decidim/admin/attributes_display_helper.rb

Overview

Custom helpers, scoped to the admin panel.

Instance Method Summary collapse

Instance Method Details

#display_for(record, *attrs) ⇒ Object

Displays the given attributes list in a list. This is a simple `show_for` alternative, so there's no way to modify how an attribute is shown and there is no intention on adding this. It is intended to be used inside a `dl` HTML tag, so you can customize how a specific attribute has to be shown directly:

<dl>
  <%= display_for @post, :title, :body %>
  <dt>Comments number</dt>
  <dd>2</dd>
</dl>

This will render this HTML:

<dl>
  <dt>Title</dt>
  <dd>Hello, world!</dd>
  <dt>Body</dt>
  <dd>Lorem ipsum dolor sit amet...</dd>
  <dt>Comments number</dt>
  <dd>2</dd>
</dl>

record - any Ruby object that needs some attributes rendered attrs - a list of N attributes of the `record`.


33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'decidim-admin/app/helpers/decidim/admin/attributes_display_helper.rb', line 33

def display_for(record, *attrs)
  attrs.map do |attr|
    if attr.is_a?(Hash)
      attribute_name = attr.keys.first
      label = attr.values.first
    else
      label = attribute_name = attr
    end

    if record.column_for_attribute(attribute_name).type == :jsonb
      display_available_locales(record).map do |locale|
        display_label(record, label, locale) + display_value(record, attribute_name, locale)
      end.reduce(:+)
    else
      display_label(record, label) + display_value(record, attribute_name)
    end
  end.reduce(:+)
end

#display_label(record, attr, locale = nil) ⇒ Object

Holds the logic to render a label for a given attribute.


53
54
55
56
57
# File 'decidim-admin/app/helpers/decidim/admin/attributes_display_helper.rb', line 53

def display_label(record, attr, locale = nil)
  return (:dt, record.class.human_attribute_name(attr)) unless locale

  (:dt, record.class.human_attribute_name(attr) + " (#{locale})")
end