Sha256: 32a537cb7f5dd0bb97a6e70c4a92cc6b678e67cc9696896c396e4fb8132588a5

Contents?: true

Size: 1.86 KB

Versions: 10

Compression:

Stored size: 1.86 KB

Contents

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

        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 words?(v)
                make_in_words(resource, v)
              elsif v.kind_of? Symbol
                resource.send(v)
              elsif v.respond_to?(:call)
                v.call(resource).to_s.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

10 entries across 10 versions & 1 rubygems

Version Path
express_admin-2.0.0.b app/components/express_admin/definition_list.rb
express_admin-2.0.0.a app/components/express_admin/definition_list.rb
express_admin-1.8.1 app/components/express_admin/definition_list.rb
express_admin-1.8.0 app/components/express_admin/definition_list.rb
express_admin-1.7.32 app/components/express_admin/definition_list.rb
express_admin-1.7.31 app/components/express_admin/definition_list.rb
express_admin-1.7.30 app/components/express_admin/definition_list.rb
express_admin-1.7.29 app/components/express_admin/definition_list.rb
express_admin-1.7.28 app/components/express_admin/definition_list.rb
express_admin-1.7.27 app/components/express_admin/definition_list.rb