class ActiveAdminAddons::ListBuilder

Public Instance Methods

hash_to_html(_value, _label) click to toggle source
# 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}:&nbsp".html_safe)
        context.concat(context.content_tag(:span) do
          context.content_tag(:i, _value)
        end)
      end
    end
  end)
end
list?(_data) click to toggle source
# 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) click to toggle source
# 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() click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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