Sha256: 0420a9874106cec460ecd5f926071183a5f0ae75df1e6ee4ebc55cc05df7b6bd

Contents?: true

Size: 1.55 KB

Versions: 27

Compression:

Stored size: 1.55 KB

Contents

# frozen_string_literal: true

module Koi
  module Tables
    module Cells
      # Displays a link to the record
      # The link text is the value of the attribute
      class LinkComponent < Katalyst::Tables::CellComponent
        def initialize(url:, default_url:, link:, **)
          super(**)

          with_content_wrapper(WrapperComponent.new(cell: self, url:, default_url:, **link))
        end

        private

        def default_html_attributes
          { class: "type-link" }
        end

        class WrapperComponent < ViewComponent::Base
          include Katalyst::HtmlAttributes

          delegate :record, :column, :value, :row, to: :@cell

          def initialize(cell:, url:, default_url:, **)
            super(**)

            @cell = cell
            @url = url
            @default_url = default_url
          end

          def call
            if row.header?
              content
            else
              link_to(content, @default_url ? default_url : url, **html_attributes)
            end
          end

          def url
            case @url
            when Symbol
              # helpers are not available until the component is rendered
              @url = helpers.public_send(@url, record)
            when Proc
              @url = @url.call(record)
            else
              @url
            end
          end

          def default_url
            if value.is_a?(ApplicationRecord)
              [:admin, value]
            else
              [:admin, record]
            end
          end
        end
      end
    end
  end
end

Version data entries

27 entries across 27 versions & 1 rubygems

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