Class: Koi::Tables::Body::AttachmentComponent

Inherits:
Koi::Tables::BodyCellComponent show all
Defined in:
app/components/koi/tables/body.rb

Overview

Shows an attachment

The value is expected to be an ActiveStorage attachment

If it is representable, shows as a image tag using a default variant named :thumb.

Otherwise shows as a link to download.

Instance Method Summary collapse

Methods inherited from Koi::Tables::BodyCellComponent

#before_render, #to_s

Constructor Details

#initialize(table, record, attribute, variant: :thumb, **options) ⇒ AttachmentComponent

Returns a new instance of AttachmentComponent.



186
187
188
189
190
# File 'app/components/koi/tables/body.rb', line 186

def initialize(table, record, attribute, variant: :thumb, **options)
  super(table, record, attribute, **options)

  @variant = variant
end

Instance Method Details

#filenameObject



206
207
208
# File 'app/components/koi/tables/body.rb', line 206

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


215
216
217
# File 'app/components/koi/tables/body.rb', line 215

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

#rendered_valueObject



192
193
194
# File 'app/components/koi/tables/body.rb', line 192

def rendered_value
  representation
end

#representationObject



196
197
198
199
200
201
202
203
204
# File 'app/components/koi/tables/body.rb', line 196

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