Sha256: 81c6a8f257c26afc8c60840ade0319731c86ab57f5ff06865fc02a61ccc778bc

Contents?: true

Size: 1.34 KB

Versions: 43

Compression:

Stored size: 1.34 KB

Contents

# frozen_string_literal: true

module Koi
  module SummaryList
    class Base < ViewComponent::Base
      include Katalyst::HtmlAttributes

      define_html_attribute_methods :term_attributes, default: {}
      define_html_attribute_methods :description_attributes, default: {}

      def initialize(model, attribute, label: nil, skip_blank: true)
        super()

        @model      = model
        @attribute  = attribute
        @label      = label
        @skip_blank = skip_blank
      end

      def call
        tag.dt(attribute_name, **term_attributes) +
          tag.dd(content_or_value, **description_attributes)
      end

      def render?
        raw_value.present? || !@skip_blank
      end

      def attribute_name
        @label&.dig(:text) || @model.class.human_attribute_name(@attribute)
      end

      def attribute_value
        raw_value.to_s
      end

      def raw_value
        @model.public_send(@attribute)
      end

      def inspect
        "#<#{self.class.name} #{@attribute.inspect}>"
      end

      # Convenience method for rendering the content of the cell
      # <% dl.text(:name) { |cell| tag.em(cell) } %>
      # => <dt>Name</dt><dd><em>Jamie Banks</em></dd>
      def to_s
        attribute_value
      end

      private

      def content_or_value
        content? ? content : attribute_value
      end
    end
  end
end

Version data entries

43 entries across 43 versions & 1 rubygems

Version Path
katalyst-koi-4.15.1 app/components/koi/summary_list/base.rb
katalyst-koi-4.15.0 app/components/koi/summary_list/base.rb
katalyst-koi-4.14.3 app/components/koi/summary_list/base.rb
katalyst-koi-4.14.2 app/components/koi/summary_list/base.rb
katalyst-koi-4.14.1 app/components/koi/summary_list/base.rb
katalyst-koi-4.14.0 app/components/koi/summary_list/base.rb
katalyst-koi-4.13.2 app/components/koi/summary_list/base.rb
katalyst-koi-4.13.0 app/components/koi/summary_list/base.rb
katalyst-koi-4.12.6 app/components/koi/summary_list/base.rb
katalyst-koi-4.12.5 app/components/koi/summary_list/base.rb
katalyst-koi-4.12.4 app/components/koi/summary_list/base.rb
katalyst-koi-4.12.3 app/components/koi/summary_list/base.rb
katalyst-koi-4.12.2 app/components/koi/summary_list/base.rb
katalyst-koi-4.12.1 app/components/koi/summary_list/base.rb
katalyst-koi-4.12.0 app/components/koi/summary_list/base.rb
katalyst-koi-4.11.2 app/components/koi/summary_list/base.rb
katalyst-koi-4.11.1 app/components/koi/summary_list/base.rb
katalyst-koi-4.11.0 app/components/koi/summary_list/base.rb
katalyst-koi-4.10.3 app/components/koi/summary_list/base.rb
katalyst-koi-4.10.2 app/components/koi/summary_list/base.rb