From 223317fb5ad27d80374dbc63ce96a501834e83be Mon Sep 17 00:00:00 2001 From: Paul Chavard Date: Thu, 25 Jul 2024 18:29:01 +0200 Subject: [PATCH] fix(rebase): avoid errors when adding pj champs to a new revision --- app/models/concerns/dossier_rebase_concern.rb | 14 +++++++------- .../models/concerns/dossier_rebase_concern_spec.rb | 8 ++++++-- 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/app/models/concerns/dossier_rebase_concern.rb b/app/models/concerns/dossier_rebase_concern.rb index 1bea21a4e..1420bab39 100644 --- a/app/models/concerns/dossier_rebase_concern.rb +++ b/app/models/concerns/dossier_rebase_concern.rb @@ -62,13 +62,6 @@ module DossierRebaseConcern .transform_values { Champ.where(id: _1) } .tap { _1.default = Champ.none } - # add champ - changes_by_op[:add] - .map { target_coordinates_by_stable_id[_1.stable_id] } - # add parent champs first so we can then add children - .sort_by { _1.child? ? 1 : 0 } - .each { add_new_champs_for_revision(_1) } - # remove champ children_champ, root_champ = changes_by_op[:remove].partition(&:child?) children_champ.each { champs_by_stable_id[_1.stable_id].destroy_all } @@ -79,6 +72,13 @@ module DossierRebaseConcern # update dossier revision update_column(:revision_id, target_revision.id) + + # add champ (after changing dossier revision to avoid errors) + changes_by_op[:add] + .map { target_coordinates_by_stable_id[_1.stable_id] } + # add parent champs first so we can then add children + .sort_by { _1.child? ? 1 : 0 } + .each { add_new_champs_for_revision(_1) } end def apply(change, champs) diff --git a/spec/models/concerns/dossier_rebase_concern_spec.rb b/spec/models/concerns/dossier_rebase_concern_spec.rb index d3fbec10b..574ca25f4 100644 --- a/spec/models/concerns/dossier_rebase_concern_spec.rb +++ b/spec/models/concerns/dossier_rebase_concern_spec.rb @@ -312,6 +312,10 @@ describe DossierRebaseConcern do type_champ: TypeDeChamp.type_champs.fetch(:text), libelle: "Un champ text" }) + procedure.draft_revision.add_type_de_champ({ + type_champ: TypeDeChamp.type_champs.fetch(:piece_justificative), + libelle: "Un champ pj" + }) procedure.draft_revision.find_and_ensure_exclusive_use(text_type_de_champ.stable_id).update(mandatory: false, libelle: "nouveau libelle") procedure.draft_revision.find_and_ensure_exclusive_use(datetime_type_de_champ.stable_id).update(type_champ: TypeDeChamp.type_champs.fetch(:date)) procedure.draft_revision.find_and_ensure_exclusive_use(repetition_text_type_de_champ.stable_id).update(libelle: "nouveau libelle dans une repetition") @@ -361,8 +365,8 @@ describe DossierRebaseConcern do expect(procedure.revisions.size).to eq(3) expect(dossier.revision).to eq(procedure.published_revision) - expect(dossier.champs_public.size).to eq(6) - expect(dossier.champs.count(&:public?)).to eq(12) + expect(dossier.champs_public.size).to eq(7) + expect(dossier.champs.count(&:public?)).to eq(13) expect(rebased_text_champ.value).to eq(text_champ.value) expect(rebased_text_champ.type_de_champ).not_to eq(text_champ.type_de_champ) expect(rebased_datetime_champ.type_champ).to eq(TypeDeChamp.type_champs.fetch(:date))