Add new types_de_champ#move api

This commit is contained in:
Paul Chavard 2019-03-20 16:27:30 +03:00
parent f5a66df802
commit 639facaf2a
4 changed files with 132 additions and 3 deletions

View file

@ -1,7 +1,7 @@
module NewAdministrateur
class TypesDeChampController < AdministrateurController
before_action :retrieve_procedure, only: [:create, :update, :destroy]
before_action :procedure_locked?, only: [:create, :update, :destroy]
before_action :retrieve_procedure, only: [:create, :update, :move, :destroy]
before_action :procedure_locked?, only: [:create, :update, :move, :destroy]
def create
type_de_champ = TypeDeChamp.new(type_de_champ_create_params)
@ -25,6 +25,15 @@ module NewAdministrateur
end
end
def move
type_de_champ = TypeDeChamp.where(procedure: @procedure).find(params[:id])
new_index = params[:order_place].to_i
@procedure.move_type_de_champ(type_de_champ, new_index)
head :no_content
end
def destroy
type_de_champ = TypeDeChamp.where(procedure: @procedure).find(params[:id])

View file

@ -410,8 +410,48 @@ class Procedure < ApplicationRecord
result
end
def move_type_de_champ(type_de_champ, new_index)
types_de_champ, collection_attribute_name = if type_de_champ.parent&.repetition?
if type_de_champ.parent.private?
[type_de_champ.parent.types_de_champ, :types_de_champ_private_attributes]
else
[type_de_champ.parent.types_de_champ, :types_de_champ_attributes]
end
elsif type_de_champ.private?
[self.types_de_champ_private, :types_de_champ_private_attributes]
else
[self.types_de_champ, :types_de_champ_attributes]
end
attributes = move_type_de_champ_attributes(types_de_champ.to_a, type_de_champ, new_index)
if type_de_champ.parent&.repetition?
attributes = [
{
id: type_de_champ.parent.id,
libelle: type_de_champ.parent.libelle,
types_de_champ_attributes: attributes
}
]
end
update!(collection_attribute_name => attributes)
end
private
def move_type_de_champ_attributes(types_de_champ, type_de_champ, new_index)
old_index = types_de_champ.index(type_de_champ)
types_de_champ.insert(new_index, types_de_champ.delete_at(old_index))
.map.with_index do |type_de_champ, index|
{
id: type_de_champ.id,
libelle: type_de_champ.libelle,
order_place: index
}
end
end
def claim_path_ownership!(path)
procedure = Procedure.joins(:administrateurs)
.where(administrateurs: { id: administrateur_ids })

View file

@ -372,7 +372,11 @@ Rails.application.routes.draw do
get 'annotations'
end
resources :types_de_champ, only: [:create, :update, :destroy]
resources :types_de_champ, only: [:create, :update, :destroy] do
member do
patch :move
end
end
end
resources :services, except: [:show] do

View file

@ -819,4 +819,80 @@ describe Procedure do
it { expect(procedure.usual_instruction_time).to be_nil }
end
end
describe '#move_type_de_champ' do
let(:procedure) { create(:procedure) }
context 'type_de_champ' do
let(:type_de_champ) { create(:type_de_champ_text, order_place: 0, procedure: procedure) }
let!(:type_de_champ1) { create(:type_de_champ_text, order_place: 1, procedure: procedure) }
let!(:type_de_champ2) { create(:type_de_champ_text, order_place: 2, procedure: procedure) }
it 'move down' do
procedure.move_type_de_champ(type_de_champ, 2)
type_de_champ.reload
procedure.reload
expect(procedure.types_de_champ.index(type_de_champ)).to eq(2)
expect(type_de_champ.order_place).to eq(2)
end
context 'repetition' do
let!(:type_de_champ_repetition) do
create(:type_de_champ_repetition, types_de_champ: [
type_de_champ,
type_de_champ1,
type_de_champ2
], procedure: procedure)
end
it 'move down' do
procedure.move_type_de_champ(type_de_champ, 2)
type_de_champ.reload
procedure.reload
expect(type_de_champ.parent.types_de_champ.index(type_de_champ)).to eq(2)
expect(type_de_champ.order_place).to eq(2)
end
context 'private' do
let!(:type_de_champ_repetition) do
create(:type_de_champ_repetition, types_de_champ: [
type_de_champ,
type_de_champ1,
type_de_champ2
], private: true, procedure: procedure)
end
it 'move down' do
procedure.move_type_de_champ(type_de_champ, 2)
type_de_champ.reload
procedure.reload
expect(type_de_champ.parent.types_de_champ.index(type_de_champ)).to eq(2)
expect(type_de_champ.order_place).to eq(2)
end
end
end
end
context 'private' do
let(:type_de_champ) { create(:type_de_champ_text, order_place: 0, private: true, procedure: procedure) }
let!(:type_de_champ1) { create(:type_de_champ_text, order_place: 1, private: true, procedure: procedure) }
let!(:type_de_champ2) { create(:type_de_champ_text, order_place: 2, private: true, procedure: procedure) }
it 'move down' do
procedure.move_type_de_champ(type_de_champ, 2)
type_de_champ.reload
procedure.reload
expect(procedure.types_de_champ_private.index(type_de_champ)).to eq(2)
expect(type_de_champ.order_place).to eq(2)
end
end
end
end