Class: ActiveAdminAddons::ListBuilder

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

Instance Attribute Summary

Attributes inherited from CustomBuilder

#args, #block, #context, #model

Instance Method Summary collapse

Methods inherited from CustomBuilder

builder_method_name, create_view_methods, #initialize

Constructor Details

This class inherits a constructor from ActiveAdminAddons::CustomBuilder

Instance Method Details

#hash_to_html(_value, _label) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/activeadmin_addons/addons/list_builder.rb', line 56

def hash_to_html(_value, _label)
  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

#list?(_data) ⇒ Boolean

Returns:

  • (Boolean)


20
21
22
# File 'lib/activeadmin_addons/addons/list_builder.rb', line 20

def list?(_data)
  _data.is_a?(Array) || _data.is_a?(Hash)
end

#localized_value(model, attribute) ⇒ Object



24
25
26
# File 'lib/activeadmin_addons/addons/list_builder.rb', line 24

def localized_value(model, attribute)
  I18n.t("addons_list.#{model.class.name.underscore}.#{attribute}.#{@level.join('_')}")
end

#renderObject



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

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

  return if data.nil?

  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)

  @level = []
  render_list(data)
end

#render_array(_data) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/activeadmin_addons/addons/list_builder.rb', line 28

def render_array(_data)
  context.(options[:list_type]) do
    _data.each do |value|
      @level.push(value)
      if list? value
        value = render_list(value)
      elsif !!options[:localize]
        value = localized_value(model, attribute)
      end
      @level.pop
      context.concat(context.(:li, value))
    end
  end
end

#render_hash(_data) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/activeadmin_addons/addons/list_builder.rb', line 43

def render_hash(_data)
  context.(options[:list_type]) do
    _data.keys.each do |key|
      @level.push(key)
      label = !!options[:localize] ? localized_value(model, attribute) : key
      value = _data[key]
      value = render_list(value) if list? value
      @level.pop
      hash_to_html(value, label)
    end
  end
end

#render_list(_data) ⇒ Object



16
17
18
# File 'lib/activeadmin_addons/addons/list_builder.rb', line 16

def render_list(_data)
  _data.is_a?(Array) ? render_array(_data) : render_hash(_data)
end