feat(move_type_de_champ): stop renumbering all procedure_revision_type_de_champ on move_type_de_champ
This commit is contained in:
parent
4b57ed871a
commit
4d97c5bc8a
2 changed files with 18 additions and 8 deletions
|
@ -85,14 +85,17 @@ class ProcedureRevision < ApplicationRecord
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# []
|
||||||
def move_type_de_champ(stable_id, position)
|
def move_type_de_champ(stable_id, position)
|
||||||
coordinate, _ = coordinate_and_tdc(stable_id)
|
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.reload
|
||||||
|
|
||||||
coordinate
|
coordinate
|
||||||
|
@ -102,6 +105,14 @@ class ProcedureRevision < ApplicationRecord
|
||||||
siblings.where("position >= ?", position).update_all("position = position + 1")
|
siblings.where("position >= ?", position).update_all("position = 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 remove_type_de_champ(stable_id)
|
def remove_type_de_champ(stable_id)
|
||||||
coordinate, tdc = coordinate_and_tdc(stable_id)
|
coordinate, tdc = coordinate_and_tdc(stable_id)
|
||||||
|
|
||||||
|
|
|
@ -103,20 +103,19 @@ describe ProcedureRevision do
|
||||||
context 'with 4 types de champ publiques' do
|
context 'with 4 types de champ publiques' do
|
||||||
it 'move down' do
|
it 'move down' do
|
||||||
expect(draft.types_de_champ_public.index(type_de_champ_public)).to eq(0)
|
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.move_type_de_champ(type_de_champ_public.stable_id, 2)
|
||||||
draft.reload
|
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.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)
|
expect(draft.procedure.types_de_champ_for_procedure_presentation.not_repetition.index(type_de_champ_public)).to eq(2)
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'move up' do
|
it 'move up' do
|
||||||
expect(draft.types_de_champ_public.index(last_type_de_champ)).to eq(3)
|
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.move_type_de_champ(last_type_de_champ.stable_id, 0)
|
||||||
draft.reload
|
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.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)
|
expect(draft.procedure.types_de_champ_for_procedure_presentation.not_repetition.index(last_type_de_champ)).to eq(0)
|
||||||
end
|
end
|
||||||
|
|
Loading…
Add table
Reference in a new issue