refactor(gallery): use gallery component in gallery demande

This commit is contained in:
Eric Leroy-Terquem 2024-09-09 10:33:31 +02:00
parent 664ef63e74
commit 3560d73b58
No known key found for this signature in database
GPG key ID: 53D8FAECEF207605
4 changed files with 23 additions and 27 deletions

View file

@ -4,14 +4,17 @@ class Attachment::GalleryItemComponent < ApplicationComponent
include GalleryHelper include GalleryHelper
attr_reader :attachment attr_reader :attachment
def initialize(attachment:) def initialize(attachment:, gallery_demande: false)
@attachment = attachment @attachment = attachment
@gallery_demande = gallery_demande
end end
def blob def blob
attachment.blob attachment.blob
end end
def gallery_demande? = @gallery_demande
def libelle def libelle
attachment.record.class.in?([Champs::PieceJustificativeChamp, Champs::TitreIdentiteChamp]) ? attachment.record.libelle : 'Pièce jointe au message' attachment.record.class.in?([Champs::PieceJustificativeChamp, Champs::TitreIdentiteChamp]) ? attachment.record.libelle : 'Pièce jointe au message'
end end

View file

@ -5,12 +5,14 @@
= image_tag(representation_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 } .fr-btn.fr-btn--tertiary.fr-btn--icon-left.fr-icon-eye{ role: :button }
Visualiser Visualiser
.champ-libelle - if !gallery_demande?
= libelle.truncate(25) .champ-libelle
= render Attachment::ShowComponent.new(attachment: attachment, truncate: true) = libelle.truncate(25)
= render Attachment::ShowComponent.new(attachment:, truncate: true, new_tab: gallery_demande?)
- else - else
.thumbnail .thumbnail
= image_tag('apercu-indisponible.png') = image_tag('apercu-indisponible.png')
.champ-libelle - if !gallery_demande?
= libelle.truncate(25) .champ-libelle
= render Attachment::ShowComponent.new(attachment: attachment, truncate: true) = libelle.truncate(25)
= render Attachment::ShowComponent.new(attachment:, truncate: true, new_tab: gallery_demande?)

View file

@ -2,25 +2,7 @@
- if profile == 'instructeur' - if profile == 'instructeur'
.gallery-items-list .gallery-items-list
- champ.piece_justificative_file.attachments.with_all_variant_records.each do |attachment| - champ.piece_justificative_file.attachments.with_all_variant_records.each do |attachment|
.gallery-item = render Attachment::GalleryItemComponent.new(attachment:, gallery_demande: true)
- blob = attachment.blob
- 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: "#{champ.libelle} -- #{sanitize(blob.filename.to_s)}" do
.thumbnail
= image_tag(preview_url_for(attachment), loading: :lazy)
.fr-btn.fr-btn--tertiary.fr-btn--icon-left.fr-icon-eye{ role: :button }
= 'Visualiser'
- elsif displayable_image?(blob)
= link_to image_url(blob_url(attachment)), title: "#{champ.libelle} -- #{sanitize(blob.filename.to_s)}", data: { src: blob.url }, class: 'gallery-link' do
.thumbnail
= image_tag(variant_url_for(attachment), loading: :lazy)
.fr-btn.fr-btn--tertiary.fr-btn--icon-left.fr-icon-eye{ role: :button }
= 'Visualiser'
- else
.thumbnail
= image_tag('apercu-indisponible.png')
= render Attachment::ShowComponent.new(attachment:, new_tab: true, truncate: true)
- else - else
%ul %ul
- champ.piece_justificative_file.attachments.each do |attachment| - champ.piece_justificative_file.attachments.each do |attachment|

View file

@ -7,8 +7,9 @@ RSpec.describe Attachment::GalleryItemComponent, type: :component do
let(:types_de_champ_public) { [{ type: :piece_justificative }] } let(:types_de_champ_public) { [{ type: :piece_justificative }] }
let(:dossier) { create(:dossier, :with_populated_champs, :en_construction, procedure:) } let(:dossier) { create(:dossier, :with_populated_champs, :en_construction, procedure:) }
let(:filename) { attachment.blob.filename.to_s } let(:filename) { attachment.blob.filename.to_s }
let(:gallery_demande) { false }
let(:component) { described_class.new(attachment: attachment) } let(:component) { described_class.new(attachment: attachment, gallery_demande:) }
subject { render_inline(component).to_html } subject { render_inline(component).to_html }
@ -23,6 +24,14 @@ RSpec.describe Attachment::GalleryItemComponent, type: :component do
expect(subject).to have_link(filename) expect(subject).to have_link(filename)
expect(component.title).to eq("#{libelle} -- #{filename}") expect(component.title).to eq("#{libelle} -- #{filename}")
end end
context "when gallery item is in page Demande" do
let(:gallery_demande) { true }
it "does not display libelle" do
expect(subject).not_to have_text(libelle)
end
end
end end
context "when attachment is from a commentaire" do context "when attachment is from a commentaire" do