2015-10-26 15:54:20 +01:00
|
|
|
class Admin::ProceduresController < ApplicationController
|
|
|
|
before_action :authenticate_administrateur!
|
|
|
|
|
|
|
|
def index
|
|
|
|
@procedures = Procedure.all
|
|
|
|
end
|
|
|
|
|
|
|
|
def show
|
|
|
|
@procedure = Procedure.find(params[:id])
|
|
|
|
rescue ActiveRecord::RecordNotFound
|
|
|
|
flash.alert = 'Procédure inéxistante'
|
|
|
|
redirect_to admin_procedures_path
|
|
|
|
end
|
|
|
|
|
|
|
|
def new
|
|
|
|
@procedure ||= Procedure.new
|
|
|
|
end
|
|
|
|
|
|
|
|
def create
|
2015-11-04 17:27:01 +01:00
|
|
|
@procedure = Procedure.new(create_procedure_params)
|
2015-10-26 15:54:20 +01:00
|
|
|
|
|
|
|
unless @procedure.save
|
|
|
|
flash.now.alert = @procedure.errors.full_messages.join('<br />').html_safe
|
|
|
|
return render 'new'
|
|
|
|
end
|
|
|
|
|
2015-11-05 11:21:44 +01:00
|
|
|
process_types_de_champ_params
|
2015-10-29 17:22:57 +01:00
|
|
|
process_types_de_piece_justificative_params
|
2015-10-28 11:53:16 +01:00
|
|
|
|
2015-10-26 15:54:20 +01:00
|
|
|
flash.notice = 'Procédure enregistrée'
|
|
|
|
redirect_to admin_procedures_path
|
|
|
|
end
|
|
|
|
|
|
|
|
def update
|
|
|
|
@procedure = Procedure.find(params[:id])
|
|
|
|
|
2015-10-29 17:22:57 +01:00
|
|
|
unless @procedure.update_attributes(create_procedure_params)
|
2015-10-26 15:54:20 +01:00
|
|
|
flash.now.alert = @procedure.errors.full_messages.join('<br />').html_safe
|
|
|
|
return render 'show'
|
|
|
|
end
|
|
|
|
|
2015-11-05 11:21:44 +01:00
|
|
|
process_types_de_champ_params
|
2015-10-29 17:22:57 +01:00
|
|
|
process_types_de_piece_justificative_params
|
2015-10-28 11:53:16 +01:00
|
|
|
|
2015-10-26 15:54:20 +01:00
|
|
|
flash.notice = 'Préocédure modifiée'
|
|
|
|
redirect_to admin_procedures_path
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2015-11-05 11:21:44 +01:00
|
|
|
def process_types_de_champ_params
|
|
|
|
unless params[:type_de_champ].nil? || params[:type_de_champ].size == 0
|
|
|
|
params[:type_de_champ].each do |index, type_de_champ|
|
2015-10-28 11:53:16 +01:00
|
|
|
|
2015-11-05 11:21:44 +01:00
|
|
|
if type_de_champ[:delete] == 'true'
|
|
|
|
unless type_de_champ[:id_type_de_champ].nil? || type_de_champ[:id_type_de_champ] == ''
|
|
|
|
TypeDeChamp.destroy(type_de_champ[:id_type_de_champ])
|
2015-10-29 17:22:57 +01:00
|
|
|
end
|
|
|
|
else
|
2015-11-05 11:21:44 +01:00
|
|
|
if type_de_champ[:id_type_de_champ].nil? || type_de_champ[:id_type_de_champ] == ''
|
|
|
|
bdd_object = TypeDeChamp.new
|
2015-10-29 17:22:57 +01:00
|
|
|
else
|
2015-11-05 11:21:44 +01:00
|
|
|
bdd_object = TypeDeChamp.find(type_de_champ[:id_type_de_champ])
|
2015-10-29 17:22:57 +01:00
|
|
|
end
|
2015-10-28 11:53:16 +01:00
|
|
|
|
2015-11-05 11:21:44 +01:00
|
|
|
save_type_de_champ bdd_object, type_de_champ
|
2015-10-29 17:22:57 +01:00
|
|
|
end
|
2015-10-28 11:53:16 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-10-29 17:22:57 +01:00
|
|
|
def process_types_de_piece_justificative_params
|
2015-10-28 18:09:47 +01:00
|
|
|
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|
|
|
|
|
|
2015-10-29 17:22:57 +01:00
|
|
|
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
|
2015-10-28 18:09:47 +01:00
|
|
|
else
|
2015-10-29 17:22:57 +01:00
|
|
|
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
|
2015-10-28 18:09:47 +01:00
|
|
|
|
2015-10-29 17:22:57 +01:00
|
|
|
save_type_de_piece_justificative bdd_object, type_de_piece_justificative
|
|
|
|
end
|
2015-10-28 18:09:47 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-11-05 11:21:44 +01:00
|
|
|
def save_type_de_champ database_object, source
|
2015-10-29 17:22:57 +01:00
|
|
|
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
|
2015-10-26 15:54:20 +01:00
|
|
|
params.require(:procedure).permit(:libelle, :description, :organisation, :direction, :lien_demarche, :use_api_carto)
|
|
|
|
end
|
|
|
|
end
|