Sha256: a1c393eb2a7f8983bcbc38d2c657c9fcb281f3cc8495716ff4252251da65eaa6

Contents?: true

Size: 1.44 KB

Versions: 11

Compression:

Stored size: 1.44 KB

Contents

# frozen_string_literal: true

module Katalyst
  module Tables
    class HeaderCellComponent < ViewComponent::Base # :nodoc:
      include Frontend::Helper
      include HasHtmlAttributes
      include Sortable

      delegate :object_name, :collection, :sorting, to: :@table

      def initialize(table, attribute, label: nil, link: {}, **html_attributes)
        super(**html_attributes)

        @table           = table
        @attribute       = attribute
        @value           = label
        @link_attributes = link
      end

      def call
        tag.th(**html_attributes) do
          if sortable?(@attribute)
            link_to(value, sort_url(@attribute), **@link_attributes)
          else
            value
          end
        end
      end

      def value
        if !@value.nil?
          @value
        elsif object_name.present?
          translation
        else
          default_value
        end
      end

      def translation(key = "activerecord.attributes.#{object_name}.#{@attribute}")
        translate(key, default: default_value)
      end

      def default_value
        @attribute.to_s.humanize.capitalize
      end

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

      private

      def default_html_attributes
        return {} unless sorting&.supports?(collection, @attribute)

        { data: { sort: sorting.status(@attribute) } }
      end
    end
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
katalyst-tables-2.2.10 app/components/katalyst/tables/header_cell_component.rb
katalyst-tables-2.2.9 app/components/katalyst/tables/header_cell_component.rb
katalyst-tables-2.2.8 app/components/katalyst/tables/header_cell_component.rb
katalyst-tables-2.2.7 app/components/katalyst/tables/header_cell_component.rb
katalyst-tables-2.2.6 app/components/katalyst/tables/header_cell_component.rb
katalyst-tables-2.2.5 app/components/katalyst/tables/header_cell_component.rb
katalyst-tables-2.2.4 app/components/katalyst/tables/header_cell_component.rb
katalyst-tables-2.2.3 app/components/katalyst/tables/header_cell_component.rb
katalyst-tables-2.2.2 app/components/katalyst/tables/header_cell_component.rb
katalyst-tables-2.2.1 app/components/katalyst/tables/header_cell_component.rb
katalyst-tables-2.2.0 app/components/katalyst/tables/header_cell_component.rb