Add button delete for TypeDeChamps and TypeDePieceJustificative on Procedure form

+ refactor procedure.js
This commit is contained in:
Xavier J 2015-10-29 17:22:57 +01:00
parent 71f9455e36
commit afb11f429a
10 changed files with 345 additions and 79 deletions

View file

@ -1,33 +1,103 @@
var ready;
ready = function () {
var ready = function () {
$("#add_type_de_champs_procedure").on('click', function (e) {
add_new_type_de_champs();
add_new_type_de('champs');
e.stopPropagation();
return false;
return stop_event(e);
});
$("#add_type_de_piece_justificative_procedure").on('click', function (e) {
add_new_type_de_piece_justificative();
add_new_type_de('piece_justificative');
e.stopPropagation();
return false;
return stop_event(e);
});
add_delete_type_de_champs_listener_on_click("#liste_champs .btn-danger");
add_delete_type_de_champs_listener_on_click("#new_type_de_champs .btn-danger");
add_delete_type_de_piece_justificative_listener_on_click("#liste_piece_justificative .btn-danger");
add_delete_type_de_piece_justificative_listener_on_click("#new_type_de_piece_justificative .btn-danger");
};
$(document).ready(ready);
$(document).on('page:load', ready);
function add_new_type_de_champs() {
var index_id = "#type_de_champs_" + types_de_champs_index;
function stop_event(e) {
e.stopPropagation();
e.preventDefault();
return false;
}
$("#liste_champs").append($(index_id));
$("#new_type_de_champs").append($(index_id).clone());
types_de_champs_index++;
function add_delete_type_de_champs_listener_on_click(node_id) {
$(node_id).on('click', function (e) {
var index_type_de_champs = (e.target.id).replace('delete_type_de_champs_', '').replace('_procedure', '')
$("#new_type_de_champs .form-inline").attr('id', 'type_de_champs_' + types_de_champs_index);
delete_type_de_champs(index_type_de_champs);
return stop_event(e);
});
}
function add_delete_type_de_piece_justificative_listener_on_click(node_id) {
$(node_id).on('click', function (e) {
var index_type_de_piece_justificative = (e.target.id).replace('delete_type_de_piece_justificative_', '').replace('_procedure', '')
delete_type_de_piece_justificative(index_type_de_piece_justificative);
return stop_event(e);
});
}
function add_new_type_de(type_libelle) {
var CHAMPS = 0, PJ = 1, ERROR = -1;
if (is_champs_or_pj() == ERROR) return false;
function is_champs_or_pj() {
if (type_libelle == 'champs') return CHAMPS;
else if (type_libelle == 'piece_justificative') return PJ;
else return ERROR;
}
function which_index() {
return (is_champs_or_pj() == CHAMPS ? types_de_champs_index : types_de_piece_justificative_index)
}
$("#liste_" + type_libelle).append($("#type_de_" + type_libelle + "_" + which_index()));
$("#new_type_de_" + type_libelle).append($("#type_de_" + type_libelle + "_" + which_index()).clone());
$("#delete_type_de_" + type_libelle + "_" + which_index() + "_button").show();
if (is_champs_or_pj() == CHAMPS) {
types_de_champs_index++;
add_new_type_de_champs_params(which_index());
}
else if (is_champs_or_pj() == PJ) {
types_de_piece_justificative_index++;
add_new_type_de_piece_justificative_params(which_index());
}
$("#new_type_de_" + type_libelle + " .form-inline").attr('id', 'type_de_' + type_libelle + '_' + which_index());
$("#new_type_de_" + type_libelle + " #id_type_de_" + type_libelle + "").attr('name', 'type_de_' + type_libelle + '[' + which_index() + '][id_type_de_' + type_libelle + ']');
$("#new_type_de_" + type_libelle + " #id_type_de_" + type_libelle + "").val('');
$("#new_type_de_" + type_libelle + " #delete").attr('name', 'type_de_' + type_libelle + '[' + which_index() + '][delete]');
$("#new_type_de_" + type_libelle + " #delete").val('false');
$("#new_type_de_" + type_libelle + " #delete_type_de_" + type_libelle + "_" + (which_index() - 1) + "_button").attr('id', "delete_type_de_" + type_libelle + "_" + which_index() + "_button");
$("#new_type_de_" + type_libelle + " #delete_type_de_" + type_libelle + "_" + (which_index() - 1) + "_procedure").attr('id', "delete_type_de_" + type_libelle + "_" + which_index() + "_procedure");
if (is_champs_or_pj() == CHAMPS)
add_delete_type_de_champs_listener_on_click("#delete_type_de_champs_" + which_index() + "_procedure");
else if (is_champs_or_pj() == PJ)
add_delete_type_de_piece_justificative_listener_on_click("#delete_type_de_piece_justificative_" + which_index() + "_procedure");
$("#new_type_de_" + type_libelle + " #add_type_de_" + type_libelle + "_button").remove();
$("#new_type_de_" + type_libelle + " .form-inline").append($("#add_type_de_" + type_libelle + "_button"))
}
function add_new_type_de_champs_params() {
$("#new_type_de_champs #libelle").attr('name', 'type_de_champs[' + types_de_champs_index + '][libelle]');
$("#new_type_de_champs #libelle").val('');
@ -37,39 +107,24 @@ function add_new_type_de_champs() {
$("#new_type_de_champs #type_champs").attr('name', 'type_de_champs[' + types_de_champs_index + '][type]');
$("#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 #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 .form-inline").append($("#add_type_de_champs_button"))
$("#new_type_de_champs #order_place").val(types_de_champs_index + 1);
}
function add_new_type_de_piece_justificative() {
var index_id = "#type_de_piece_justificative_" + types_de_piece_justificative_index;
$("#liste_piece_justificative").append($(index_id));
$("#new_type_de_piece_justificative").append($(index_id).clone());
types_de_piece_justificative_index++;
$("#new_type_de_piece_justificative .form-inline").attr('id', 'type_de_piece_justificative_' + types_de_piece_justificative_index);
function add_new_type_de_piece_justificative_params() {
$("#new_type_de_piece_justificative #libelle").attr('name', 'type_de_piece_justificative[' + types_de_piece_justificative_index + '][libelle]');
$("#new_type_de_piece_justificative #libelle").val('');
$("#new_type_de_piece_justificative #description").attr('name', 'type_de_piece_justificative[' + types_de_piece_justificative_index + '][description]');
$("#new_type_de_piece_justificative #description").val('');
}
$("#new_type_de_piece_justificative #id_type_de_piece_justificative").attr('name', 'type_de_piece_justificative[' + types_de_piece_justificative_index + '][id_type_de_piece_justificative]');
$("#new_type_de_piece_justificative #id_type_de_piece_justificative").val('')
$("#new_type_de_piece_justificative #delete").attr('name', 'type_de_piece_justificative[' + types_de_piece_justificative_index + '][delete]');
$("#new_type_de_piece_justificative #delete").val('false')
function delete_type_de_champs(index) {
$("#type_de_champs_" + index).hide();
$("#type_de_champs_" + index + " #delete").val('true');
}
$("#new_type_de_piece_justificative #add_type_de_piece_justificative_button").remove();
$("#new_type_de_piece_justificative .form-inline").append($("#add_type_de_piece_justificative_button"))
function delete_type_de_piece_justificative(index) {
$("#type_de_piece_justificative_" + index).hide();
$("#type_de_piece_justificative_" + index + " #delete").val('true');
}

View file

@ -31,8 +31,8 @@ class Admin::ProceduresController < ApplicationController
return render 'new'
end
save_types_de_champs_params
save_types_de_piece_justificative_params
process_types_de_champs_params
process_types_de_piece_justificative_params
flash.notice = 'Procédure enregistrée'
@ -42,65 +42,85 @@ class Admin::ProceduresController < ApplicationController
def update
@procedure = Procedure.find(params[:id])
unless @procedure.update_attributes(create_params)
unless @procedure.update_attributes(create_procedure_params)
flash.now.alert = @procedure.errors.full_messages.join('<br />').html_safe
return render 'show'
end
save_types_de_champs_params
save_types_de_piece_justificative_params
process_types_de_champs_params
process_types_de_piece_justificative_params
flash.notice = 'Préocédure modifiée'
redirect_to admin_procedures_path
rescue ActiveRecord::RecordNotFound
flash.alert = 'Procédure inéxistante'
redirect_to admin_procedures_path
# rescue ActiveRecord::RecordNotFound
# flash.alert = 'Procédure inéxistante'
# redirect_to admin_procedures_path
end
private
def save_types_de_champs_params
def process_types_de_champs_params
unless params[:type_de_champs].nil? || params[:type_de_champs].size == 0
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
if type_de_champs[:delete] == 'true'
unless type_de_champs[:id_type_de_champs].nil? || type_de_champs[:id_type_de_champs] == ''
TypeDeChamps.destroy(type_de_champs[:id_type_de_champs])
end
else
type_de_champs_tmp = TypeDeChamps.find(type_de_champs[:id_type_de_champs])
if type_de_champs[:id_type_de_champs].nil? || type_de_champs[:id_type_de_champs] == ''
bdd_object = TypeDeChamps.new
else
bdd_object = TypeDeChamps.find(type_de_champs[:id_type_de_champs])
end
save_type_de_champs bdd_object, type_de_champs
end
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 save_types_de_piece_justificative_params
def process_types_de_piece_justificative_params
unless params[:type_de_piece_justificative].nil? || params[:type_de_piece_justificative].size == 0
params[:type_de_piece_justificative].each do |index, type_de_piece_justificative|
if type_de_piece_justificative[:id_type_de_piece_justificative].nil? || type_de_piece_justificative[:id_type_de_piece_justificative] == ''
type_de_piece_justificative_tmp = TypeDePieceJustificative.new
if type_de_piece_justificative[:delete] == 'true'
unless type_de_piece_justificative[:id_type_de_piece_justificative].nil? || type_de_piece_justificative[:id_type_de_piece_justificative] == ''
TypeDePieceJustificative.destroy(type_de_piece_justificative[:id_type_de_piece_justificative])
end
else
type_de_piece_justificative_tmp = TypeDePieceJustificative.find(type_de_piece_justificative[:id_type_de_piece_justificative])
if type_de_piece_justificative[:id_type_de_piece_justificative].nil? || type_de_piece_justificative[:id_type_de_piece_justificative] == ''
bdd_object = TypeDePieceJustificative.new
else
bdd_object = TypeDePieceJustificative.find(type_de_piece_justificative[:id_type_de_piece_justificative])
end
save_type_de_piece_justificative bdd_object, type_de_piece_justificative
end
type_de_piece_justificative_tmp.libelle = type_de_piece_justificative[:libelle]
type_de_piece_justificative_tmp.description = type_de_piece_justificative[:description]
type_de_piece_justificative_tmp.procedure = @procedure
type_de_piece_justificative_tmp.save
end
end
end
def create_params
def save_type_de_champs database_object, source
database_object.libelle = source[:libelle]
database_object.type_champs = source[:type]
database_object.description = source[:description]
database_object.order_place = source[:order_place]
database_object.procedure = @procedure
database_object.save
end
def save_type_de_piece_justificative database_object, source
database_object.libelle = source[:libelle]
database_object.description = source[:description]
database_object.procedure = @procedure
database_object.save
end
def create_procedure_params
params.require(:procedure).permit(:libelle, :description, :organisation, :direction, :lien_demarche, :use_api_carto)
end
end

View file

@ -28,4 +28,8 @@
-if type_de_champs.nil?
.form-group#add_type_de_champs_button
%br &nbsp;
%button.form-control.btn.btn-success#add_type_de_champs_procedure Ajouter
%button.form-control.btn.btn-success#add_type_de_champs_procedure Ajouter
.form-group{id: "delete_type_de_champs_#{index}_button", style: ("display:none" if type_de_champs.nil?)}
%br &nbsp;
%button.form-control.btn.btn-danger{id: "delete_type_de_champs_#{index}_procedure"} X

View file

@ -15,4 +15,8 @@
-if type_de_piece_justificative.nil?
.form-group#add_type_de_piece_justificative_button
%br &nbsp;
%button.form-control.btn.btn-success#add_type_de_piece_justificative_procedure Ajouter
%button.form-control.btn.btn-success#add_type_de_piece_justificative_procedure Ajouter
.form-group{id: "delete_type_de_piece_justificative_#{index}_button", style: ("display:none" if type_de_piece_justificative.nil?)}
%br &nbsp;
%button.form-control.btn.btn-danger{id: "delete_type_de_piece_justificative_#{index}_procedure"} X

View file

@ -1 +0,0 @@
%h1 coucou

View file

@ -329,6 +329,43 @@ describe Admin::ProceduresController, type: :controller do
end
end
context 'when delete a type de champs' do
let(:types_de_champs_params) {
{'0' =>
{libelle: 'Champs de test editée',
type: 'number',
description: 'Description de test editée',
order_place: 1,
delete: 'true',
id_type_de_champs: procedure.types_de_champs.first.id}
}
}
it { expect(subject.types_de_champs.size).to eq(0) }
end
context 'when delete a type de champs present in database and a type champ not present in database' do
let(:types_de_champs_params) {
{'0' =>
{libelle: 'Champs de test editée',
type: 'number',
description: 'Description de test editée',
order_place: 1,
delete: 'true',
id_type_de_champs: procedure.types_de_champs.first.id},
'1' =>
{libelle: 'Champs de test editée',
type: 'number',
description: 'Description de test editée',
order_place: 1,
delete: 'true',
id_type_de_champs: ''}
}
}
it { expect(subject.types_de_champs.size).to eq(0) }
end
end
describe 'type_de_piece_justificative processing' do
@ -379,6 +416,44 @@ describe Admin::ProceduresController, type: :controller do
it { expect(subject.description).to eq(types_de_piece_justificative_params['0'][:description]) }
end
end
context 'when delete a type de piece justificative' do
let(:types_de_piece_justificative_params) {
{'0' =>
{libelle: 'PJ de test editée',
type: 'number',
description: 'Description de test editée',
order_place: 1,
delete: 'true',
id_type_de_piece_justificative: procedure.types_de_piece_justificative.first.id}
}
}
it { expect(subject.types_de_piece_justificative.size).to eq(1) }
end
context 'when delete a type de piece justificative present in database and a type piece justificative not present in database' do
let(:types_de_piece_justificative_params) {
{'0' =>
{libelle: 'PJ de test editée',
type: 'number',
description: 'Description de test editée',
order_place: 1,
delete: 'true',
id_type_de_piece_justificative: procedure.types_de_piece_justificative.first.id},
'1' =>
{libelle: 'PJ de test editée',
type: 'number',
description: 'Description de test editée',
order_place: 1,
delete: 'true',
id_type_de_piece_justificative: ''}
}
}
it { expect(subject.types_de_piece_justificative.size).to eq(1) }
end
end
end
end

View file

@ -47,6 +47,11 @@ feature 'add a new type de champs', js: true do
expect(page.find_by_id('type_de_champs_0').find_by_id('order_place', visible: false).value).to eq('1')
end
scenario 'the first line have new button delete' do
expect(page).to have_css('#delete_type_de_champs_0_button')
expect(page).to have_css('#delete_type_de_champs_0_procedure')
end
scenario 'the new line is empty' 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('description').value).to eq('')

View file

@ -1,6 +1,6 @@
require 'spec_helper'
feature 'add a new type de champs', js: true do
feature 'add a new type de piece justificative', js: true do
let(:administrateur) { create(:administrateur) }
before do
@ -12,7 +12,7 @@ feature 'add a new type de champs', js: true do
visit new_admin_procedure_path
end
scenario 'page have form to created new type de champs' do
scenario 'page have form to created new type de piece justificative' do
expect(page).to have_css('#type_de_piece_justificative_0')
expect(page).to have_css('input[name="type_de_piece_justificative[0][libelle]"]')
expect(page).to have_css('textarea[name="type_de_piece_justificative[0][description]"]')
@ -22,14 +22,14 @@ feature 'add a new type de champs', js: true do
expect(page).to have_css('#new_type_de_piece_justificative #add_type_de_piece_justificative_button')
end
context 'when user add a new champs type' do
context 'when user add a new piece justificative type' do
before do
page.find_by_id('type_de_piece_justificative_0').find_by_id('libelle').set 'Libelle de test'
page.find_by_id('type_de_piece_justificative_0').find_by_id('description').set 'Description de test'
page.click_on 'add_type_de_piece_justificative_procedure'
end
scenario 'a new champs type line is appeared with increment index id' do
scenario 'a new piece justificative type line is appeared with increment index id' do
expect(page).to have_css('#type_de_piece_justificative_1')
expect(page).to have_css('input[name="type_de_piece_justificative[1][libelle]"]')
expect(page).to have_css('textarea[name="type_de_piece_justificative[1][description]"]')

View file

@ -0,0 +1,52 @@
require 'spec_helper'
feature 'delete a type de champs form', js: true do
let(:administrateur) { create(:administrateur) }
before do
login_as administrateur, scope: :administrateur
end
context 'when user click on type de champs red X button' do
let!(:procedure) { create(:procedure, :with_type_de_champs) }
before do
visit admin_procedure_path id: procedure.id
end
context 'when user edit a type de champs already save in database' do
let(:type_de_champs) { procedure.types_de_champs.first }
before do
page.click_on 'delete_type_de_champs_0_procedure'
end
scenario 'form is mask for the user' do
expect(page.find_by_id('type_de_champs_0', visible: false).visible?).to be_falsey
end
scenario 'delete attribut of type de champs is turn to true' do
expect(page.find_by_id('type_de_champs_0', visible: false).find_by_id('delete', visible: false).value).to eq('true')
end
end
context 'when user edit a type de champs just add on the form page' do
before do
page.click_on 'add_type_de_champs_procedure'
page.click_on 'add_type_de_champs_procedure'
page.click_on 'delete_type_de_champs_1_procedure'
page.click_on 'delete_type_de_champs_2_procedure'
end
scenario 'form is mask for the user' do
expect(page.find_by_id('type_de_champs_1', visible: false).visible?).to be_falsey
expect(page.find_by_id('type_de_champs_2', visible: false).visible?).to be_falsey
end
scenario 'delete attribut of type de champs is turn to true' do
expect(page.find_by_id('type_de_champs_1', visible: false).find_by_id('delete', visible: false).value).to eq('true')
expect(page.find_by_id('type_de_champs_2', visible: false).find_by_id('delete', visible: false).value).to eq('true')
end
end
end
end

View file

@ -0,0 +1,52 @@
require 'spec_helper'
feature 'delete a type de piece_justificative form', js: true do
let(:administrateur) { create(:administrateur) }
before do
login_as administrateur, scope: :administrateur
end
context 'when user click on type de piece_justificative red X button' do
let!(:procedure) { create(:procedure, :with_two_type_de_piece_justificative) }
before do
visit admin_procedure_path id: procedure.id
end
context 'when user edit a type de piece_justificative already save in database' do
let(:type_de_piece_justificative) { procedure.types_de_piece_justificative.first }
before do
page.click_on 'delete_type_de_piece_justificative_0_procedure'
end
scenario 'form is mask for the user' do
expect(page.find_by_id('type_de_piece_justificative_0', visible: false).visible?).to be_falsey
end
scenario 'delete attribut of type de piece_justificative is turn to true' do
expect(page.find_by_id('type_de_piece_justificative_0', visible: false).find_by_id('delete', visible: false).value).to eq('true')
end
end
context 'when user edit a type de piece_justificative just add on the form page' do
before do
page.click_on 'add_type_de_piece_justificative_procedure'
page.click_on 'add_type_de_piece_justificative_procedure'
page.click_on 'delete_type_de_piece_justificative_2_procedure'
page.click_on 'delete_type_de_piece_justificative_3_procedure'
end
scenario 'form is mask for the user' do
expect(page.find_by_id('type_de_piece_justificative_2', visible: false).visible?).to be_falsey
expect(page.find_by_id('type_de_piece_justificative_3', visible: false).visible?).to be_falsey
end
scenario 'delete attribut of type de piece_justificative is turn to true' do
expect(page.find_by_id('type_de_piece_justificative_2', visible: false).find_by_id('delete', visible: false).value).to eq('true')
expect(page.find_by_id('type_de_piece_justificative_3', visible: false).find_by_id('delete', visible: false).value).to eq('true')
end
end
end
end