refactor(gallery): extract representation_url_for method

This commit is contained in:
Eric Leroy-Terquem 2024-09-09 09:46:59 +02:00
parent bae752f1aa
commit 2882af43aa
No known key found for this signature in database
GPG key ID: 53D8FAECEF207605
3 changed files with 24 additions and 3 deletions

View file

@ -2,7 +2,7 @@
- if displayable_pdf?(blob)
= link_to blob.url, id: blob.id, data: { iframe: true, src: blob.url }, class: 'gallery-link', type: blob.content_type, title: title do
.thumbnail
= image_tag(preview_url_for(attachment), loading: :lazy)
= image_tag(representation_url_for(attachment), loading: :lazy)
.fr-btn.fr-btn--tertiary.fr-btn--icon-left.fr-icon-eye{ role: :button }
Visualiser
.champ-libelle
@ -12,13 +12,12 @@
- elsif displayable_image?(blob)
= link_to image_url(blob_url(attachment)), title: title, data: { src: blob.url }, class: 'gallery-link' do
.thumbnail
= image_tag(variant_url_for(attachment), loading: :lazy)
= image_tag(representation_url_for(attachment), loading: :lazy)
.fr-btn.fr-btn--tertiary.fr-btn--icon-left.fr-icon-eye{ role: :button }
Visualiser
.champ-libelle
= libelle.truncate(25)
= render Attachment::ShowComponent.new(attachment: attachment, truncate: true)
- else
.thumbnail
= image_tag('apercu-indisponible.png')

View file

@ -9,6 +9,12 @@ module GalleryHelper
blob.variable? && blob.content_type.in?(AUTHORIZED_IMAGE_TYPES)
end
def representation_url_for(attachment)
return variant_url_for(attachment) if displayable_image?(attachment.blob)
preview_url_for(attachment) if displayable_pdf?(attachment.blob)
end
def preview_url_for(attachment)
preview = attachment.preview(resize_to_limit: [400, 400])
preview.image.attached? ? preview.processed.url : 'pdf-placeholder.png'

View file

@ -74,4 +74,20 @@ RSpec.describe GalleryHelper, type: :helper do
it { is_expected.to eq("pdf-placeholder.png") }
end
end
describe ".representation_url_for" do
subject { representation_url_for(attachment) }
context "when attachment is an image with no variant" do
let(:file) { fixture_file_upload('spec/fixtures/files/logo_test_procedure.png', 'image/png') }
it { is_expected.to eq("apercu-indisponible.png") }
end
context "when attachment is a pdf with no preview" do
let(:file) { fixture_file_upload('spec/fixtures/files/RIB.pdf', 'application/pdf') }
it { is_expected.to eq("pdf-placeholder.png") }
end
end
end