This commit is contained in:
simon lehericey 2022-04-07 15:23:18 +02:00 committed by Paul Chavard
parent 5337fa02e3
commit 9c73bf1932
4 changed files with 27 additions and 19 deletions

View file

@ -1,7 +1,6 @@
class ActiveStorage::DownloadableFile class ActiveStorage::DownloadableFile
def self.create_list_from_dossiers(dossiers, for_expert = false) def self.create_list_from_dossiers(dossiers, for_expert = false)
dossiers PiecesJustificativesService.generate_dossier_export(dossiers) +
.map { |d| pj_and_path(d.id, PiecesJustificativesService.generate_dossier_export(d)) } +
PiecesJustificativesService.liste_documents(dossiers, for_expert) PiecesJustificativesService.liste_documents(dossiers, for_expert)
end end

View file

@ -107,7 +107,10 @@ class PiecesJustificativesService
end end
end end
def self.generate_dossier_export(dossier) def self.generate_dossier_export(dossiers)
pdfs = []
dossiers.find_each do |dossier|
pdf = ApplicationController pdf = ApplicationController
.render(template: 'dossiers/show', formats: [:pdf], .render(template: 'dossiers/show', formats: [:pdf],
assigns: { assigns: {
@ -115,13 +118,18 @@ class PiecesJustificativesService
dossier: dossier dossier: dossier
}) })
FakeAttachment.new( a = FakeAttachment.new(
file: StringIO.new(pdf), file: StringIO.new(pdf),
filename: "export-#{dossier.id}.pdf", filename: "export-#{dossier.id}.pdf",
name: 'pdf_export_for_instructeur', name: 'pdf_export_for_instructeur',
id: dossier.id, id: dossier.id,
created_at: dossier.updated_at created_at: dossier.updated_at
) )
pdfs << ActiveStorage::DownloadableFile.pj_and_path(dossier.id, a)
end
pdfs
end end
private private

View file

@ -192,7 +192,7 @@ describe PiecesJustificativesService do
describe '.generate_dossier_export' do describe '.generate_dossier_export' do
let(:dossier) { create(:dossier) } let(:dossier) { create(:dossier) }
subject { PiecesJustificativesService.generate_dossier_export(dossier) } subject { PiecesJustificativesService.generate_dossier_export(Dossier.where(id: dossier.id)) }
it "doesn't update dossier" do it "doesn't update dossier" do
expect { subject }.not_to change { dossier.updated_at } expect { subject }.not_to change { dossier.updated_at }

View file

@ -437,6 +437,7 @@ describe ProcedureExportService do
context 'with files (and http calls)' do context 'with files (and http calls)' do
let!(:dossier) { create(:dossier, :accepte, :with_populated_champs, :with_individual, procedure: procedure) } let!(:dossier) { create(:dossier, :accepte, :with_populated_champs, :with_individual, procedure: procedure) }
let(:dossier_exports) { PiecesJustificativesService.generate_dossier_export(Dossier.where(id: dossier)) }
before do before do
allow_any_instance_of(ActiveStorage::Attachment).to receive(:url).and_return("https://opengraph.githubassets.com/d0e7862b24d8026a3c03516d865b28151eb3859029c6c6c2e86605891fbdcd7a/socketry/async-io") allow_any_instance_of(ActiveStorage::Attachment).to receive(:url).and_return("https://opengraph.githubassets.com/d0e7862b24d8026a3c03516d865b28151eb3859029c6c6c2e86605891fbdcd7a/socketry/async-io")
@ -454,7 +455,7 @@ describe ProcedureExportService do
"#{service.send(:base_filename)}/dossier-#{dossier.id}/", "#{service.send(:base_filename)}/dossier-#{dossier.id}/",
"#{service.send(:base_filename)}/dossier-#{dossier.id}/pieces_justificatives/", "#{service.send(:base_filename)}/dossier-#{dossier.id}/pieces_justificatives/",
"#{service.send(:base_filename)}/dossier-#{dossier.id}/#{ActiveStorage::DownloadableFile.timestamped_filename(ActiveStorage::Attachment.where(record_type: "Champ").first)}", "#{service.send(:base_filename)}/dossier-#{dossier.id}/#{ActiveStorage::DownloadableFile.timestamped_filename(ActiveStorage::Attachment.where(record_type: "Champ").first)}",
"#{service.send(:base_filename)}/dossier-#{dossier.id}/#{ActiveStorage::DownloadableFile.timestamped_filename(PiecesJustificativesService.generate_dossier_export(dossier))}" "#{service.send(:base_filename)}/dossier-#{dossier.id}/#{ActiveStorage::DownloadableFile.timestamped_filename(dossier_exports.first.first)}"
] ]
expect(files.size).to eq(structure.size) expect(files.size).to eq(structure.size)
expect(files.map(&:filename)).to match_array(structure) expect(files.map(&:filename)).to match_array(structure)