Class: ActiveAdminAddons::ListBuilder
Instance Attribute Summary
#args, #block, #context, #model
Instance Method Summary
collapse
builder_method_name, create_view_methods, #initialize
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.content_tag(:li) do
if _value.blank?
context.content_tag(:span, _label)
else
context.content_tag(:span) do
context.concat("#{_label}: ".html_safe)
context.concat(context.content_tag(:span) do
context.content_tag(:i, _value)
end)
end
end
end)
end
|
#list?(_data) ⇒ 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
|
#render ⇒ Object
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.content_tag(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.content_tag(: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.content_tag(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
|