From 434cd770301bfd16d270e3d3810c50bd16008a8f Mon Sep 17 00:00:00 2001 From: Martin Date: Thu, 18 Jan 2024 10:13:16 +0100 Subject: [PATCH] poc(spec): renumber only at one point --- app/models/procedure_revision.rb | 9 +++++++-- spec/models/dossier_spec.rb | 3 ++- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/app/models/procedure_revision.rb b/app/models/procedure_revision.rb index 023c2d1dd..2b1669a0e 100644 --- a/app/models/procedure_revision.rb +++ b/app/models/procedure_revision.rb @@ -72,7 +72,6 @@ class ProcedureRevision < ApplicationRecord end end - # [] def move_type_de_champ(stable_id, position) coordinate, _ = coordinate_and_tdc(stable_id) siblings = coordinate.siblings @@ -89,6 +88,12 @@ class ProcedureRevision < ApplicationRecord coordinate end + def renumber(siblings) + siblings.to_a.compact.each.with_index do |sibling, position| + sibling.update_column(:position, position) + end + end + def remove_type_de_champ(stable_id) coordinate, tdc = coordinate_and_tdc(stable_id) @@ -101,9 +106,9 @@ class ProcedureRevision < ApplicationRecord children.each(&:destroy_if_orphan) tdc.destroy_if_orphan + # they are not aware of the removal coordinate.siblings.where("position >= ?", coordinate.position).update_all("position = position - 1") - # they are not aware of the removal types_de_champ_public.reset types_de_champ_private.reset diff --git a/spec/models/dossier_spec.rb b/spec/models/dossier_spec.rb index 49c1b5e02..56f8368a8 100644 --- a/spec/models/dossier_spec.rb +++ b/spec/models/dossier_spec.rb @@ -1931,7 +1931,8 @@ describe Dossier, type: :model do procedure.publish! dossier procedure.draft_revision.remove_type_de_champ(text_type_de_champ.stable_id) - procedure.draft_revision.add_type_de_champ(type_champ: TypeDeChamp.type_champs.fetch(:text), libelle: 'New text field') + coordinate = procedure.draft_revision.add_type_de_champ(type_champ: TypeDeChamp.type_champs.fetch(:text), libelle: 'New text field') + procedure.draft_revision.renumber(coordinate.revision_type_de_champ.siblings) procedure.draft_revision.find_and_ensure_exclusive_use(yes_no_type_de_champ.stable_id).update(libelle: 'Updated yes/no') procedure.draft_revision.find_and_ensure_exclusive_use(commune_type_de_champ.stable_id).update(libelle: 'Commune de naissance') procedure.draft_revision.find_and_ensure_exclusive_use(repetition_type_de_champ.stable_id).update(libelle: 'Repetition')