From 651fd6149b3ba8887b8feff650929162b64ee824 Mon Sep 17 00:00:00 2001 From: Frederic Merizen Date: Thu, 10 Jan 2019 13:34:47 +0100 Subject: [PATCH] [#2180] When cloning a procedure, convert types de PJ to types de champ PJ --- app/models/procedure.rb | 2 +- app/services/pieces_justificatives_service.rb | 33 +++++++++++++++++++ spec/models/procedure_spec.rb | 22 ++++++++----- 3 files changed, 48 insertions(+), 9 deletions(-) diff --git a/app/models/procedure.rb b/app/models/procedure.rb index b79d5ad47..05d1b33a8 100644 --- a/app/models/procedure.rb +++ b/app/models/procedure.rb @@ -187,7 +187,6 @@ class Procedure < ApplicationRecord populate_champ_stable_ids procedure = self.deep_clone(include: { - types_de_piece_justificative: nil, attestation_template: nil, types_de_champ: :drop_down_list, types_de_champ_private: :drop_down_list @@ -203,6 +202,7 @@ class Procedure < ApplicationRecord [:notice, :deliberation].each { |attachment| clone_attachment(procedure, attachment) } + procedure.types_de_champ += PiecesJustificativesService.types_pj_as_types_de_champ(self) procedure.administrateur = admin procedure.initiated_mail = initiated_mail&.dup procedure.received_mail = received_mail&.dup diff --git a/app/services/pieces_justificatives_service.rb b/app/services/pieces_justificatives_service.rb index 6a68a0d7b..806f76caf 100644 --- a/app/services/pieces_justificatives_service.rb +++ b/app/services/pieces_justificatives_service.rb @@ -31,4 +31,37 @@ class PiecesJustificativesService missing_pjs.map { |pj| "La pièce jointe #{pj.libelle.truncate(200)} doit être fournie." } end + + def self.types_pj_as_types_de_champ(procedure) + order_place = procedure.types_de_champ.last&.order_place || 0 + types_de_champ = [ + TypeDeChamp.new( + libelle: "Pièces jointes", + type_champ: TypeDeChamp.type_champs.fetch(:header_section), + order_place: order_place + ) + ] + types_de_champ += procedure.types_de_piece_justificative.map do |tpj| + order_place += 1 + description = tpj.description + if tpj.lien_demarche.present? + if description.present? + description += "\n" + end + description += "Récupérer le formulaire vierge pour mon dossier : #{tpj.lien_demarche}" + end + TypeDeChamp.new( + libelle: tpj.libelle, + type_champ: TypeDeChamp.type_champs.fetch(:piece_justificative), + description: description, + order_place: order_place, + mandatory: tpj.mandatory + ) + end + if types_de_champ.count > 1 + types_de_champ + else + [] + end + end end diff --git a/spec/models/procedure_spec.rb b/spec/models/procedure_spec.rb index 1e8d964cb..5fa6f8943 100644 --- a/spec/models/procedure_spec.rb +++ b/spec/models/procedure_spec.rb @@ -366,13 +366,12 @@ describe Procedure do it 'should duplicate specific objects with different id' do expect(subject.id).not_to eq(procedure.id) - expect(subject.types_de_piece_justificative.size).to eq procedure.types_de_piece_justificative.size - expect(subject.types_de_champ.size).to eq procedure.types_de_champ.size + expect(subject.types_de_champ.size).to eq(procedure.types_de_champ.size + 1 + procedure.types_de_piece_justificative.size) expect(subject.types_de_champ_private.size).to eq procedure.types_de_champ_private.size expect(subject.types_de_champ.map(&:drop_down_list).compact.size).to eq procedure.types_de_champ.map(&:drop_down_list).compact.size expect(subject.types_de_champ_private.map(&:drop_down_list).compact.size).to eq procedure.types_de_champ_private.map(&:drop_down_list).compact.size - subject.types_de_champ.zip(procedure.types_de_champ).each do |stc, ptc| + procedure.types_de_champ.zip(subject.types_de_champ).each do |ptc, stc| expect(stc).to have_same_attributes_as(ptc) end @@ -380,10 +379,6 @@ describe Procedure do expect(stc).to have_same_attributes_as(ptc) end - subject.types_de_piece_justificative.zip(procedure.types_de_piece_justificative).each do |stc, ptc| - expect(stc).to have_same_attributes_as(ptc) - end - expect(subject.attestation_template.title).to eq(procedure.attestation_template.title) expect(subject.cloned_from_library).to be(false) @@ -393,7 +388,18 @@ describe Procedure do expect(cloned_procedure).to have_same_attributes_as(procedure) end - context 'when the procedure is clone from the library' do + it 'should not clone piece justificatives but create corresponding champs' do + expect(subject.types_de_piece_justificative.size).to eq(0) + + champs_pj = subject.types_de_champ[procedure.types_de_champ.size + 1, procedure.types_de_piece_justificative.size] + champs_pj.zip(procedure.types_de_piece_justificative).each do |stc, ptpj| + expect(stc.libelle).to eq(ptpj.libelle) + expect(stc.description).to eq(ptpj.description) + expect(stc.mandatory).to eq(ptpj.mandatory) + end + end + + context 'when the procedure is cloned from the library' do let(:from_library) { true } it { expect(subject.cloned_from_library).to be(true) }