Change updated method for type de champs' procedure

This commit is contained in:
Xavier J 2015-10-28 15:08:23 +01:00
parent 4eecba4ce1
commit e673fe6ae2
6 changed files with 56 additions and 15 deletions

View file

@ -32,6 +32,12 @@ function add_new_type_de_champs() {
$("#new_type_de_champs #order_place").attr('name', 'type_de_champs[' + types_de_champs_index + '][order_place]'); $("#new_type_de_champs #order_place").attr('name', 'type_de_champs[' + types_de_champs_index + '][order_place]');
$("#new_type_de_champs #order_place").val(types_de_champs_index+1); $("#new_type_de_champs #order_place").val(types_de_champs_index+1);
$("#new_type_de_champs #id_type_de_champs").attr('name', 'type_de_champs[' + types_de_champs_index + '][id_type_de_champs]');
$("#new_type_de_champs #id_type_de_champs").val('')
$("#new_type_de_champs #delete").attr('name', 'type_de_champs[' + types_de_champs_index + '][delete]');
$("#new_type_de_champs #delete").val('false')
$("#new_type_de_champs #add_type_de_champs_button").remove(); $("#new_type_de_champs #add_type_de_champs_button").remove();
$("#new_type_de_champs .form-inline").append($("#add_type_de_champs_button")) $("#new_type_de_champs .form-inline").append($("#add_type_de_champs_button"))
} }

View file

@ -59,11 +59,14 @@ class Admin::ProceduresController < ApplicationController
private private
def save_types_de_champs_params def save_types_de_champs_params
TypeDeChamps.destroy_all(procedure: @procedure)
unless params[:type_de_champs].nil? || params[:type_de_champs].size == 0 unless params[:type_de_champs].nil? || params[:type_de_champs].size == 0
params[:type_de_champs].each do |index, type_de_champs| params[:type_de_champs].each do |index, type_de_champs|
if type_de_champs[:id_type_de_champs].nil? || type_de_champs[:id_type_de_champs] == ''
type_de_champs_tmp = TypeDeChamps.new type_de_champs_tmp = TypeDeChamps.new
else
type_de_champs_tmp = TypeDeChamps.find(type_de_champs[:id_type_de_champs])
end
type_de_champs_tmp.libelle = type_de_champs[:libelle] type_de_champs_tmp.libelle = type_de_champs[:libelle]
type_de_champs_tmp.type_champs = type_de_champs[:type] type_de_champs_tmp.type_champs = type_de_champs[:type]

View file

@ -22,6 +22,8 @@
=("#{ type_de_champs.description }" unless type_de_champs.nil? ) =("#{ type_de_champs.description }" unless type_de_champs.nil? )
%input#order_place{type: 'hidden', name: "type_de_champs[#{index}][order_place]", value: (("#{ type_de_champs.order_place}" unless type_de_champs.nil?) || index+1)} %input#order_place{type: 'hidden', name: "type_de_champs[#{index}][order_place]", value: (("#{ type_de_champs.order_place}" unless type_de_champs.nil?) || index+1)}
%input#id_type_de_champs{type: 'hidden', name: "type_de_champs[#{index}][id_type_de_champs]", value: (("#{ type_de_champs.id}" unless type_de_champs.nil?))}
%input#delete{type: 'hidden', name: "type_de_champs[#{index}][delete]", value: 'false'}
-if type_de_champs.nil? -if type_de_champs.nil?
.form-group#add_type_de_champs_button .form-group#add_type_de_champs_button

View file

@ -226,31 +226,55 @@ describe Admin::ProceduresController, type: :controller do
context 'when no type de champs is informed' do context 'when no type de champs is informed' do
let(:types_de_champs_params) { {} } let(:types_de_champs_params) { {} }
it { expect(subject.types_de_champs.size).to eq(0) } it { expect(subject.types_de_champs.size).to eq(1) }
end end
context 'when two types de champs are informed' do context 'when two types de champs are informed' do
it { expect(subject.types_de_champs.size).to eq(2) } it { expect(subject.types_de_champs.size).to eq(3) }
describe ' check types de champs attributs present into database' do describe ' check types de champs attributs added into database' do
subject { procedure.types_de_champs } subject { procedure.types_de_champs }
it { expect(subject[0].libelle).to eq(types_de_champs_params['0'][:libelle]) } it { expect(subject[1].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[1].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[1].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].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[2].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[2].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[2].description).to eq(types_de_champs_params['1'][:description]) }
it { expect(subject[1].order_place).to eq(types_de_champs_params['1'][:order_place]) } it { expect(subject[2].order_place).to eq(types_de_champs_params['1'][:order_place]) }
end end
end end
context 'when one of two types de champs have not a libelle' do context 'when one of two types de champs have not a libelle' do
let(:types_de_champs_params) { types_de_champs_params_errors } let(:types_de_champs_params) { types_de_champs_params_errors }
it { expect(subject.types_de_champs.size).to eq(2) }
end
context 'when one types de champs is edit' do
let(:types_de_champs_params) {
{'0' =>
{libelle: 'Champs de test editée',
type: 'number',
description: 'Description de test editée',
order_place: 1,
id_type_de_champs: procedure.types_de_champs.first.id}
}
}
it { expect(subject.types_de_champs.size).to eq(1) } it { expect(subject.types_de_champs.size).to eq(1) }
describe ' check types de champs attributs updated into database' do
subject { procedure.types_de_champs.first }
it { expect(subject.libelle).to eq(types_de_champs_params['0'][:libelle]) }
it { expect(subject.type_champs).to eq(types_de_champs_params['0'][:type]) }
it { expect(subject.description).to eq(types_de_champs_params['0'][:description]) }
it { expect(subject.order_place).to eq(types_de_champs_params['0'][:order_place]) }
end
end end
end end
end end

View file

@ -1,6 +1,6 @@
FactoryGirl.define do FactoryGirl.define do
factory :type_de_champs do factory :type_de_champs do
libelle 'Description' libelle 'Libellé'
description 'description de votre projet' description 'description de votre projet'
type_champs 'textarea' type_champs 'textarea'
order_place 1 order_place 1

View file

@ -18,6 +18,8 @@ feature 'add a new type de champs', js: true do
expect(page).to have_css('select[name="type_de_champs[0][type]"]') expect(page).to have_css('select[name="type_de_champs[0][type]"]')
expect(page).to have_css('textarea[name="type_de_champs[0][description]"]') expect(page).to have_css('textarea[name="type_de_champs[0][description]"]')
expect(page).to have_css('input[name="type_de_champs[0][order_place]"]', visible: false) expect(page).to have_css('input[name="type_de_champs[0][order_place]"]', visible: false)
expect(page).to have_css('input[name="type_de_champs[0][id_type_de_champs]"]', visible: false)
expect(page).to have_css('input[name="type_de_champs[0][delete]"]', visible: false)
expect(page).to have_css('#new_type_de_champs #add_type_de_champs_button') expect(page).to have_css('#new_type_de_champs #add_type_de_champs_button')
end end
@ -35,6 +37,8 @@ feature 'add a new type de champs', js: true do
expect(page).to have_css('select[name="type_de_champs[1][type]"]') expect(page).to have_css('select[name="type_de_champs[1][type]"]')
expect(page).to have_css('textarea[name="type_de_champs[1][description]"]') expect(page).to have_css('textarea[name="type_de_champs[1][description]"]')
expect(page).to have_css('input[name="type_de_champs[1][order_place]"]', visible: false) expect(page).to have_css('input[name="type_de_champs[1][order_place]"]', visible: false)
expect(page).to have_css('input[name="type_de_champs[1][id_type_de_champs]"]', visible: false)
expect(page).to have_css('input[name="type_de_champs[1][delete]"]', visible: false)
end end
scenario 'the first line is filled' do scenario 'the first line is filled' do
@ -47,6 +51,8 @@ feature 'add a new type de champs', js: true do
expect(page.find_by_id('type_de_champs_1').find_by_id('libelle').value).to eq('') expect(page.find_by_id('type_de_champs_1').find_by_id('libelle').value).to eq('')
expect(page.find_by_id('type_de_champs_1').find_by_id('description').value).to eq('') expect(page.find_by_id('type_de_champs_1').find_by_id('description').value).to eq('')
expect(page.find_by_id('type_de_champs_1').find_by_id('order_place', visible: false).value).to eq('2') expect(page.find_by_id('type_de_champs_1').find_by_id('order_place', visible: false).value).to eq('2')
expect(page.find_by_id('type_de_champs_1').find_by_id('id_type_de_champs', visible: false).value).to eq('')
expect(page.find_by_id('type_de_champs_1').find_by_id('delete', visible: false).value).to eq('false')
end end
scenario 'the button Ajouter is at side new line' do scenario 'the button Ajouter is at side new line' do