From 4b57ed871ab97d90f575efc26e5b75eaf422754c Mon Sep 17 00:00:00 2001 From: Martin Date: Wed, 17 Jan 2024 15:20:56 +0100 Subject: [PATCH 1/8] feat(add_type_de_champ): stop renumbering all procedure_revision_type_de_champ --- app/models/procedure_revision.rb | 27 +++++++++++++++++++++----- spec/models/procedure_revision_spec.rb | 10 +++++++++- 2 files changed, 31 insertions(+), 6 deletions(-) diff --git a/app/models/procedure_revision.rb b/app/models/procedure_revision.rb index dff060439..f9ff81818 100644 --- a/app/models/procedure_revision.rb +++ b/app/models/procedure_revision.rb @@ -38,24 +38,37 @@ class ProcedureRevision < ApplicationRecord parent_coordinate, _ = coordinate_and_tdc(parent_stable_id) parent_id = parent_coordinate&.id + siblings = if parent_coordinate + parent_coordinate.revision_types_de_champ + elsif params['private'] || params[:private] + revision_types_de_champ_private + else + revision_types_de_champ_public + end + + after_stable_id = params.delete(:after_stable_id) after_coordinate, _ = coordinate_and_tdc(after_stable_id) - # the collection is orderd by (position, id), so we can use after_coordinate.position - # if not present, a big number is used to ensure the element is at the tail - position = (after_coordinate&.position) || 100_000 + if siblings.to_a.empty? # first element of the list, starts at 0 + position = 0.0 + elsif after_coordinate # middle of the list, between two items + position = after_coordinate.position + 1 + else # last element of the list, end with last position + 1 + position = siblings.to_a.last.position + 1.0 + end tdc = TypeDeChamp.new(params) if tdc.save h = { type_de_champ: tdc, parent_id: parent_id, position: position } + incr_coordinates_above(siblings, position) coordinate = revision_types_de_champ.create!(h) - - renumber(coordinate.reload.siblings) end # they are not aware of the addition types_de_champ_public.reset types_de_champ_private.reset + reload tdc rescue => e @@ -85,6 +98,10 @@ class ProcedureRevision < ApplicationRecord coordinate end + def incr_coordinates_above(siblings, position) + siblings.where("position >= ?", position).update_all("position = position + 1") + end + def remove_type_de_champ(stable_id) coordinate, tdc = coordinate_and_tdc(stable_id) diff --git a/spec/models/procedure_revision_spec.rb b/spec/models/procedure_revision_spec.rb index 59ca5b327..6a258c3de 100644 --- a/spec/models/procedure_revision_spec.rb +++ b/spec/models/procedure_revision_spec.rb @@ -28,6 +28,7 @@ describe ProcedureRevision do it 'public' do expect { subject }.to change { draft.types_de_champ_public.size }.from(2).to(3) expect(draft.types_de_champ_public.last).to eq(subject) + expect(draft.revision_types_de_champ_public.map(&:position)).to eq([0,1,2]) expect(last_coordinate.position).to eq(2) expect(last_coordinate.type_de_champ).to eq(subject) @@ -37,7 +38,12 @@ describe ProcedureRevision do context 'with a private tdc' do let(:tdc_params) { text_params.merge(private: true) } - it { expect { subject }.to change { draft.types_de_champ_private.count }.from(1).to(2) } + it 'private' do + expect { subject }.to change { draft.types_de_champ_private.count }.from(1).to(2) + expect(draft.types_de_champ_private.last).to eq(subject) + expect(draft.revision_types_de_champ_private.map(&:position)).to eq([0,1]) + expect(last_coordinate.position).to eq(1) + end end context 'with a repetition child' do @@ -46,6 +52,7 @@ describe ProcedureRevision do it do expect { subject }.to change { draft.reload.types_de_champ.count }.from(4).to(5) expect(draft.children_of(type_de_champ_repetition).last).to eq(subject) + expect(draft.children_of(type_de_champ_repetition).map(&:revision_type_de_champ).map(&:position)).to eq([0,1]) expect(last_coordinate.position).to eq(1) @@ -74,6 +81,7 @@ describe ProcedureRevision do expect(draft.revision_types_de_champ_public.map(&:libelle)).to eq(['l1', 'l2']) subject expect(draft.revision_types_de_champ_public.reload.map(&:libelle)).to eq(['l1', 'in the middle', 'l2']) + expect(draft.revision_types_de_champ_public.map(&:position)).to eq([0,1,2]) end end From 4d97c5bc8a20403aaf12c3fdec2aa9ad32db009f Mon Sep 17 00:00:00 2001 From: Martin Date: Wed, 17 Jan 2024 15:51:06 +0100 Subject: [PATCH 2/8] feat(move_type_de_champ): stop renumbering all procedure_revision_type_de_champ on move_type_de_champ --- app/models/procedure_revision.rb | 19 +++++++++++++++---- spec/models/procedure_revision_spec.rb | 7 +++---- 2 files changed, 18 insertions(+), 8 deletions(-) diff --git a/app/models/procedure_revision.rb b/app/models/procedure_revision.rb index f9ff81818..dcec531cf 100644 --- a/app/models/procedure_revision.rb +++ b/app/models/procedure_revision.rb @@ -85,14 +85,17 @@ class ProcedureRevision < ApplicationRecord end end + # [] def move_type_de_champ(stable_id, position) coordinate, _ = coordinate_and_tdc(stable_id) - siblings = coordinate.siblings.to_a + if position > coordinate.position + decr_siblings_between(coordinate, position) + else + inc_siblings_between(coordinate, position) + end + coordinate.update_column(:position, position) - siblings.insert(position, siblings.delete_at(siblings.index(coordinate))) - - renumber(siblings) coordinate.reload coordinate @@ -102,6 +105,14 @@ class ProcedureRevision < ApplicationRecord siblings.where("position >= ?", position).update_all("position = position + 1") end + def decr_siblings_between(coordinate, position) + coordinate.siblings.where(position: coordinate.position..position).update_all("position = position - 1") + end + + def inc_siblings_between(coordinate, position) + coordinate.siblings.where(position: position..coordinate.position).update_all("position = position + 1") + end + def remove_type_de_champ(stable_id) coordinate, tdc = coordinate_and_tdc(stable_id) diff --git a/spec/models/procedure_revision_spec.rb b/spec/models/procedure_revision_spec.rb index 6a258c3de..b10dd641d 100644 --- a/spec/models/procedure_revision_spec.rb +++ b/spec/models/procedure_revision_spec.rb @@ -103,20 +103,19 @@ describe ProcedureRevision do context 'with 4 types de champ publiques' do it 'move down' do expect(draft.types_de_champ_public.index(type_de_champ_public)).to eq(0) - + stable_id_before = draft.revision_types_de_champ_public.map(&:stable_id) draft.move_type_de_champ(type_de_champ_public.stable_id, 2) draft.reload - + expect(draft.revision_types_de_champ_public.map(&:position)).to eq([0,1,2,3]) expect(draft.types_de_champ_public.index(type_de_champ_public)).to eq(2) expect(draft.procedure.types_de_champ_for_procedure_presentation.not_repetition.index(type_de_champ_public)).to eq(2) end it 'move up' do expect(draft.types_de_champ_public.index(last_type_de_champ)).to eq(3) - draft.move_type_de_champ(last_type_de_champ.stable_id, 0) draft.reload - + expect(draft.revision_types_de_champ_public.map(&:position)).to eq([0,1,2,3]) expect(draft.types_de_champ_public.index(last_type_de_champ)).to eq(0) expect(draft.procedure.types_de_champ_for_procedure_presentation.not_repetition.index(last_type_de_champ)).to eq(0) end From a78a6b6f34e35a0dcc90e172d582bdad6765c0e4 Mon Sep 17 00:00:00 2001 From: Martin Date: Wed, 17 Jan 2024 16:01:30 +0100 Subject: [PATCH 3/8] feat(remove_type_de_champ): stop renumbering all procedure_revision_type_de_champ on remove_type_de_champ --- app/models/procedure_revision.rb | 34 ++++++++++++++++---------------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/app/models/procedure_revision.rb b/app/models/procedure_revision.rb index dcec531cf..844b2d536 100644 --- a/app/models/procedure_revision.rb +++ b/app/models/procedure_revision.rb @@ -101,18 +101,6 @@ class ProcedureRevision < ApplicationRecord coordinate end - def incr_coordinates_above(siblings, position) - siblings.where("position >= ?", position).update_all("position = position + 1") - end - - def decr_siblings_between(coordinate, position) - coordinate.siblings.where(position: coordinate.position..position).update_all("position = position - 1") - end - - def inc_siblings_between(coordinate, position) - coordinate.siblings.where(position: position..coordinate.position).update_all("position = position + 1") - end - def remove_type_de_champ(stable_id) coordinate, tdc = coordinate_and_tdc(stable_id) @@ -129,7 +117,7 @@ class ProcedureRevision < ApplicationRecord types_de_champ_public.reset types_de_champ_private.reset - renumber(coordinate.siblings) + decr_siblings_above(coordinate.siblings, coordinate.position) coordinate end @@ -276,12 +264,24 @@ class ProcedureRevision < ApplicationRecord end end - def renumber(siblings) - siblings.to_a.compact.each.with_index do |sibling, position| - sibling.update_column(:position, position) - end + def incr_coordinates_above(siblings, position) + siblings.where("position >= ?", position).update_all("position = position + 1") end + def decr_siblings_above(coordinate) + coordinate.siblings.where("position >= ?", coordinate.position).update_all("position = position - 1") + end + + def decr_siblings_between(coordinate, position) + coordinate.siblings.where(position: coordinate.position..position).update_all("position = position - 1") + end + + def inc_siblings_between(coordinate, position) + coordinate.siblings.where(position: position..coordinate.position).update_all("position = position + 1") + end + + + def compare_revision_types_de_champ(from_coordinates, to_coordinates) if from_coordinates == to_coordinates [] From a9c0d3e7e9b70585782168fd82dd48f1c94e8ea1 Mon Sep 17 00:00:00 2001 From: Martin Date: Wed, 17 Jan 2024 16:12:25 +0100 Subject: [PATCH 4/8] tech(clean): avoid useless indirection, cleanup some code complexity, rubocopify --- app/models/procedure_revision.rb | 62 +++++++++++--------------- spec/models/procedure_revision_spec.rb | 12 ++--- 2 files changed, 32 insertions(+), 42 deletions(-) diff --git a/app/models/procedure_revision.rb b/app/models/procedure_revision.rb index 844b2d536..023c2d1dd 100644 --- a/app/models/procedure_revision.rb +++ b/app/models/procedure_revision.rb @@ -38,31 +38,18 @@ class ProcedureRevision < ApplicationRecord parent_coordinate, _ = coordinate_and_tdc(parent_stable_id) parent_id = parent_coordinate&.id - siblings = if parent_coordinate - parent_coordinate.revision_types_de_champ - elsif params['private'] || params[:private] - revision_types_de_champ_private - else - revision_types_de_champ_public - end - - after_stable_id = params.delete(:after_stable_id) after_coordinate, _ = coordinate_and_tdc(after_stable_id) - if siblings.to_a.empty? # first element of the list, starts at 0 - position = 0.0 - elsif after_coordinate # middle of the list, between two items - position = after_coordinate.position + 1 - else # last element of the list, end with last position + 1 - position = siblings.to_a.last.position + 1.0 - end + siblings = siblings_for(parent_coordinate:, private_tdc: params[:private] || params['private']) tdc = TypeDeChamp.new(params) if tdc.save + position = next_position_for(after_coordinate:, siblings:) h = { type_de_champ: tdc, parent_id: parent_id, position: position } - incr_coordinates_above(siblings, position) - coordinate = revision_types_de_champ.create!(h) + + siblings.where("position >= ?", position).update_all("position = position + 1") + revision_types_de_champ.create!(h) end # they are not aware of the addition @@ -88,11 +75,12 @@ class ProcedureRevision < ApplicationRecord # [] def move_type_de_champ(stable_id, position) coordinate, _ = coordinate_and_tdc(stable_id) + siblings = coordinate.siblings if position > coordinate.position - decr_siblings_between(coordinate, position) + siblings.where(position: coordinate.position..position).update_all("position = position - 1") else - inc_siblings_between(coordinate, position) + siblings.where(position: position..coordinate.position).update_all("position = position + 1") end coordinate.update_column(:position, position) @@ -113,12 +101,12 @@ class ProcedureRevision < ApplicationRecord children.each(&:destroy_if_orphan) tdc.destroy_if_orphan + 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 - decr_siblings_above(coordinate.siblings, coordinate.position) - coordinate end @@ -264,24 +252,26 @@ class ProcedureRevision < ApplicationRecord end end - def incr_coordinates_above(siblings, position) - siblings.where("position >= ?", position).update_all("position = position + 1") + def siblings_for(parent_coordinate: nil, private_tdc: false) + if parent_coordinate + parent_coordinate.revision_types_de_champ + elsif private_tdc + revision_types_de_champ_private + else + revision_types_de_champ_public + end end - def decr_siblings_above(coordinate) - coordinate.siblings.where("position >= ?", coordinate.position).update_all("position = position - 1") + def next_position_for(siblings:, after_coordinate: nil) + if siblings.to_a.empty? # first element of the list, starts at 0 + 0 + elsif after_coordinate # middle of the list, between two items + after_coordinate.position + 1 + else # last element of the list, end with last position + 1 + siblings.to_a.last.position + 1 + end end - def decr_siblings_between(coordinate, position) - coordinate.siblings.where(position: coordinate.position..position).update_all("position = position - 1") - end - - def inc_siblings_between(coordinate, position) - coordinate.siblings.where(position: position..coordinate.position).update_all("position = position + 1") - end - - - def compare_revision_types_de_champ(from_coordinates, to_coordinates) if from_coordinates == to_coordinates [] diff --git a/spec/models/procedure_revision_spec.rb b/spec/models/procedure_revision_spec.rb index b10dd641d..5ec369381 100644 --- a/spec/models/procedure_revision_spec.rb +++ b/spec/models/procedure_revision_spec.rb @@ -28,7 +28,7 @@ describe ProcedureRevision do it 'public' do expect { subject }.to change { draft.types_de_champ_public.size }.from(2).to(3) expect(draft.types_de_champ_public.last).to eq(subject) - expect(draft.revision_types_de_champ_public.map(&:position)).to eq([0,1,2]) + expect(draft.revision_types_de_champ_public.map(&:position)).to eq([0, 1, 2]) expect(last_coordinate.position).to eq(2) expect(last_coordinate.type_de_champ).to eq(subject) @@ -41,7 +41,7 @@ describe ProcedureRevision do it 'private' do expect { subject }.to change { draft.types_de_champ_private.count }.from(1).to(2) expect(draft.types_de_champ_private.last).to eq(subject) - expect(draft.revision_types_de_champ_private.map(&:position)).to eq([0,1]) + expect(draft.revision_types_de_champ_private.map(&:position)).to eq([0, 1]) expect(last_coordinate.position).to eq(1) end end @@ -52,7 +52,7 @@ describe ProcedureRevision do it do expect { subject }.to change { draft.reload.types_de_champ.count }.from(4).to(5) expect(draft.children_of(type_de_champ_repetition).last).to eq(subject) - expect(draft.children_of(type_de_champ_repetition).map(&:revision_type_de_champ).map(&:position)).to eq([0,1]) + expect(draft.children_of(type_de_champ_repetition).map(&:revision_type_de_champ).map(&:position)).to eq([0, 1]) expect(last_coordinate.position).to eq(1) @@ -81,7 +81,7 @@ describe ProcedureRevision do expect(draft.revision_types_de_champ_public.map(&:libelle)).to eq(['l1', 'l2']) subject expect(draft.revision_types_de_champ_public.reload.map(&:libelle)).to eq(['l1', 'in the middle', 'l2']) - expect(draft.revision_types_de_champ_public.map(&:position)).to eq([0,1,2]) + expect(draft.revision_types_de_champ_public.map(&:position)).to eq([0, 1, 2]) end end @@ -106,7 +106,7 @@ describe ProcedureRevision do stable_id_before = draft.revision_types_de_champ_public.map(&:stable_id) draft.move_type_de_champ(type_de_champ_public.stable_id, 2) draft.reload - expect(draft.revision_types_de_champ_public.map(&:position)).to eq([0,1,2,3]) + expect(draft.revision_types_de_champ_public.map(&:position)).to eq([0, 1, 2, 3]) expect(draft.types_de_champ_public.index(type_de_champ_public)).to eq(2) expect(draft.procedure.types_de_champ_for_procedure_presentation.not_repetition.index(type_de_champ_public)).to eq(2) end @@ -115,7 +115,7 @@ describe ProcedureRevision do expect(draft.types_de_champ_public.index(last_type_de_champ)).to eq(3) draft.move_type_de_champ(last_type_de_champ.stable_id, 0) draft.reload - expect(draft.revision_types_de_champ_public.map(&:position)).to eq([0,1,2,3]) + expect(draft.revision_types_de_champ_public.map(&:position)).to eq([0, 1, 2, 3]) expect(draft.types_de_champ_public.index(last_type_de_champ)).to eq(0) expect(draft.procedure.types_de_champ_for_procedure_presentation.not_repetition.index(last_type_de_champ)).to eq(0) end From 434cd770301bfd16d270e3d3810c50bd16008a8f Mon Sep 17 00:00:00 2001 From: Martin Date: Thu, 18 Jan 2024 10:13:16 +0100 Subject: [PATCH 5/8] 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') From 5c049a1c0f030721c816eaf8d6543da847b27bf8 Mon Sep 17 00:00:00 2001 From: Martin Date: Thu, 18 Jan 2024 11:03:55 +0100 Subject: [PATCH 6/8] feat(update_draft_revision_type_de_champs_task): force to use real position otherwise have to process parent_coordinates... --- .../update_draft_revision_type_de_champs_task.rb | 15 +-------------- ...ate_draft_revision_type_de_champs_task_spec.rb | 6 +++--- 2 files changed, 4 insertions(+), 17 deletions(-) diff --git a/app/tasks/maintenance/update_draft_revision_type_de_champs_task.rb b/app/tasks/maintenance/update_draft_revision_type_de_champs_task.rb index 5ce483abf..af03e79ec 100644 --- a/app/tasks/maintenance/update_draft_revision_type_de_champs_task.rb +++ b/app/tasks/maintenance/update_draft_revision_type_de_champs_task.rb @@ -29,8 +29,7 @@ module Maintenance fail "TypeDeChamp not found ! #{typed_id}" if stable_id.nil? tdc = revision.find_and_ensure_exclusive_use(stable_id) - - revision.move_type_de_champ(stable_id, compute_position(row, tdc.revision_type_de_champ)) + revision.move_type_de_champ(stable_id, Integer(row['new_position'])) tdc.update!( libelle: row["new_libelle"].strip, @@ -38,17 +37,5 @@ module Maintenance mandatory: row["new_required"] == "true" ) end - - private - - def compute_position(row, rtdc) - position = Integer(row["new_position"]) - - if rtdc.child? - position - rtdc.parent.position - 1 - else - position - end - end end end diff --git a/spec/tasks/maintenance/update_draft_revision_type_de_champs_task_spec.rb b/spec/tasks/maintenance/update_draft_revision_type_de_champs_task_spec.rb index d5332d6f7..57e01ef8d 100644 --- a/spec/tasks/maintenance/update_draft_revision_type_de_champs_task_spec.rb +++ b/spec/tasks/maintenance/update_draft_revision_type_de_champs_task_spec.rb @@ -21,9 +21,9 @@ module Maintenance demarche_id,id,new_libelle,new_description,new_required,new_position,delete_flag #{procedure.id},#{find_by_stable_id(12).to_typed_id},[NEW] Number,[NEW] Number desc,true,0, #{procedure.id},#{find_by_stable_id(13).to_typed_id},Bloc,[NEW] bloc desc,,1, - #{procedure.id},#{find_by_stable_id(132).to_typed_id},[NEW] RepNum,,true,2, - #{procedure.id},#{find_by_stable_id(131).to_typed_id},[NEW] RepText,,,3, - #{procedure.id},#{find_by_stable_id(11).to_typed_id},[supp] Text,,,4,true + #{procedure.id},#{find_by_stable_id(132).to_typed_id},[NEW] RepNum,,true,0, + #{procedure.id},#{find_by_stable_id(131).to_typed_id},[NEW] RepText,,,1, + #{procedure.id},#{find_by_stable_id(11).to_typed_id},[supp] Text,,,2,true CSV end From 89d2c57cb27356e9f456fddfaf8b1ab45db9db8e Mon Sep 17 00:00:00 2001 From: mfo Date: Mon, 22 Jan 2024 11:27:09 +0100 Subject: [PATCH 7/8] review(sim): some review suggestions Co-authored-by: LeSim --- app/models/procedure_revision.rb | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/app/models/procedure_revision.rb b/app/models/procedure_revision.rb index 2b1669a0e..ffe35c54a 100644 --- a/app/models/procedure_revision.rb +++ b/app/models/procedure_revision.rb @@ -41,14 +41,16 @@ class ProcedureRevision < ApplicationRecord after_stable_id = params.delete(:after_stable_id) after_coordinate, _ = coordinate_and_tdc(after_stable_id) - siblings = siblings_for(parent_coordinate:, private_tdc: params[:private] || params['private']) + siblings = siblings_for(parent_coordinate:, private_tdc: params[:private]) tdc = TypeDeChamp.new(params) if tdc.save + # moving all the impacted tdc down position = next_position_for(after_coordinate:, siblings:) - h = { type_de_champ: tdc, parent_id: parent_id, position: position } - siblings.where("position >= ?", position).update_all("position = position + 1") + + # insertion of the new tdc + h = { type_de_champ: tdc, parent_id: parent_id, position: position } revision_types_de_champ.create!(h) end From 0f194a5e8e73cc574e2a7332bd9ec62bedea03d7 Mon Sep 17 00:00:00 2001 From: Martin Date: Mon, 22 Jan 2024 14:54:09 +0100 Subject: [PATCH 8/8] refactor(procedure_revision): without renumber, use types_de_champ_public factory for ordered champs --- app/models/procedure_revision.rb | 6 ------ spec/models/dossier_spec.rb | 16 +++++++++++++--- 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/app/models/procedure_revision.rb b/app/models/procedure_revision.rb index ffe35c54a..1dc1041a5 100644 --- a/app/models/procedure_revision.rb +++ b/app/models/procedure_revision.rb @@ -90,12 +90,6 @@ 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) diff --git a/spec/models/dossier_spec.rb b/spec/models/dossier_spec.rb index 56f8368a8..50a25c194 100644 --- a/spec/models/dossier_spec.rb +++ b/spec/models/dossier_spec.rb @@ -1911,7 +1911,18 @@ describe Dossier, type: :model do describe "champs_for_export" do context 'with a unconditionnal procedure' do - let(:procedure) { create(:procedure, :with_type_de_champ, :with_datetime, :with_yes_no, :with_explication, :with_commune, :with_repetition, zones: [create(:zone)]) } + let(:procedure) { create(:procedure, types_de_champ_public:, zones: [create(:zone)]) } + let(:types_de_champ_public) do + [ + { type: :text }, + { type: :datetime }, + { type: :yes_no }, + { type: :explication }, + { type: :communes }, + { type: :repetition, children: [{ type: :text }] } + ] + end + let(:text_type_de_champ) { procedure.active_revision.types_de_champ_public.find { |type_de_champ| type_de_champ.type_champ == TypeDeChamp.type_champs.fetch(:text) } } let(:yes_no_type_de_champ) { procedure.active_revision.types_de_champ_public.find { |type_de_champ| type_de_champ.type_champ == TypeDeChamp.type_champs.fetch(:yes_no) } } let(:datetime_type_de_champ) { procedure.active_revision.types_de_champ_public.find { |type_de_champ| type_de_champ.type_champ == TypeDeChamp.type_champs.fetch(:datetime) } } @@ -1931,8 +1942,7 @@ describe Dossier, type: :model do procedure.publish! dossier procedure.draft_revision.remove_type_de_champ(text_type_de_champ.stable_id) - 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) + coordinate = procedure.draft_revision.add_type_de_champ(type_champ: TypeDeChamp.type_champs.fetch(:text), libelle: 'New text field', after_stable_id: repetition_champ.stable_id) 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')