From 9503a363e513446c3706a6a5b3466aad9e708f6c Mon Sep 17 00:00:00 2001 From: clemkeirua Date: Wed, 20 Nov 2019 11:44:04 +0100 Subject: [PATCH 1/2] 4378 Ajout des PJ de la messagerie dans le zip des PJs du dossier --- app/lib/active_storage/downloadable_file.rb | 6 +++--- app/services/pieces_justificatives_service.rb | 9 +++++++-- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/app/lib/active_storage/downloadable_file.rb b/app/lib/active_storage/downloadable_file.rb index 3ef733e8d..d1841d7cc 100644 --- a/app/lib/active_storage/downloadable_file.rb +++ b/app/lib/active_storage/downloadable_file.rb @@ -13,10 +13,10 @@ class ActiveStorage::DownloadableFile def self.create_list_from_dossier(dossier) pjs = PiecesJustificativesService.liste_pieces_justificatives(dossier) - pjs.map do |pj| + pjs.map do |piece_justificative| [ - ActiveStorage::DownloadableFile.new(pj.piece_justificative_file), - pj.piece_justificative_file.filename.to_s + ActiveStorage::DownloadableFile.new(piece_justificative), + piece_justificative.filename.to_s ] end end diff --git a/app/services/pieces_justificatives_service.rb b/app/services/pieces_justificatives_service.rb index cf834fe92..c58997acc 100644 --- a/app/services/pieces_justificatives_service.rb +++ b/app/services/pieces_justificatives_service.rb @@ -1,17 +1,21 @@ class PiecesJustificativesService def self.liste_pieces_justificatives(dossier) + pjs_commentaires = dossier.commentaires + .map(&:piece_jointe) + .filter(&:attached?) + champs_blocs_repetables = dossier.champs .filter { |c| c.type_champ == TypeDeChamp.type_champs.fetch(:repetition) } .flat_map(&:champs) - champs_pieces_justificatives_with_attachments( + pjs_commentaires + champs_pieces_justificatives_with_attachments( champs_blocs_repetables + dossier.champs ) end def self.pieces_justificatives_total_size(dossier) liste_pieces_justificatives(dossier) - .sum { |pj| pj.piece_justificative_file.byte_size } + .sum(&:byte_size) end def self.serialize_types_de_champ_as_type_pj(procedure) @@ -48,5 +52,6 @@ class PiecesJustificativesService champs .filter { |c| c.type_champ == TypeDeChamp.type_champs.fetch(:piece_justificative) } .filter { |pj| pj.piece_justificative_file.attached? } + .map(&:piece_justificative_file) end end From 789fcf72ca792b17b3846e224c9add3ba80ea4d9 Mon Sep 17 00:00:00 2001 From: clemkeirua Date: Wed, 20 Nov 2019 15:54:53 +0100 Subject: [PATCH 2/2] =?UTF-8?q?ajout=20d'un=20test=20automatis=C3=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- spec/factories/commentaire.rb | 2 +- spec/lib/active_storage/downloadable_file_spec.rb | 14 ++++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/spec/factories/commentaire.rb b/spec/factories/commentaire.rb index 360a4b05e..6e5594d3f 100644 --- a/spec/factories/commentaire.rb +++ b/spec/factories/commentaire.rb @@ -9,7 +9,7 @@ FactoryBot.define do end trait :with_file do - file { Rack::Test::UploadedFile.new("./spec/fixtures/files/logo_test_procedure.png", 'image/png') } + piece_jointe { Rack::Test::UploadedFile.new("./spec/fixtures/files/logo_test_procedure.png", 'image/png') } end end end diff --git a/spec/lib/active_storage/downloadable_file_spec.rb b/spec/lib/active_storage/downloadable_file_spec.rb index 271915378..8a4ab048e 100644 --- a/spec/lib/active_storage/downloadable_file_spec.rb +++ b/spec/lib/active_storage/downloadable_file_spec.rb @@ -24,5 +24,19 @@ describe ActiveStorage::DownloadableFile do expect(list.size).to eq(4) end end + + context 'when there is a message with no attachment' do + let(:commentaire) { create(:commentaire) } + let(:dossier) { commentaire.dossier } + + it { expect(list.length).to be 0 } + end + + context 'when there is a message with an attachment' do + let(:commentaire) { create(:commentaire, :with_file) } + let(:dossier) { commentaire.dossier } + + it { expect(list.length).to be 1 } + end end end