Revert "Revert "Expose dossier PDF export as IO""
This reverts commit 362093eff0
.
This commit is contained in:
parent
aa13bf1c23
commit
66cc0dd08d
7 changed files with 37 additions and 18 deletions
|
@ -1,4 +1,6 @@
|
||||||
class ArchiveCreationJob < ApplicationJob
|
class ArchiveCreationJob < ApplicationJob
|
||||||
|
queue_as :exports
|
||||||
|
|
||||||
def perform(procedure, archive, instructeur)
|
def perform(procedure, archive, instructeur)
|
||||||
ProcedureArchiveService
|
ProcedureArchiveService
|
||||||
.new(procedure)
|
.new(procedure)
|
||||||
|
|
|
@ -69,7 +69,6 @@ class Dossier < ApplicationRecord
|
||||||
has_many :attestations, dependent: :destroy
|
has_many :attestations, dependent: :destroy
|
||||||
|
|
||||||
has_one_attached :justificatif_motivation
|
has_one_attached :justificatif_motivation
|
||||||
has_one_attached :pdf_export_for_instructeur
|
|
||||||
|
|
||||||
has_many :champs, -> { root.public_ordered }, inverse_of: false, dependent: :destroy
|
has_many :champs, -> { root.public_ordered }, inverse_of: false, dependent: :destroy
|
||||||
has_many :champs_private, -> { root.private_ordered }, class_name: 'Champ', inverse_of: false, dependent: :destroy
|
has_many :champs_private, -> { root.private_ordered }, class_name: 'Champ', inverse_of: false, dependent: :destroy
|
||||||
|
|
|
@ -49,6 +49,30 @@ class PiecesJustificativesService
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
class FakeAttachment < Hashie::Dash
|
||||||
|
property :filename
|
||||||
|
property :name
|
||||||
|
property :file
|
||||||
|
property :id
|
||||||
|
property :created_at
|
||||||
|
|
||||||
|
def download
|
||||||
|
file
|
||||||
|
end
|
||||||
|
|
||||||
|
def read(*args)
|
||||||
|
file.read(*args)
|
||||||
|
end
|
||||||
|
|
||||||
|
def close
|
||||||
|
file.close
|
||||||
|
end
|
||||||
|
|
||||||
|
def attached?
|
||||||
|
true
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
def self.generate_dossier_export(dossier)
|
def self.generate_dossier_export(dossier)
|
||||||
pdf = ApplicationController
|
pdf = ApplicationController
|
||||||
.render(template: 'dossiers/show', formats: [:pdf],
|
.render(template: 'dossiers/show', formats: [:pdf],
|
||||||
|
@ -56,10 +80,14 @@ class PiecesJustificativesService
|
||||||
include_infos_administration: true,
|
include_infos_administration: true,
|
||||||
dossier: dossier
|
dossier: dossier
|
||||||
})
|
})
|
||||||
ActiveRecord::Base.no_touching do
|
|
||||||
dossier.pdf_export_for_instructeur.attach(io: StringIO.open(pdf), filename: "export-#{dossier.id}.pdf", content_type: 'application/pdf')
|
FakeAttachment.new(
|
||||||
end
|
file: StringIO.new(pdf),
|
||||||
dossier.pdf_export_for_instructeur
|
filename: "export-#{dossier.id}.pdf",
|
||||||
|
name: 'pdf_export_for_instructeur',
|
||||||
|
id: dossier.id,
|
||||||
|
created_at: dossier.updated_at
|
||||||
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
|
@ -26,7 +26,7 @@ class ProcedureArchiveService
|
||||||
|
|
||||||
Zip::OutputStream.open(tmp_file) do |zipfile|
|
Zip::OutputStream.open(tmp_file) do |zipfile|
|
||||||
files.each do |attachment, pj_filename|
|
files.each do |attachment, pj_filename|
|
||||||
zipfile.put_next_entry(pj_filename)
|
zipfile.put_next_entry("procedure-#{@procedure.id}/#{pj_filename}")
|
||||||
zipfile.puts(attachment.download)
|
zipfile.puts(attachment.download)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -201,15 +201,6 @@ FactoryBot.define do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
trait :with_pdf_export do
|
|
||||||
after(:create) do |dossier, _evaluator|
|
|
||||||
dossier.pdf_export_for_instructeur.attach(
|
|
||||||
io: StringIO.new('Hello World'),
|
|
||||||
filename: 'export.pdf'
|
|
||||||
)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
trait :with_justificatif do
|
trait :with_justificatif do
|
||||||
after(:create) do |dossier, _evaluator|
|
after(:create) do |dossier, _evaluator|
|
||||||
dossier.justificatif_motivation.attach(
|
dossier.justificatif_motivation.attach(
|
||||||
|
|
|
@ -1,12 +1,12 @@
|
||||||
describe ActiveStorage::DownloadableFile do
|
describe ActiveStorage::DownloadableFile do
|
||||||
let(:dossier) { create(:dossier, :en_construction, :with_pdf_export) }
|
let(:dossier) { create(:dossier, :en_construction) }
|
||||||
|
|
||||||
subject(:list) { ActiveStorage::DownloadableFile.create_list_from_dossier(dossier) }
|
subject(:list) { ActiveStorage::DownloadableFile.create_list_from_dossier(dossier) }
|
||||||
|
|
||||||
describe 'create_list_from_dossier' do
|
describe 'create_list_from_dossier' do
|
||||||
context 'when no piece_justificative is present' do
|
context 'when no piece_justificative is present' do
|
||||||
it { expect(list.length).to eq 1 }
|
it { expect(list.length).to eq 1 }
|
||||||
it { expect(list.first[0].record_type).to eq "Dossier" }
|
it { expect(list.first[0].name).to eq "pdf_export_for_instructeur" }
|
||||||
end
|
end
|
||||||
|
|
||||||
context 'when there is a piece_justificative' do
|
context 'when there is a piece_justificative' do
|
||||||
|
|
|
@ -49,7 +49,6 @@ describe PiecesJustificativesService do
|
||||||
subject { PiecesJustificativesService.generate_dossier_export(dossier) }
|
subject { PiecesJustificativesService.generate_dossier_export(dossier) }
|
||||||
it "generates pdf export for instructeur" do
|
it "generates pdf export for instructeur" do
|
||||||
subject
|
subject
|
||||||
expect(dossier.pdf_export_for_instructeur).to be_attached
|
|
||||||
end
|
end
|
||||||
|
|
||||||
it "doesn't update dossier" do
|
it "doesn't update dossier" do
|
||||||
|
|
Loading…
Reference in a new issue