Sha256: e33b7c792b9c9a6055e7997054a4e707f4ea8fc92067ee028ec72def6634d593

Contents?: true

Size: 1.18 KB

Versions: 1

Compression:

Stored size: 1.18 KB

Contents

# frozen_string_literal: true

module Katalyst
  module Tables
    class BodyCellComponent < ViewComponent::Base # :nodoc:
      include Katalyst::HtmlAttributes

      attr_reader :record

      def initialize(table, record, attribute, heading: false, **html_attributes)
        super(**html_attributes)

        @table     = table
        @record    = record
        @attribute = attribute
        @type      = heading ? :th : :td
      end

      def before_render
        # fallback if no content block is given
        with_content(rendered_value) unless content?
      end

      def call
        content # ensure content is set before rendering options

        content_tag(@type, content, **html_attributes)
      end

      # @return the object for this row.
      def object
        @record
      end

      def value
        @record.public_send(@attribute)
      end

      def rendered_value
        value.to_s
      end

      def to_s
        value.to_s
      end

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

      # Backwards compatibility with tables 1.0
      alias_method :options, :html_attributes=
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
katalyst-tables-3.0.0.beta1 app/components/katalyst/tables/body_cell_component.rb