demarches-normaliennes/app/controllers/admin/procedures_controller.rb

138 lines
4.2 KiB
Ruby
Raw Normal View History

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])
2015-11-09 17:15:51 +01:00
@types_de_champ = @procedure.types_de_champ.order(:order_place)
@types_de_piece_justificative = @procedure.types_de_piece_justificative.order(:libelle)
2015-10-26 15:54:20 +01:00
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-09 17:15:51 +01:00
process_new_types_de_champ_params
process_new_types_de_piece_justificative_params
2015-10-26 15:54:20 +01:00
flash.notice = 'Procédure enregistrée'
redirect_to admin_procedures_path
end
def update
2015-11-09 17:15:51 +01:00
# raise
2015-10-26 15:54:20 +01:00
@procedure = Procedure.find(params[:id])
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-09 17:15:51 +01:00
process_new_types_de_champ_params
process_update_types_de_champ_params
process_new_types_de_piece_justificative_params
process_update_types_de_piece_justificative_params
2015-10-26 15:54:20 +01:00
flash.notice = 'Préocédure modifiée'
redirect_to admin_procedures_path
end
private
2015-11-09 17:15:51 +01:00
def process_new_types_de_champ_params
unless params[:procedure][:new_type_de_champ].nil?
params[:procedure][:new_type_de_champ].each do |new_type_de_champ|
type_de_champ = TypeDeChamp.new
if new_type_de_champ[1]['_destroy'] == 'false'
save_new_type_de_champ type_de_champ, new_type_de_champ[1]
end
end
end
end
2015-11-09 17:15:51 +01:00
def process_update_types_de_champ_params
unless params[:procedure][:types_de_champ].nil?
params[:procedure][:types_de_champ].each do |type_de_champ|
tmp = TypeDeChamp.find(type_de_champ[0])
if type_de_champ[1]['_destroy'] == 'false'
save_new_type_de_champ tmp, type_de_champ[1]
elsif type_de_champ[1]['_destroy'] == 'true'
tmp.destroy
end
2015-10-28 18:09:47 +01:00
end
end
end
2015-11-09 17:15:51 +01:00
def process_new_types_de_piece_justificative_params
unless params[:procedure][:new_type_de_piece_justificative].nil?
params[:procedure][:new_type_de_piece_justificative].each do |new_type_de_piece_justificative|
type_de_pj = TypeDePieceJustificative.new
if new_type_de_piece_justificative[1]['_destroy'] == 'false'
save_new_type_de_piece_justificative type_de_pj, new_type_de_piece_justificative[1]
end
end
end
end
def process_update_types_de_piece_justificative_params
unless params[:procedure][:types_de_piece_justificative].nil?
params[:procedure][:types_de_piece_justificative].each do |type_de_piece_justificative|
tmp = TypeDePieceJustificative.find(type_de_piece_justificative[0])
if type_de_piece_justificative[1]['_destroy'] == 'false'
save_new_type_de_piece_justificative tmp, type_de_piece_justificative[1]
elsif type_de_piece_justificative[1]['_destroy'] == 'true'
tmp.destroy
end
end
end
end
def save_new_type_de_champ database_object, source
database_object.libelle = source[:libelle]
2015-11-09 17:15:51 +01:00
database_object.type_champs = source[:type_champs]
database_object.description = source[:description]
database_object.order_place = source[:order_place]
database_object.procedure = @procedure
database_object.save
end
2015-11-09 17:15:51 +01:00
def save_new_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)
2015-11-09 17:15:51 +01:00
#params.require(:procedure).permit(:libelle, :description, :organisation, :direction, :lien_demarche, :use_api_carto, types_de_champ_attributes: [:libelle, :description, :order_place, :type_champs])
2015-10-26 15:54:20 +01:00
end
end