refactor(active_storage): no download on clone
This commit is contained in:
parent
b526fef4db
commit
95f65900d3
5 changed files with 30 additions and 49 deletions
|
@ -54,27 +54,7 @@ class AttestationTemplate < ApplicationRecord
|
||||||
|
|
||||||
def dup
|
def dup
|
||||||
attestation_template = AttestationTemplate.new(title: title, body: body, footer: footer, activated: activated)
|
attestation_template = AttestationTemplate.new(title: title, body: body, footer: footer, activated: activated)
|
||||||
|
PiecesJustificativesService.clone_attachments(self, attestation_template)
|
||||||
if logo.attached?
|
|
||||||
attestation_template.logo.attach(
|
|
||||||
io: StringIO.new(logo.download),
|
|
||||||
filename: logo.filename.to_s,
|
|
||||||
content_type: logo.content_type,
|
|
||||||
# we don't want to run virus scanner on duplicated file
|
|
||||||
metadata: { virus_scan_result: ActiveStorage::VirusScanner::SAFE }
|
|
||||||
)
|
|
||||||
end
|
|
||||||
|
|
||||||
if signature.attached?
|
|
||||||
attestation_template.signature.attach(
|
|
||||||
io: StringIO.new(signature.download),
|
|
||||||
filename: signature.filename.to_s,
|
|
||||||
content_type: signature.content_type,
|
|
||||||
# we don't want to run virus scanner on duplicated file
|
|
||||||
metadata: { virus_scan_result: ActiveStorage::VirusScanner::SAFE }
|
|
||||||
)
|
|
||||||
end
|
|
||||||
|
|
||||||
attestation_template
|
attestation_template
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -488,10 +488,7 @@ class Procedure < ApplicationRecord
|
||||||
}
|
}
|
||||||
include_list[:groupe_instructeurs] = :instructeurs if !is_different_admin
|
include_list[:groupe_instructeurs] = :instructeurs if !is_different_admin
|
||||||
procedure = self.deep_clone(include: include_list) do |original, kopy|
|
procedure = self.deep_clone(include: include_list) do |original, kopy|
|
||||||
begin
|
|
||||||
PiecesJustificativesService.clone_attachments(original, kopy)
|
PiecesJustificativesService.clone_attachments(original, kopy)
|
||||||
rescue ActiveStorage::FileNotFoundError, ActiveStorage::IntegrityError
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
procedure.path = SecureRandom.uuid
|
procedure.path = SecureRandom.uuid
|
||||||
procedure.aasm_state = :brouillon
|
procedure.aasm_state = :brouillon
|
||||||
|
|
|
@ -56,26 +56,22 @@ class PiecesJustificativesService
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.clone_attachments(original, kopy)
|
def self.clone_attachments(original, kopy)
|
||||||
if original.is_a?(TypeDeChamp)
|
case original
|
||||||
|
when TypeDeChamp
|
||||||
clone_attachment(original.piece_justificative_template, kopy.piece_justificative_template)
|
clone_attachment(original.piece_justificative_template, kopy.piece_justificative_template)
|
||||||
elsif original.is_a?(Procedure)
|
when Procedure
|
||||||
clone_attachment(original.logo, kopy.logo)
|
clone_attachment(original.logo, kopy.logo)
|
||||||
clone_attachment(original.notice, kopy.notice)
|
clone_attachment(original.notice, kopy.notice)
|
||||||
clone_attachment(original.deliberation, kopy.deliberation)
|
clone_attachment(original.deliberation, kopy.deliberation)
|
||||||
|
when AttestationTemplate
|
||||||
|
clone_attachment(original.logo, kopy.logo)
|
||||||
|
clone_attachment(original.signature, kopy.signature)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.clone_attachment(original_attachment, copy_attachment)
|
def self.clone_attachment(original_attachment, copy_attachment)
|
||||||
if original_attachment.attached?
|
if original_attachment.attached?
|
||||||
original_attachment.open do |tempfile|
|
copy_attachment.attach(original_attachment.blob)
|
||||||
copy_attachment.attach({
|
|
||||||
io: File.open(tempfile.path),
|
|
||||||
filename: original_attachment.filename,
|
|
||||||
content_type: original_attachment.content_type,
|
|
||||||
# we don't want to run virus scanner on cloned file
|
|
||||||
metadata: { virus_scan_result: ActiveStorage::VirusScanner::SAFE }
|
|
||||||
})
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -71,12 +71,14 @@ describe AttestationTemplate, type: :model do
|
||||||
let(:attestation_template) { create(:attestation_template, :with_files) }
|
let(:attestation_template) { create(:attestation_template, :with_files) }
|
||||||
|
|
||||||
it do
|
it do
|
||||||
expect(subject.logo.blob).not_to eq(attestation_template.logo.blob)
|
expect(subject.logo.attachment).not_to eq(attestation_template.logo.attachment)
|
||||||
|
expect(subject.logo.blob).to eq(attestation_template.logo.blob)
|
||||||
expect(subject.logo.attached?).to be_truthy
|
expect(subject.logo.attached?).to be_truthy
|
||||||
end
|
end
|
||||||
|
|
||||||
it do
|
it do
|
||||||
expect(subject.signature.blob).not_to eq(attestation_template.signature.blob)
|
expect(subject.signature.attachment).not_to eq(attestation_template.signature.attachment)
|
||||||
|
expect(subject.signature.blob).to eq(attestation_template.signature.blob)
|
||||||
expect(subject.signature.attached?).to be_truthy
|
expect(subject.signature.attached?).to be_truthy
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -657,7 +657,23 @@ describe Procedure do
|
||||||
let(:procedure) { create(:procedure, :with_notice, received_mail: received_mail, service: service) }
|
let(:procedure) { create(:procedure, :with_notice, received_mail: received_mail, service: service) }
|
||||||
|
|
||||||
it 'should duplicate notice' do
|
it 'should duplicate notice' do
|
||||||
expect(subject.notice.attached?).to be true
|
expect(subject.notice.attached?).to be_truthy
|
||||||
|
expect(subject.notice.attachment).not_to eq(procedure.notice.attachment)
|
||||||
|
expect(subject.notice.attachment.blob).to eq(procedure.notice.attachment.blob)
|
||||||
|
|
||||||
|
subject.notice.attach(logo)
|
||||||
|
subject.reload
|
||||||
|
procedure.reload
|
||||||
|
|
||||||
|
expect(subject.notice.attached?).to be_truthy
|
||||||
|
expect(subject.notice.attachment.blob).not_to eq(procedure.notice.attachment.blob)
|
||||||
|
|
||||||
|
subject.notice.purge
|
||||||
|
subject.reload
|
||||||
|
procedure.reload
|
||||||
|
|
||||||
|
expect(subject.notice.attached?).to be_falsey
|
||||||
|
expect(procedure.notice.attached?).to be_truthy
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -677,16 +693,6 @@ describe Procedure do
|
||||||
expect(subject.canonical_procedure).to be_nil
|
expect(subject.canonical_procedure).to be_nil
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
context 'with an pj not found' do
|
|
||||||
let(:procedure) { create(:procedure) }
|
|
||||||
|
|
||||||
before do
|
|
||||||
expect(PiecesJustificativesService).to receive(:clone_attachments).at_least(:once).and_raise(ActiveStorage::FileNotFoundError)
|
|
||||||
end
|
|
||||||
|
|
||||||
it { expect { procedure.clone(administrateur, false) }.not_to raise_error }
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
describe '#publish!' do
|
describe '#publish!' do
|
||||||
|
|
Loading…
Reference in a new issue