reorder the siblings after deletion

This commit is contained in:
simon lehericey 2022-05-11 10:59:48 +02:00
parent 5dd8bbe797
commit b6c2aa30f4
3 changed files with 65 additions and 0 deletions

View file

@ -97,6 +97,8 @@ class ProcedureRevision < ApplicationRecord
if tdc.revision_types_de_champ.empty?
tdc.destroy
end
reorder(coordinate.siblings)
end
def draft?
@ -152,6 +154,13 @@ class ProcedureRevision < ApplicationRecord
private
def reorder(siblings)
siblings.to_a.compact.each.with_index do |e, position|
e.update(position: position)
e.type_de_champ.update!(order_place: position)
end
end
def compare_attestation_template(from_at, to_at)
changes = []
if from_at.nil? && to_at.present?

View file

@ -28,6 +28,16 @@ class ProcedureRevisionTypeDeChamp < ApplicationRecord
type_de_champ.private?
end
def siblings
if parent_id.present?
revision.revision_types_de_champ.where(parent_id: parent_id).ordered
elsif private?
revision.revision_types_de_champ_private
else
revision.revision_types_de_champ_public
end
end
private
def set_position

View file

@ -124,6 +124,52 @@ describe ProcedureRevision do
end
end
context 'with multiple tdc' do
context 'in public tdc' do
let(:procedure) { create(:procedure, :with_type_de_champ, types_de_champ_count: 3) }
it 'reorders' do
expect(draft.revision_types_de_champ_public.pluck(:position)).to eq([0, 1, 2])
draft.remove_type_de_champ(draft.types_de_champ_public[1].stable_id)
expect(draft.revision_types_de_champ_public.pluck(:position)).to eq([0, 1])
end
end
context 'in repetition tdc' do
let(:procedure) { create(:procedure, :with_repetition) }
let!(:second_child) do
draft.add_type_de_champ({
type_champ: TypeDeChamp.type_champs.fetch(:text),
libelle: "second child",
parent_id: type_de_champ_repetition.stable_id
})
end
let!(:last_child) do
draft.add_type_de_champ({
type_champ: TypeDeChamp.type_champs.fetch(:text),
libelle: "last child",
parent_id: type_de_champ_repetition.stable_id
})
end
it 'reorders' do
children = draft.children_of(type_de_champ_repetition)
expect(children.pluck(:position)).to eq([0, 1, 2])
expect(children.pluck(:order_place)).to eq([0, 1, 2])
draft.remove_type_de_champ(children[1].stable_id)
children.reload
expect(children.pluck(:position)).to eq([0, 1])
expect(children.pluck(:order_place)).to eq([0, 1])
end
end
end
context 'for a type_de_champ_repetition' do
let(:procedure) { create(:procedure, :with_repetition) }
let!(:child) { child = draft.children_of(type_de_champ_repetition).first }