Sha256: 59ef813bbd70b0288617fdf162563f8631f32981d3f54ff91c05aee020015b69

Contents?: true

Size: 1.52 KB

Versions: 27

Compression:

Stored size: 1.52 KB

Contents

# frozen_string_literal: true

module Koi
  module Tables
    module Cells
      # Shows an attachment
      #
      # The value is expected to be an ActiveStorage attachment
      #
      # If it is representable, shows as a image tag using the specified variant.
      #
      # Otherwise shows as a link to download.
      class AttachmentComponent < Katalyst::Tables::CellComponent
        def initialize(variant:, **)
          super(**)

          @variant = variant
        end

        def rendered_value
          representation
        end

        def representation
          if value.try(:variable?) && named_variant.present?
            image_tag(value.variant(@variant))
          elsif value.try(:attached?)
            filename.to_s
          else
            ""
          end
        end

        def filename
          value.blob.filename
        end

        # Utility for accessing the path Rails provides for retrieving the
        # attachment for use in cells. Example:
        #    <% row.attachment :file do |cell| %>
        #       <%= link_to "Download", cell.internal_path %>
        #    <% end %>
        def internal_path
          rails_blob_path(value, disposition: :attachment)
        end

        private

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

        # Find the reflective variant by name (i.e. :thumb by default)
        def named_variant
          record.attachment_reflections[@column.to_s].named_variants[@variant.to_sym]
        end
      end
    end
  end
end

Version data entries

27 entries across 27 versions & 1 rubygems

Version Path
katalyst-koi-4.14.1 app/components/koi/tables/cells/attachment_component.rb
katalyst-koi-4.14.0 app/components/koi/tables/cells/attachment_component.rb
katalyst-koi-4.13.2 app/components/koi/tables/cells/attachment_component.rb
katalyst-koi-4.13.0 app/components/koi/tables/cells/attachment_component.rb
katalyst-koi-4.12.6 app/components/koi/tables/cells/attachment_component.rb
katalyst-koi-4.12.5 app/components/koi/tables/cells/attachment_component.rb
katalyst-koi-4.12.4 app/components/koi/tables/cells/attachment_component.rb
katalyst-koi-4.12.3 app/components/koi/tables/cells/attachment_component.rb
katalyst-koi-4.12.2 app/components/koi/tables/cells/attachment_component.rb
katalyst-koi-4.12.1 app/components/koi/tables/cells/attachment_component.rb
katalyst-koi-4.12.0 app/components/koi/tables/cells/attachment_component.rb
katalyst-koi-4.11.2 app/components/koi/tables/cells/attachment_component.rb
katalyst-koi-4.11.1 app/components/koi/tables/cells/attachment_component.rb
katalyst-koi-4.11.0 app/components/koi/tables/cells/attachment_component.rb
katalyst-koi-4.10.3 app/components/koi/tables/cells/attachment_component.rb
katalyst-koi-4.10.2 app/components/koi/tables/cells/attachment_component.rb
katalyst-koi-4.10.1 app/components/koi/tables/cells/attachment_component.rb
katalyst-koi-4.10.0 app/components/koi/tables/cells/attachment_component.rb
katalyst-koi-4.9.5 app/components/koi/tables/cells/attachment_component.rb
katalyst-koi-4.9.4 app/components/koi/tables/cells/attachment_component.rb