Sha256: 03f61178efe995b30fe7074cadbe6862fdaa8a05796306051aa8722fbec0afcb

Contents?: true

Size: 1.7 KB

Versions: 33

Compression:

Stored size: 1.7 KB

Contents

module ExpressAdmin
  module Components
    module Presenters
      class DefinitionList < ExpressTemplates::Components::Configurable
        include ExpressTemplates::Components::Capabilities::Resourceful

        tag :dl

        list_types = {}
        list_types[:array] = {description: "List of fields on the current resource",
                              options: -> {resource.columns.map(&:name)}}
        list_types[:hash] = {description: "List of terms and definitions."}

        has_argument :list, "A list of things to define, presented as <label>: <definition>.",
                            as: :list, type: list_types

        contains -> {
          definitions.each do |label, content|
            dt { label }
            dd { content }
          end
        }

        def definitions
          if config[:list].kind_of?(Array)
            definitions_from_array(config[:list])
          elsif config[:list].kind_of?(Hash)
            definitions_from_hash(config[:list])
          end
        end

        def definitions_from_hash(hash)
          processed = hash.map do |k,v|
            value = if v.kind_of? Symbol
              resource.send(v)
            elsif v.respond_to?(:call)
              v.call(resource).html_safe
            else
              v
            end
            [promptify(k), value]
          end
          Hash[processed]
        end

        def definitions_from_array(fields)
          Hash[fields.map {|field| ["#{field.to_s.titleize}:", "{{resource.#{field}}}"]}]
        end

        private
          def promptify(k)
            if k.kind_of?(Symbol)
              k.to_s.promptify
            else
              k.to_s
            end
          end

      end
    end
  end
end

Version data entries

33 entries across 33 versions & 1 rubygems

Version Path
express_admin-1.7.25 app/components/express_admin/definition_list.rb
express_admin-1.7.24 app/components/express_admin/definition_list.rb
express_admin-1.7.23 app/components/express_admin/definition_list.rb
express_admin-1.7.22 app/components/express_admin/definition_list.rb
express_admin-1.7.21 app/components/express_admin/definition_list.rb
express_admin-1.7.20 app/components/express_admin/definition_list.rb
express_admin-1.7.19 app/components/express_admin/definition_list.rb
express_admin-1.7.18 app/components/express_admin/definition_list.rb
express_admin-1.7.17 app/components/express_admin/definition_list.rb
express_admin-1.7.16 app/components/express_admin/definition_list.rb
express_admin-1.7.16.pre1 app/components/express_admin/definition_list.rb
express_admin-1.7.15 app/components/express_admin/definition_list.rb
express_admin-1.7.14 app/components/express_admin/definition_list.rb
express_admin-1.7.13 app/components/express_admin/definition_list.rb
express_admin-1.7.12 app/components/express_admin/definition_list.rb
express_admin-1.7.11 app/components/express_admin/definition_list.rb
express_admin-1.7.10 app/components/express_admin/definition_list.rb
express_admin-1.7.9 app/components/express_admin/definition_list.rb
express_admin-1.7.8 app/components/express_admin/definition_list.rb
express_admin-1.7.7 app/components/express_admin/definition_list.rb