Sha256: 84d28d4897fcdfa9d69a8f05e35762973baff3da92496957e3bb4c5ca6181375

Contents?: true

Size: 1.26 KB

Versions: 1

Compression:

Stored size: 1.26 KB

Contents

# frozen_string_literal: true

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

      delegate :object_name, :sort, to: :@table

      def initialize(table, attribute, label: nil, **html_options)
        super(**html_options)

        @table        = table
        @attribute    = attribute
        @value        = label
      end

      def call
        content = if @table.sort&.supports?(@table.collection, @attribute)
                    sort_link(value) # writes to html_options
                  else
                    value
                  end

        tag.th(content, **@html_options)
      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

      private

      def sort_link(content)
        (@html_options["data"] ||= {})["sort"] = sort.status(@attribute)
        link_to(content, sort_url_for(sort: sort.toggle(@attribute)))
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
katalyst-tables-2.0.0 app/components/katalyst/tables/header_cell_component.rb