Sha256: d5185ccf46b6030c9336604bae00d152848500928483f35d0d801dc43bcec1bd

Contents?: true

Size: 1.54 KB

Versions: 8

Compression:

Stored size: 1.54 KB

Contents

# frozen_string_literal: true

module Katalyst
  module Tables
    class HeaderCellComponent < ViewComponent::Base # :nodoc:
      include Frontend::Helper
      include Katalyst::HtmlAttributes
      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

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

      private

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

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

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
katalyst-tables-2.6.0 app/components/katalyst/tables/header_cell_component.rb
katalyst-tables-2.6.0.beta app/components/katalyst/tables/header_cell_component.rb
katalyst-tables-2.5.0 app/components/katalyst/tables/header_cell_component.rb
katalyst-tables-2.4.0 app/components/katalyst/tables/header_cell_component.rb
katalyst-tables-2.3.1 app/components/katalyst/tables/header_cell_component.rb
katalyst-tables-2.3.0 app/components/katalyst/tables/header_cell_component.rb
katalyst-tables-2.2.12 app/components/katalyst/tables/header_cell_component.rb
katalyst-tables-2.2.11 app/components/katalyst/tables/header_cell_component.rb