diff --git a/app/controllers/admin/types_de_champ_controller.rb b/app/controllers/admin/types_de_champ_controller.rb index 3a4d4bdaf..b025406f6 100644 --- a/app/controllers/admin/types_de_champ_controller.rb +++ b/app/controllers/admin/types_de_champ_controller.rb @@ -21,6 +21,20 @@ class Admin::TypesDeChampController < AdminController params.require(:procedure).permit(types_de_champ_attributes: [:libelle, :description, :order_place, :type_champ, :id]) end + def move_up + index = params[:index].to_i + if @procedure.types_de_champ.count < 2 || index < 1 + render json: {}, status: 400 + else + types_de_champ_to_move_down = @procedure.types_de_champ_ordered[index - 1] + types_de_champ_to_move_up = @procedure.types_de_champ_ordered[index] + types_de_champ_to_move_down.update_attributes(order_place: index) + types_de_champ_to_move_up.update_attributes(order_place: index - 1) + + render 'show', format: :js + end + end + private def retrieve_procedure diff --git a/config/routes.rb b/config/routes.rb index d6b9ca3a0..3167a6b9c 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -42,7 +42,9 @@ Rails.application.routes.draw do namespace :admin do get 'sign_in' => '/administrateurs/sessions#new' resources :procedures do - resource :types_de_champ, only: [:show, :update] + resource :types_de_champ, only: [:show, :update] do + post '/:index/move_up' => 'types_de_champ#move_up' + end resources :types_de_champ, only: [:destroy] resource :pieces_justificatives, only: [:edit, :update] end diff --git a/spec/controllers/admin/types_de_champ_controller_spec.rb b/spec/controllers/admin/types_de_champ_controller_spec.rb index 691697e99..a30cfca83 100644 --- a/spec/controllers/admin/types_de_champ_controller_spec.rb +++ b/spec/controllers/admin/types_de_champ_controller_spec.rb @@ -98,4 +98,44 @@ describe Admin::TypesDeChampController, type: :controller do it { expect(subject.status).to eq(404) } end end + + describe 'POST #move_up' do + subject { post :move_up, procedure_id: procedure.id, index: index, format: :js } + context 'when procedure have no type de champ' do + let(:index) { 0 } + let(:procedure) { create(:procedure) } + it { expect(subject.status).to eq(400) } + end + context 'when procedure have only one type de champ' do + let(:index) { 1 } + let(:procedure) { create(:procedure) } + let!(:type_de_champ) { create(:type_de_champ, procedure: procedure) } + it { expect(subject.status).to eq(400) } + end + context 'when procedure have tow type de champs' do + context 'when index == 0' do + let(:index) { 0 } + let(:procedure) { create(:procedure) } + let!(:type_de_champ_1) { create(:type_de_champ, procedure: procedure) } + let!(:type_de_champ_2) { create(:type_de_champ, procedure: procedure) } + it { expect(subject.status).to eq(400) } + end + context 'when index > 0' do + let(:index) { 1 } + let(:procedure) { create(:procedure) } + let!(:type_de_champ_0) { create(:type_de_champ, procedure: procedure, order_place: 0) } + let!(:type_de_champ_1) { create(:type_de_champ, procedure: procedure, order_place: 1) } + + it { expect(subject.status).to eq(200) } + it { expect(subject).to render_template('show') } + it 'changes order places' do + post :move_up, procedure_id: procedure.id, index: index, format: :js + type_de_champ_0.reload + type_de_champ_1.reload + expect(type_de_champ_0.order_place).to eq(1) + expect(type_de_champ_1.order_place).to eq(0) + end + end + end + end end