Merge pull request #7265 from betagouv/fix_create_new_revision

corrige create_new_revision pour les enfants de type de champ répétition
This commit is contained in:
LeSim 2022-05-06 12:43:40 +02:00 committed by GitHub
commit c20e400faa
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 49 additions and 18 deletions

View file

@ -713,9 +713,20 @@ class Procedure < ApplicationRecord
end end
def create_new_revision def create_new_revision
draft_revision new_draft = draft_revision
.deep_clone(include: [:revision_types_de_champ]) .deep_clone(include: [:revision_types_de_champ])
.tap(&:save!) .tap(&:save!)
children = new_draft.revision_types_de_champ.where.not(parent_id: nil)
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)
end
new_draft.revision_types_de_champ.reload
new_draft
end end
def average_dossier_weight def average_dossier_weight

View file

@ -137,32 +137,52 @@ describe ProcedureRevision do
end end
describe '#create_new_revision' do describe '#create_new_revision' do
let(:procedure) { create(:procedure, :with_type_de_champ, :with_type_de_champ_private, :with_repetition) }
let(:new_draft) { procedure.create_new_revision } let(:new_draft) { procedure.create_new_revision }
before { new_draft.save } context 'from a simple procedure' do
let(:procedure) { create(:procedure) }
it 'should be part of procedure' do it 'should be part of procedure' do
expect(new_draft.procedure).to eq(draft.procedure) expect(new_draft.procedure).to eq(draft.procedure)
expect(procedure.revisions.count).to eq(2) expect(procedure.revisions.count).to eq(2)
expect(procedure.revisions).to eq([draft, new_draft]) expect(procedure.revisions).to eq([draft, new_draft])
end end
end
it 'should have types_de_champ' do context 'with simple tdc' do
expect(new_draft.types_de_champ_public.count).to eq(2) let(:procedure) { create(:procedure, :with_type_de_champ, :with_type_de_champ_private) }
it 'should have the same tdcs with different links' do
expect(new_draft.types_de_champ_public.count).to eq(1)
expect(new_draft.types_de_champ_private.count).to eq(1) expect(new_draft.types_de_champ_private.count).to eq(1)
expect(new_draft.types_de_champ_public).to eq(draft.types_de_champ_public) expect(new_draft.types_de_champ_public).to eq(draft.types_de_champ_public)
expect(new_draft.types_de_champ_private).to eq(draft.types_de_champ_private) expect(new_draft.types_de_champ_private).to eq(draft.types_de_champ_private)
expect(new_draft.revision_types_de_champ_public.count).to eq(2) expect(new_draft.revision_types_de_champ_public.count).to eq(1)
expect(new_draft.revision_types_de_champ_private.count).to eq(1) expect(new_draft.revision_types_de_champ_private.count).to eq(1)
expect(new_draft.revision_types_de_champ_public.count).to eq(draft.revision_types_de_champ_public.count)
expect(new_draft.revision_types_de_champ_private.count).to eq(draft.revision_types_de_champ_private.count)
expect(new_draft.revision_types_de_champ_public).not_to eq(draft.revision_types_de_champ_public) expect(new_draft.revision_types_de_champ_public).not_to eq(draft.revision_types_de_champ_public)
expect(new_draft.revision_types_de_champ_private).not_to eq(draft.revision_types_de_champ_private) expect(new_draft.revision_types_de_champ_private).not_to eq(draft.revision_types_de_champ_private)
end end
end
context 'with repetition_type_de_champ' do
let(:procedure) { create(:procedure, :with_repetition) }
it 'should have the same tdcs with different links' do
expect(new_draft.types_de_champ.count).to eq(2)
expect(new_draft.types_de_champ).to eq(draft.types_de_champ)
new_repetition, new_child = new_draft.types_de_champ.partition(&:repetition?).map(&:first)
parent = new_draft.revision_types_de_champ.find_by(type_de_champ: new_repetition)
child = new_draft.revision_types_de_champ.find_by(type_de_champ: new_child)
expect(child.parent_id).to eq(parent.id)
end
end
describe '#compare' do describe '#compare' do
let(:procedure) { create(:procedure, :with_type_de_champ, :with_type_de_champ_private, :with_repetition) }
let(:type_de_champ_first) { draft.types_de_champ_public.first } let(:type_de_champ_first) { draft.types_de_champ_public.first }
let(:type_de_champ_second) { draft.types_de_champ_public.second } let(:type_de_champ_second) { draft.types_de_champ_public.second }