Use the same clone_attachment method for all attributes on procedure.clone

This commit is contained in:
Mathieu Magnin 2019-03-28 17:17:29 +01:00
parent f05a052674
commit 27c8e9dcb8
2 changed files with 36 additions and 22 deletions

View file

@ -193,16 +193,6 @@ class Procedure < ApplicationRecord
end
end
def clone_attachments(original, kopy)
if original.is_a?(TypeDeChamp) && original.piece_justificative_template.attached?
kopy.piece_justificative_template.attach({
io: StringIO.new(original.piece_justificative_template.download),
filename: original.piece_justificative_template.blob.filename,
content_type: original.piece_justificative_template.blob.content_type
})
end
end
def clone(admin, from_library)
is_different_admin = !admin.owns?(self)
@ -222,8 +212,6 @@ class Procedure < ApplicationRecord
procedure.remote_logo_url = self.logo_url
procedure.lien_notice = nil
[:notice, :deliberation].each { |attachment| clone_attachment(procedure, attachment) }
procedure.types_de_champ += PiecesJustificativesService.types_pj_as_types_de_champ(self)
if is_different_admin || from_library
procedure.types_de_champ.each { |tdc| tdc.options&.delete(:old_pj) }
@ -255,6 +243,26 @@ class Procedure < ApplicationRecord
procedure
end
def clone_attachments(original, kopy)
if original.is_a?(TypeDeChamp)
clone_attachment(:piece_justificative_template, original, kopy)
elsif original.is_a?(Procedure)
clone_attachment(:notice, original, kopy)
clone_attachment(:deliberation, original, kopy)
end
end
def clone_attachment(attribute, original, kopy)
original_attachment = original.send(attribute)
if original_attachment.attached?
kopy.send(attribute).attach({
io: StringIO.new(original_attachment.download),
filename: original_attachment.blob.filename,
content_type: original_attachment.blob.content_type
})
end
end
def whitelisted?
whitelisted_at.present?
end
@ -455,16 +463,6 @@ class Procedure < ApplicationRecord
true
end
def clone_attachment(cloned_procedure, attachment_symbol)
attachment = send(attachment_symbol)
if attachment.attached?
cloned_procedure.send(attachment_symbol).attach(
io: StringIO.new(attachment.download),
filename: attachment.filename
)
end
end
def check_juridique
if juridique_required? && (cadre_juridique.blank? && !deliberation.attached?)
errors.add(:cadre_juridique, " : veuillez remplir le texte de loi ou la délibération")

View file

@ -489,6 +489,22 @@ describe Procedure do
it 'should duplicate piece_justificative_template on a type_de_champ' do
expect(subject.types_de_champ.where(type_champ: "piece_justificative").first.piece_justificative_template.attached?).to be true
end
context 'with a notice attached' do
let(:procedure) { create(:procedure, :with_notice, received_mail: received_mail, service: service) }
it 'should duplicate notice' do
expect(subject.notice.attached?).to be true
end
end
context 'with a deliberation attached' do
let(:procedure) { create(:procedure, :with_deliberation, received_mail: received_mail, service: service) }
it 'should duplicate deliberation' do
expect(subject.deliberation.attached?).to be true
end
end
end
describe '#publish!' do