feat(type_de_champ_editor): move champs after another champ.

This commit is contained in:
Martin 2024-03-10 07:44:13 +01:00 committed by mfo
parent b8a155a7ee
commit 44cdb14fb9
4 changed files with 23 additions and 11 deletions

View file

@ -1,3 +1,3 @@
= form_with(url: move_and_morph_admin_procedure_type_de_champ_path(@coordinate.revision.procedure, @coordinate.type_de_champ.stable_id), class: 'fr-ml-3w flex', method: :patch, data: { turbo: true }) do |f| = form_with(url: move_and_morph_admin_procedure_type_de_champ_path(@coordinate.revision.procedure, @coordinate.type_de_champ.stable_id), class: 'fr-ml-3w flex', method: :patch, data: { turbo: true }) do |f|
= label_tag :target_stable_id, "Déplacer ce champ à la place de ", for: describedby_id, class: 'flex align-center flex-no-shrink fr-mr-3w' = label_tag :target_stable_id, "Deplacer le champ après ", for: describedby_id, class: 'flex align-center flex-no-shrink fr-mr-3w'
= select_tag :target_stable_id, options_for_select(options), id: describedby_id, class: 'fr-select', aria: { discribedby: describedby_id }, data: { 'select-champ-position-template-target': 'select', selected: @coordinate.stable_id } = select_tag :target_stable_id, options_for_select(options), id: describedby_id, class: 'fr-select', aria: { discribedby: describedby_id }, data: { 'select-champ-position-template-target': 'select', selected: @coordinate.stable_id }

View file

@ -66,18 +66,13 @@ module Administrateurs
target_type_de_champ = draft.find_and_ensure_exclusive_use(params[:target_stable_id]) target_type_de_champ = draft.find_and_ensure_exclusive_use(params[:target_stable_id])
@coordinate = draft.coordinate_for(source_type_de_champ) @coordinate = draft.coordinate_for(source_type_de_champ)
from = @coordinate.position from = @coordinate.position
to = draft.coordinate_for(target_type_de_champ).position to = draft.coordinate_for(target_type_de_champ).position # move after
@coordinate = draft.move_type_de_champ(@coordinate.stable_id, to) @coordinate = draft.move_type_de_champ_after(@coordinate.stable_id, to)
reload_procedure_with_includes reload_procedure_with_includes
@coordinate = draft.revision_types_de_champ.find { _1.id == @coordinate.id }
@destroyed = @coordinate @destroyed = @coordinate
@created = champ_component_from(@coordinate) @created = champ_component_from(@coordinate)
@morphed = @coordinate.siblings @morphed = @coordinate.siblings
if from > to # case of moved up, update components from target (> plus one) to origin @morphed = @morphed.where("position > ?", [from, to].min).where("position <= ?", [from, to].max)
@morphed = @morphed.filter { _1.position > to && _1.position <= from }
else # case of moved down, update components from origin up to target (< minus one)
@morphed = @morphed.filter { _1.position >= from && _1.position < to }
end
@morphed = @morphed.map { |c| champ_component_from(c) } @morphed = @morphed.map { |c| champ_component_from(c) }
end end

View file

@ -82,6 +82,23 @@ class ProcedureRevision < ApplicationRecord
coordinate coordinate
end end
def move_type_de_champ_after(stable_id, position)
coordinate, _ = coordinate_and_tdc(stable_id)
siblings = coordinate.siblings
if position > coordinate.position
siblings.where(position: coordinate.position..position).update_all("position = position - 1")
coordinate.update_column(:position, position)
else
siblings.where(position: (position + 1)...coordinate.position).update_all("position = position + 1")
coordinate.update_column(:position, position + 1)
end
coordinate.reload
coordinate
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)

View file

@ -175,7 +175,7 @@ describe Administrateurs::TypesDeChampController, type: :controller do
end end
end end
# l1, l2, l3 => l3, l1, l2 # l1, l2, l3 => l1, l3, l2
context 'move and morph up' do context 'move and morph up' do
let(:params) do let(:params) do
{ procedure_id: procedure.id, stable_id: third_coordinate.stable_id, target_stable_id: first_coordinate.stable_id } { procedure_id: procedure.id, stable_id: third_coordinate.stable_id, target_stable_id: first_coordinate.stable_id }
@ -189,7 +189,7 @@ describe Administrateurs::TypesDeChampController, type: :controller do
expect(assigns(:coordinate).stable_id).to eq(first_coordinate.stable_id) expect(assigns(:coordinate).stable_id).to eq(first_coordinate.stable_id)
expect(assigns(:destroyed).stable_id).to eq(first_coordinate.stable_id) expect(assigns(:destroyed).stable_id).to eq(first_coordinate.stable_id)
expect(extract_libelle(assigns(:created))).to eq(['l3', []]) expect(extract_libelle(assigns(:created))).to eq(['l3', []])
expect(morpheds).to eq([['l1', ['l3']], ['l2', ['l3', 'l1']]]) expect(morpheds).to eq([['l3', ['l1']], ['l2', ['l1', 'l3']]])
end end
end end
end end