Class: Koi::Tables::Cells::AttachmentComponent

Inherits:
Katalyst::Tables::CellComponent
  • Object
show all
Defined in:
app/components/koi/tables/cells/attachment_component.rb

Overview

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.

Instance Method Summary collapse

Constructor Details

#initialize(variant:) ⇒ AttachmentComponent

Returns a new instance of AttachmentComponent.



14
15
16
17
18
# File 'app/components/koi/tables/cells/attachment_component.rb', line 14

def initialize(variant:, **)
  super(**)

  @variant = variant
end

Instance Method Details

#filenameObject



34
35
36
# File 'app/components/koi/tables/cells/attachment_component.rb', line 34

def filename
  value.blob.filename
end

#internal_pathObject

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 %>


43
44
45
# File 'app/components/koi/tables/cells/attachment_component.rb', line 43

def internal_path
  rails_blob_path(value, disposition: :attachment)
end

#rendered_valueObject



20
21
22
# File 'app/components/koi/tables/cells/attachment_component.rb', line 20

def rendered_value
  representation
end

#representationObject



24
25
26
27
28
29
30
31
32
# File 'app/components/koi/tables/cells/attachment_component.rb', line 24

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