From 3bbcda9c043ccd4813ab2c6e64505f393d389112 Mon Sep 17 00:00:00 2001 From: Eric Leroy-Terquem Date: Tue, 2 Jul 2024 18:45:41 +0200 Subject: [PATCH] refactor(dossier): refactor pieces jointes --- .../instructeurs/dossiers_controller.rb | 17 +++++++- .../concerns/blob_image_processor_concern.rb | 10 ++++- .../dossiers/pieces_jointes.html.haml | 43 +++---------------- 3 files changed, 31 insertions(+), 39 deletions(-) diff --git a/app/controllers/instructeurs/dossiers_controller.rb b/app/controllers/instructeurs/dossiers_controller.rb index 1605c4de1..ef1773d41 100644 --- a/app/controllers/instructeurs/dossiers_controller.rb +++ b/app/controllers/instructeurs/dossiers_controller.rb @@ -364,9 +364,24 @@ module Instructeurs def pieces_jointes @dossier = current_instructeur.dossiers.find(params[:dossier_id]) - @champs_with_pieces_jointes = @dossier + + champs_attachments_and_libelles = @dossier .champs .filter { _1.class.in?([Champs::PieceJustificativeChamp, Champs::TitreIdentiteChamp]) } + .flat_map do |c| + c.piece_justificative_file.map do |attachment| + [attachment, c.libelle] + end + end + + commentaires_attachments_and_libelles = @dossier + .commentaires + .map(&:piece_jointe) + .map(&:attachments) + .flatten + .map { [_1, 'Messagerie'] } + + @attachments_and_libelles = champs_attachments_and_libelles + commentaires_attachments_and_libelles end private diff --git a/app/models/concerns/blob_image_processor_concern.rb b/app/models/concerns/blob_image_processor_concern.rb index 4ab57509b..d69e380f5 100644 --- a/app/models/concerns/blob_image_processor_concern.rb +++ b/app/models/concerns/blob_image_processor_concern.rb @@ -8,11 +8,19 @@ module BlobImageProcessorConcern end def representation_required? - attachments.any? { _1.record.class == Champs::TitreIdentiteChamp || _1.record.class == Champs::PieceJustificativeChamp || _1.record.class == Commentaire } + from_champ? || from_messagerie? end private + def from_champ? + attachments.any? { _1.record.class == Champs::TitreIdentiteChamp || _1.record.class == Champs::PieceJustificativeChamp } + end + + def from_messagerie? + attachments.any? { _1.record.class == Commentaire } + end + def watermark_required? attachments.any? { _1.record.class == Champs::TitreIdentiteChamp } end diff --git a/app/views/instructeurs/dossiers/pieces_jointes.html.haml b/app/views/instructeurs/dossiers/pieces_jointes.html.haml index 03f333ad8..97ab75c55 100644 --- a/app/views/instructeurs/dossiers/pieces_jointes.html.haml +++ b/app/views/instructeurs/dossiers/pieces_jointes.html.haml @@ -4,63 +4,32 @@ .fr-container .gallery.gallery-pieces-jointes{ "data-controller": "lightbox" } - - @champs_with_pieces_jointes.each do |champ| - - champ.piece_justificative_file.with_all_variant_records.each do |attachment| - .gallery-item - - 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} -- #{blob.filename}" 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 - .champ-libelle - = champ.libelle.truncate(25) - = render Attachment::ShowComponent.new(attachment: attachment, truncate: true) - - - elsif displayable_image?(blob) - = link_to image_url(blob_url(attachment)), title: "#{champ.libelle} -- #{blob.filename}", 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 - .champ-libelle - = champ.libelle.truncate(25) - = render Attachment::ShowComponent.new(attachment: attachment, truncate: true) - - - else - .thumbnail - = image_tag('apercu-indisponible.png') - .champ-libelle - = champ.libelle.truncate(25) - = render Attachment::ShowComponent.new(attachment: attachment, truncate: true) - - - @dossier.commentaires.map(&:piece_jointe).map(&:attachments).flatten.each do |attachment| + - @attachments_and_libelles.each do |attachment, libelle| .gallery-item - 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: "Messagerie -- #{blob.filename}" do + = link_to blob.url, id: blob.id, data: { iframe: true, src: blob.url }, class: 'gallery-link', type: blob.content_type, title: "#{libelle} -- #{blob.filename}" 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 .champ-libelle - Messagerie + = libelle.truncate(25) = render Attachment::ShowComponent.new(attachment: attachment, truncate: true) - elsif displayable_image?(blob) - = link_to image_url(blob_url(attachment)), title: "Messagerie -- #{blob.filename}", data: { src: blob.url }, class: 'gallery-link' do + = link_to image_url(blob_url(attachment)), title: "#{libelle} -- #{blob.filename}", 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 .champ-libelle - Messagerie + = libelle.truncate(25) = render Attachment::ShowComponent.new(attachment: attachment, truncate: true) - else .thumbnail = image_tag('apercu-indisponible.png') .champ-libelle - Messagerie + = libelle.truncate(25) = render Attachment::ShowComponent.new(attachment: attachment, truncate: true)