feat(move_type_de_champ): stop renumbering all procedure_revision_type_de_champ on move_type_de_champ

This commit is contained in:
Martin 2024-01-17 15:51:06 +01:00 committed by mfo
parent 4b57ed871a
commit 4d97c5bc8a
2 changed files with 18 additions and 8 deletions

View file

@ -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)

View file

@ -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