add move down for type de champ controller and refactor

This commit is contained in:
Tanguy PATTE 2015-11-19 11:37:01 +01:00
parent 0503e2f278
commit fde504b54d
5 changed files with 86 additions and 9 deletions

View file

@ -22,16 +22,19 @@ class Admin::TypesDeChampController < AdminController
end end
def move_up def move_up
index = params[:index].to_i index = params[:index].to_i - 1
if @procedure.types_de_champ.count < 2 || index < 1 if @procedure.switch_types_de_champ index
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 render 'show', format: :js
else
render json: {}, status: 400
end
end
def move_down
if @procedure.switch_types_de_champ params[:index].to_i
render 'show', format: :js
else
render json: {}, status: 400
end end
end end

View file

@ -12,4 +12,15 @@ class Procedure < ActiveRecord::Base
def types_de_champ_ordered def types_de_champ_ordered
types_de_champ.order(:order_place) types_de_champ.order(:order_place)
end end
def switch_types_de_champ index_of_first_element
return false if index_of_first_element < 0
types_de_champ_tmp = types_de_champ_ordered
nb_types_de_champ = types_de_champ_tmp.count
return false if index_of_first_element == nb_types_de_champ - 1
return false if types_de_champ_ordered.count < 1
types_de_champ_tmp[index_of_first_element].update_attributes(order_place: index_of_first_element + 1)
types_de_champ_tmp[index_of_first_element + 1].update_attributes(order_place: index_of_first_element)
true
end
end end

View file

@ -44,6 +44,7 @@ Rails.application.routes.draw do
resources :procedures do resources :procedures do
resource :types_de_champ, only: [:show, :update] do resource :types_de_champ, only: [:show, :update] do
post '/:index/move_up' => 'types_de_champ#move_up' post '/:index/move_up' => 'types_de_champ#move_up'
post '/:index/move_down' => 'types_de_champ#move_down'
end end
resources :types_de_champ, only: [:destroy] resources :types_de_champ, only: [:destroy]
resource :pieces_justificatives, only: [:edit, :update] resource :pieces_justificatives, only: [:edit, :update]

View file

@ -138,4 +138,38 @@ describe Admin::TypesDeChampController, type: :controller do
end end
end end
end end
describe 'POST #move_down' do
let(:request) { post :move_down, procedure_id: procedure.id, index: index, format: :js }
subject { request }
let(:index) { 0 }
let(:procedure) { create(:procedure) }
context 'when procedure have no type de champ' do
it { expect(subject.status).to eq(400) }
end
context 'when procedure have only one type de champ' do
let!(:type_de_champ_0) { create(:type_de_champ, procedure: procedure) }
it { expect(subject.status).to eq(400) }
end
context 'when procedure have 2 type de champ' do
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) }
context 'when index represent last type_de_champ' do
let(:index) { 1 }
it { expect(subject.status).to eq(400) }
end
context 'when index does not represent last type_de_champ' do
let(:index) { 0 }
it { expect(subject.status).to eq(200) }
it { expect(subject).to render_template('show') }
it 'changes order place' do
request
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 end

View file

@ -44,4 +44,32 @@ describe Procedure do
it { expect(subject.first).to eq(type_de_champ_1) } it { expect(subject.first).to eq(type_de_champ_1) }
it { expect(subject.last).to eq(type_de_champ_0) } it { expect(subject.last).to eq(type_de_champ_0) }
end end
describe '#switch_types_de_champ' do
let(:procedure) { create(:procedure) }
let(:index) { 0 }
subject { procedure.switch_types_de_champ index}
context 'when procedure have no types_de_champ' do
it { expect(subject).to eq(false) }
end
context 'when procedure have 2 types de champ' do
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) }
context 'when index is not the last element' do
it { expect(subject).to eq(true) }
it 'switch order place' do
procedure.switch_types_de_champ index
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
context 'when index is the last element' do
let(:index) { 1 }
it { expect(subject).to eq(false) }
end
end
end
end end