feat(add_type_de_champ): stop renumbering all procedure_revision_type_de_champ
This commit is contained in:
parent
043ffef79a
commit
4b57ed871a
2 changed files with 31 additions and 6 deletions
|
@ -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)
|
||||
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
Loading…
Reference in a new issue