feat(administrateurs/types_de_champ_controller#move_and_morph): add action to move and morph fields between two coordinates

This commit is contained in:
Martin 2024-01-03 10:30:29 +01:00
parent d4b4274d26
commit 4073804b00
4 changed files with 58 additions and 0 deletions

View file

@ -61,6 +61,24 @@ module Administrateurs
draft.move_type_de_champ(params[:stable_id], params[:position].to_i)
end
def move_and_morph
source_type_de_champ = draft.find_and_ensure_exclusive_use(params[:stable_id])
target_type_de_champ = draft.find_and_ensure_exclusive_use(params[:target_stable_id])
@coordinate = @source_coordinate = draft.coordinate_for(source_type_de_champ)
from = @coordinate.position
to = draft.coordinate_for(target_type_de_champ).position
@coordinate = draft.move_type_de_champ(@coordinate.stable_id, to)
@destroyed = @coordinate
@created = champ_component_from(@coordinate)
@morphed = @coordinate.siblings
if from > to # case of moved up, update components from target (> plus one) to origin
@morphed = @morphed.where("position > ?", to).where("position <= ?", from)
else # case of moved down, update components from origin up to target (< minus one)
@morphed = @morphed.where("position >= ?", from).where("position < ?", to)
end
@morphed = @morphed.map { |c| champ_component_from(c) }
end
def move_up
@coordinate = draft.move_up_type_de_champ(params[:stable_id])
@destroyed = @coordinate

View file

@ -0,0 +1,2 @@
= render partial: 'insert'

View file

@ -623,6 +623,7 @@ Rails.application.routes.draw do
resources :types_de_champ, only: [:create, :update, :destroy], param: :stable_id do
member do
patch :move
patch :move_and_morph
patch :move_up
patch :move_down
put :piece_justificative_template

View file

@ -154,6 +154,43 @@ describe Administrateurs::TypesDeChampController, type: :controller do
end
end
describe '#move_and_morph' do
# l1, l2, l3 => l2, l3, l1
context 'move and morph down' do
let(:params) do
{ procedure_id: procedure.id, stable_id: first_coordinate.stable_id, target_stable_id: third_coordinate.stable_id }
end
subject { patch :move_and_morph, params: params, format: :turbo_stream }
it do
is_expected.to have_http_status(:ok)
expect(assigns(:coordinate)).to eq(first_coordinate)
expect(assigns(:destroyed)).to eq(first_coordinate)
expect(extract_libelle(assigns(:created))).to eq(['l1', ['l2', 'l3']])
expect(morpheds).to eq([['l2', []], ['l3', ['l2']]])
end
end
# l1, l2, l3 => l3, l1, l2
context 'move and morph up' do
let(:params) do
{ procedure_id: procedure.id, stable_id: third_coordinate.stable_id, target_stable_id: first_coordinate.stable_id }
end
subject { patch :move_and_morph, params: params, format: :turbo_stream }
it do
is_expected.to have_http_status(:ok)
[first_coordinate, second_coordinate, third_coordinate].map(&:reload)
expect(assigns(:coordinate).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(morpheds).to eq([['l1', ['l3']], ['l2', ['l3', 'l1']]])
end
end
end
# l1, l2, l3 => l1, l3
# destroyed: l2, morphed: (l3, [l1])
describe '#destroy' do