katalyst / koi

Koi Gem
https://github.com/katalyst/koi
MIT License
8 stars 4 forks source link

Previewing PDF attachments in index tables #585

Open hasarindaKI opened 6 months ago

hasarindaKI commented 6 months ago

For more details => https://api.rubyonrails.org/v7.0.8/classes/ActiveStorage/Preview.html & https://api.rubyonrails.org/v7.0.8/classes/ActiveStorage/Blob/Representable.html

Proposed change

if value.try(:representable?)
    if value.try(:variable?)
        image_tag( @variant.nil? ? value : value.variant(@variant))
    else
        image_tag(value.blob.preview(resize_to_limit: [100, 100]))
    end
elsif value.try(:attached?)
    link_to value.blob.filename, rails_blob_path(value, disposition: :attachment)
else
    ""
end

Example using Hunter & Co. app

Screenshot 2024-03-07 at 5 19 40 pm

sfnelson commented 6 months ago

@hasarindaKI for admin tables, I don't think that we should be doing resize operations on the fly. This could be an extremely resource-heavy interaction and provides very limited value to admins.

I think this type of flow would be more appropriate:

  1. Check whether the image is representable and supports the specified variant
  2. If so, show an image preview
  3. If not, show a download link Note that the reason we're using pre-defined variants is to avoid expensive processing on page view, and shift processing to image upload instead.
hasarindaKI commented 6 months ago

Yes, agreed that we shouldn't be generating previews on the fly.

Instead of representable, we could use variable & show the image.

Note: variable is a include check in a configurable list of content types.