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 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