tech(refactor): extract clone logic inside its own class

This commit is contained in:
Martin 2024-02-07 17:29:47 +01:00
parent 72a3f6e89c
commit 3e64a3bbbc
7 changed files with 34 additions and 33 deletions

View file

@ -92,7 +92,7 @@ class AttestationTemplate < ApplicationRecord
def dup
attestation_template = AttestationTemplate.new(title: title, body: body, footer: footer, activated: activated)
PiecesJustificativesService.clone_attachments(self, attestation_template)
ClonePiecesJustificativesService.clone_attachments(self, attestation_template)
attestation_template
end

View file

@ -238,7 +238,7 @@ class Champ < ApplicationRecord
relationships = fork || !private? ? [:etablissement, :geo_areas] : []
deep_clone(only: champ_attributes + value_attributes, include: relationships, validate: !fork) do |original, kopy|
PiecesJustificativesService.clone_attachments(original, kopy)
ClonePiecesJustificativesService.clone_attachments(original, kopy)
end
end

View file

@ -85,7 +85,7 @@ module DossierCloneConcern
.transform_values { [_1, _1.clone(fork)] }
cloned_dossier = deep_clone(only: dossier_attributes, include: relationships) do |original, kopy|
PiecesJustificativesService.clone_attachments(original, kopy)
ClonePiecesJustificativesService.clone_attachments(original, kopy)
if original.is_a?(Dossier)
if fork

View file

@ -497,7 +497,7 @@ class Procedure < ApplicationRecord
}
include_list[:groupe_instructeurs] = [:instructeurs, :contact_information] if !is_different_admin
procedure = self.deep_clone(include: include_list) do |original, kopy|
PiecesJustificativesService.clone_attachments(original, kopy)
ClonePiecesJustificativesService.clone_attachments(original, kopy)
end
procedure.path = SecureRandom.uuid
procedure.aasm_state = :brouillon

View file

@ -414,7 +414,7 @@ class ProcedureRevision < ApplicationRecord
def replace_type_de_champ_by_clone(coordinate)
cloned_type_de_champ = coordinate.type_de_champ.deep_clone do |original, kopy|
PiecesJustificativesService.clone_attachments(original, kopy)
ClonePiecesJustificativesService.clone_attachments(original, kopy)
end
coordinate.update!(type_de_champ: cloned_type_de_champ)
cloned_type_de_champ

View file

@ -0,0 +1,29 @@
class ClonePiecesJustificativesService
def self.clone_attachments(original, kopy)
case original
when Champs::PieceJustificativeChamp, Champs::TitreIdentiteChamp
original.piece_justificative_file.attachments.each do |attachment|
kopy.piece_justificative_file.attach(attachment.blob)
end
when TypeDeChamp
clone_attachment(original, kopy, :piece_justificative_template)
when Procedure
clone_attachment(original, kopy, :logo)
clone_attachment(original, kopy, :notice)
clone_attachment(original, kopy, :deliberation)
when AttestationTemplate
clone_attachment(original, kopy, :logo)
clone_attachment(original, kopy, :signature)
when Etablissement
clone_attachment(original, kopy, :entreprise_attestation_sociale)
clone_attachment(original, kopy, :entreprise_attestation_fiscale)
end
end
def self.clone_attachment(original, kopy, attachment_name)
attachment = original.public_send(attachment_name)
if attachment.attached?
kopy.public_send(attachment_name).attach(attachment.blob)
end
end
end

View file

@ -59,34 +59,6 @@ class PiecesJustificativesService
end.flatten
end
def self.clone_attachments(original, kopy)
case original
when Champs::PieceJustificativeChamp, Champs::TitreIdentiteChamp
original.piece_justificative_file.attachments.each do |attachment|
kopy.piece_justificative_file.attach(attachment.blob)
end
when TypeDeChamp
clone_attachment(original, kopy, :piece_justificative_template)
when Procedure
clone_attachment(original, kopy, :logo)
clone_attachment(original, kopy, :notice)
clone_attachment(original, kopy, :deliberation)
when AttestationTemplate
clone_attachment(original, kopy, :logo)
clone_attachment(original, kopy, :signature)
when Etablissement
clone_attachment(original, kopy, :entreprise_attestation_sociale)
clone_attachment(original, kopy, :entreprise_attestation_fiscale)
end
end
def self.clone_attachment(original, kopy, attachment_name)
attachment = original.public_send(attachment_name)
if attachment.attached?
kopy.public_send(attachment_name).attach(attachment.blob)
end
end
def self.generate_dossier_export(dossiers, include_infos_administration: false, include_avis_for_expert: false)
return [] if dossiers.empty?