Back : save types de champs for procedure OK

This commit is contained in:
Xavier J 2015-10-28 11:53:16 +01:00
parent eb69e4f80f
commit 4eecba4ce1
3 changed files with 123 additions and 3 deletions

View file

@ -31,6 +31,8 @@ class Admin::ProceduresController < ApplicationController
return render 'new'
end
save_types_de_champs_params
flash.notice = 'Procédure enregistrée'
redirect_to admin_procedures_path
@ -44,6 +46,8 @@ class Admin::ProceduresController < ApplicationController
return render 'show'
end
save_types_de_champs_params
flash.notice = 'Préocédure modifiée'
redirect_to admin_procedures_path
@ -54,6 +58,24 @@ class Admin::ProceduresController < ApplicationController
private
def save_types_de_champs_params
TypeDeChamps.destroy_all(procedure: @procedure)
unless params[:type_de_champs].nil? || params[:type_de_champs].size == 0
params[:type_de_champs].each do |index, type_de_champs|
type_de_champs_tmp = TypeDeChamps.new
type_de_champs_tmp.libelle = type_de_champs[:libelle]
type_de_champs_tmp.type_champs = type_de_champs[:type]
type_de_champs_tmp.description = type_de_champs[:description]
type_de_champs_tmp.order_place = type_de_champs[:order_place]
type_de_champs_tmp.procedure = @procedure
type_de_champs_tmp.save
end
end
end
def create_params
params.require(:procedure).permit(:libelle, :description, :organisation, :direction, :lien_demarche, :use_api_carto)
end

View file

@ -24,6 +24,34 @@ describe Admin::ProceduresController, type: :controller do
}
}
let(:types_de_champs_params) {
{'0' =>
{libelle: 'Champs de test',
type: 'number',
description: 'Description de test',
order_place: 1},
'1' =>
{libelle: 'Champs de test 2',
type: 'text',
description: 'Description de test 2',
order_place: 2}
}
}
let(:types_de_champs_params_errors) {
{'0' =>
{libelle: '',
type: 'number',
description: 'Description de test',
order_place: 1},
'1' =>
{libelle: 'Champs de test 2',
type: 'text',
description: 'Description de test 2',
order_place: 2}
}
}
before do
sign_in admin
end
@ -103,10 +131,47 @@ describe Admin::ProceduresController, type: :controller do
it { expect(flash[:alert]).to be_present }
end
end
describe 'type_de_champs processing' do
before do
post :create, procedure: procedure_params, type_de_champs: types_de_champs_params
end
subject { Procedure.last }
context 'when no type de champs is informed' do
let(:types_de_champs_params) { {} }
it { expect(subject.types_de_champs.size).to eq(0) }
end
context 'when two types de champs are informed' do
it { expect(subject.types_de_champs.size).to eq(2) }
describe ' check types de champs attributs present into database' do
subject { TypeDeChamps.all }
it { expect(subject[0].libelle).to eq(types_de_champs_params['0'][:libelle]) }
it { expect(subject[0].type_champs).to eq(types_de_champs_params['0'][:type]) }
it { expect(subject[0].description).to eq(types_de_champs_params['0'][:description]) }
it { expect(subject[0].order_place).to eq(types_de_champs_params['0'][:order_place]) }
it { expect(subject[1].libelle).to eq(types_de_champs_params['1'][:libelle]) }
it { expect(subject[1].type_champs).to eq(types_de_champs_params['1'][:type]) }
it { expect(subject[1].description).to eq(types_de_champs_params['1'][:description]) }
it { expect(subject[1].order_place).to eq(types_de_champs_params['1'][:order_place]) }
end
end
context 'when one of two types de champs have not a libelle' do
let(:types_de_champs_params) { types_de_champs_params_errors }
it { expect(subject.types_de_champs.size).to eq(1) }
end
end
end
describe 'PUT #update' do
let!(:procedure) { create(:procedure) }
let!(:procedure) { create(:procedure, :with_type_de_champs) }
context 'when administrateur is not connected' do
before do
@ -120,7 +185,7 @@ describe Admin::ProceduresController, type: :controller do
context 'when administrateur is connected' do
before do
put :update, id: procedure.id, procedure: procedure_params
put :update, id: procedure.id, procedure: procedure_params, type_de_champs: types_de_champs_params
procedure.reload
end
@ -155,6 +220,39 @@ describe Admin::ProceduresController, type: :controller do
it { expect(flash[:alert]).to be_present }
end
end
describe 'type_de_champs processing' do
subject { procedure }
context 'when no type de champs is informed' do
let(:types_de_champs_params) { {} }
it { expect(subject.types_de_champs.size).to eq(0) }
end
context 'when two types de champs are informed' do
it { expect(subject.types_de_champs.size).to eq(2) }
describe ' check types de champs attributs present into database' do
subject { procedure.types_de_champs }
it { expect(subject[0].libelle).to eq(types_de_champs_params['0'][:libelle]) }
it { expect(subject[0].type_champs).to eq(types_de_champs_params['0'][:type]) }
it { expect(subject[0].description).to eq(types_de_champs_params['0'][:description]) }
it { expect(subject[0].order_place).to eq(types_de_champs_params['0'][:order_place]) }
it { expect(subject[1].libelle).to eq(types_de_champs_params['1'][:libelle]) }
it { expect(subject[1].type_champs).to eq(types_de_champs_params['1'][:type]) }
it { expect(subject[1].description).to eq(types_de_champs_params['1'][:description]) }
it { expect(subject[1].order_place).to eq(types_de_champs_params['1'][:order_place]) }
end
end
context 'when one of two types de champs have not a libelle' do
let(:types_de_champs_params) { types_de_champs_params_errors }
it { expect(subject.types_de_champs.size).to eq(1) }
end
end
end
end
end

View file

@ -8,7 +8,7 @@ FactoryGirl.define do
after(:build) do |procedure, _evaluator|
type_de_champs = create(:type_de_champs)
procedure.type_de_champs << type_de_champs
procedure.types_de_champs << type_de_champs
end
end