Merge pull request #7426 from tchak/fix-clone-with-blocks

fix(revision): fix clone démarches with repetitions
This commit is contained in:
Paul Chavard 2022-05-31 13:55:35 +02:00 committed by GitHub
commit b6b3284fba
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 21 additions and 12 deletions

View file

@ -778,11 +778,15 @@ class Procedure < ApplicationRecord
private
def move_new_children_to_new_parent_coordinate(new_draft)
children = new_draft.revision_types_de_champ.where.not(parent_id: nil)
children = new_draft.revision_types_de_champ
.includes(parent: :type_de_champ)
.where.not(parent_id: nil)
coordinates_by_stable_id = new_draft.revision_types_de_champ
.includes(:type_de_champ)
.index_by(&:stable_id)
children.each do |child|
old_parent = draft_revision.revision_types_de_champ.find(child.parent_id)
new_parent = new_draft.revision_types_de_champ.find_by(type_de_champ_id: old_parent.type_de_champ_id)
child.update!(parent_id: new_parent.id)
child.update!(parent: coordinates_by_stable_id.fetch(child.parent.stable_id))
end
new_draft.revision_types_de_champ.reload
end

View file

@ -432,8 +432,8 @@ describe Procedure do
received_mail: received_mail,
service: service,
attestation_template: build(:attestation_template, logo: logo, signature: signature),
types_de_champ: [type_de_champ_0, type_de_champ_1, type_de_champ_2, type_de_champ_pj, type_de_champ_repetition],
types_de_champ_private: [type_de_champ_private_0, type_de_champ_private_1, type_de_champ_private_2, type_de_champ_private_repetition],
types_de_champ: [type_de_champ_0, type_de_champ_1, type_de_champ_2, type_de_champ_pj],
types_de_champ_private: [type_de_champ_private_0, type_de_champ_private_1, type_de_champ_private_2],
api_particulier_token: '123456789012345',
api_particulier_scopes: ['cnaf_famille'])
end
@ -441,11 +441,11 @@ describe Procedure do
let(:type_de_champ_1) { build(:type_de_champ, position: 1) }
let(:type_de_champ_2) { build(:type_de_champ_drop_down_list, position: 2) }
let(:type_de_champ_pj) { build(:type_de_champ_piece_justificative, position: 3, old_pj: { stable_id: 2713 }) }
let(:type_de_champ_repetition) { build(:type_de_champ_repetition, position: 4, types_de_champ: [build(:type_de_champ)]) }
let(:type_de_champ_repetition) { build(:type_de_champ_repetition, position: 4, procedure: procedure, types_de_champ: [build(:type_de_champ)]) }
let(:type_de_champ_private_0) { build(:type_de_champ, :private, position: 0) }
let(:type_de_champ_private_1) { build(:type_de_champ, :private, position: 1) }
let(:type_de_champ_private_2) { build(:type_de_champ_drop_down_list, :private, position: 2) }
let(:type_de_champ_private_repetition) { build(:type_de_champ_repetition, :private, position: 3, types_de_champ: [build(:type_de_champ, :private)]) }
let(:type_de_champ_private_repetition) { build(:type_de_champ_repetition, :private, position: 3, procedure: procedure, types_de_champ: [build(:type_de_champ, :private)]) }
let(:received_mail) { build(:received_mail) }
let(:from_library) { false }
let(:administrateur) { procedure.administrateurs.first }
@ -459,6 +459,9 @@ describe Procedure do
let!(:assign_to_2) { create(:assign_to, procedure: procedure, groupe_instructeur: groupe_instructeur_1, instructeur: instructeur_2) }
before do
type_de_champ_repetition
type_de_champ_private_repetition
@procedure = procedure.clone(administrateur, from_library)
@procedure.save
end
@ -490,8 +493,9 @@ describe Procedure do
expect(stc.revision).to eq(subject.draft_revision)
end
public_repetition = procedure.draft_types_de_champ.repetition.first
procedure.draft_revision.children_of(public_repetition).zip(subject.draft_revision.children_of(public_repetition)).each do |ptc, stc|
public_repetition = type_de_champ_repetition
cloned_public_repetition = subject.draft_types_de_champ.repetition.first
procedure.draft_revision.children_of(public_repetition).zip(subject.draft_revision.children_of(cloned_public_repetition)).each do |ptc, stc|
expect(stc).to have_same_attributes_as(ptc)
expect(stc.revision).to eq(subject.draft_revision)
end
@ -501,8 +505,9 @@ describe Procedure do
expect(stc.revision).to eq(subject.draft_revision)
end
private_repetition = procedure.draft_types_de_champ_private.repetition.first
procedure.draft_revision.children_of(private_repetition).zip(subject.draft_revision.children_of(private_repetition)).each do |ptc, stc|
private_repetition = type_de_champ_private_repetition
cloned_private_repetition = subject.draft_types_de_champ_private.repetition.first
procedure.draft_revision.children_of(private_repetition).zip(subject.draft_revision.children_of(cloned_private_repetition)).each do |ptc, stc|
expect(stc).to have_same_attributes_as(ptc)
expect(stc.revision).to eq(subject.draft_revision)
end