Class: ListBuilder

Inherits:
ActiveAdminAddons::CustomBuilder show all
Defined in:
lib/activeadmin_addons/addons/list.rb

Instance Attribute Summary

Attributes inherited from ActiveAdminAddons::CustomBuilder

#args, #block, #context, #model

Instance Method Summary collapse

Methods inherited from ActiveAdminAddons::CustomBuilder

#initialize, render

Constructor Details

This class inherits a constructor from ActiveAdminAddons::CustomBuilder

Instance Method Details

#localized_value(key, model, attribute) ⇒ Object



13
14
15
# File 'lib/activeadmin_addons/addons/list.rb', line 13

def localized_value(key, model, attribute)
  I18n.t("addons_list.#{model.class.name.underscore}.#{attribute}.#{key}")
end

#renderObject



3
4
5
6
7
8
9
10
11
# File 'lib/activeadmin_addons/addons/list.rb', line 3

def render
  options[:localize] = options.fetch(:localize, false)
  options[:list_type] = options.fetch(:list_type, :ul)

  raise 'invalid list type (ul, ol)' unless [:ul, :ol].include?(options[:list_type])
  raise "list must be Array or Hash" if !data.is_a?(Hash) && !data.is_a?(Array)

  data.is_a?(Array) ? render_array : render_hash
end

#render_arrayObject



17
18
19
20
21
22
23
24
# File 'lib/activeadmin_addons/addons/list.rb', line 17

def render_array
  context.(options[:list_type]) do
    data.each do |value|
      value = localized_value(value, model, attribute) if !!options[:localize]
      context.concat(context.(:li, value))
    end
  end
end

#render_hashObject



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/activeadmin_addons/addons/list.rb', line 26

def render_hash
  context.(options[:list_type]) do
    data.keys.each do |key|
      label =  !!options[:localize] ? localized_value(key, model, attribute) : key
      value = data[key]
      context.concat(context.(:li) do
        if value.blank?
          context.(:span, label)
        else
          context.(:span) do
            context.concat("#{label}:&nbsp".html_safe)
            context.concat(context.(:span) do
              context.(:i, value)
            end)
          end
        end
      end)
    end
  end
end