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

217 lines
6.2 KiB
Ruby
Raw Normal View History

class Admin::ProceduresController < AdminController
2016-01-07 15:29:29 +01:00
include SmartListing::Helper::ControllerExtensions
helper SmartListing::Helper
2015-10-26 15:54:20 +01:00
before_action :retrieve_procedure, only: [:show, :edit]
2015-10-26 15:54:20 +01:00
def index
2016-01-07 15:29:29 +01:00
@procedures = smart_listing_create :procedures,
2017-07-10 16:48:32 +02:00
current_administrateur.procedures.published.not_archived.order(created_at: :desc),
2017-06-12 13:49:51 +02:00
partial: "admin/procedures/list",
array: true
2016-01-07 15:29:29 +01:00
active_class
2015-10-26 15:54:20 +01:00
end
def archived
2016-01-07 15:29:29 +01:00
@procedures = smart_listing_create :procedures,
2017-07-10 16:52:59 +02:00
current_administrateur.procedures.archived.order(created_at: :desc),
2017-06-12 13:49:51 +02:00
partial: "admin/procedures/list",
array: true
2016-01-07 15:29:29 +01:00
archived_class
2016-01-07 15:29:29 +01:00
render 'index'
end
def draft
@procedures = smart_listing_create :procedures,
2017-07-10 16:48:32 +02:00
current_administrateur.procedures.not_published.not_archived.order(created_at: :desc),
2017-06-12 13:49:51 +02:00
partial: "admin/procedures/list",
array: true
draft_class
render 'index'
end
2015-10-26 15:54:20 +01:00
def show
@facade = AdminProceduresShowFacades.new @procedure.decorate
end
def edit
2015-10-26 15:54:20 +01:00
end
2017-06-28 18:18:51 +02:00
def hide
procedure = Procedure.find(params[:id])
procedure.hide!
flash.notice = "Procédure supprimée, en cas d'erreur contactez nous : contact@tps.apientreprise.fr"
redirect_to admin_procedures_draft_path
end
2016-06-20 17:37:04 +02:00
def destroy
procedure = Procedure.find(params[:id])
return render json: {}, status: 401 if procedure.published? || procedure.archived?
procedure.destroy
flash.notice = 'Procédure supprimée'
redirect_to admin_procedures_draft_path
end
2015-10-26 15:54:20 +01:00
def new
@procedure ||= Procedure.new
@procedure.module_api_carto ||= ModuleAPICarto.new
2015-10-26 15:54:20 +01:00
end
def create
@procedure = Procedure.new(procedure_params)
@procedure.module_api_carto = ModuleAPICarto.new(create_module_api_carto_params) if @procedure.valid?
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
flash.notice = 'Procédure enregistrée'
2015-11-26 18:41:41 +01:00
redirect_to admin_procedure_types_de_champ_path(procedure_id: @procedure.id)
2015-10-26 15:54:20 +01:00
end
def update
@procedure = current_administrateur.procedures.find(params[:id])
2015-10-26 15:54:20 +01:00
unless @procedure.update_attributes(procedure_params)
2015-10-26 15:54:20 +01:00
flash.now.alert = @procedure.errors.full_messages.join('<br />').html_safe
return render 'edit'
2015-10-26 15:54:20 +01:00
end
2017-02-15 15:21:34 +01:00
flash.notice = 'Procédure modifiée'
redirect_to edit_admin_procedure_path(id: @procedure.id)
2015-10-26 15:54:20 +01:00
end
def publish
2016-06-30 10:24:01 +02:00
procedure = current_administrateur.procedures.find(params[:procedure_id])
2016-07-12 15:20:10 +02:00
new_procedure_path = ProcedurePath.new(
2016-06-30 10:24:01 +02:00
{
path: params[:procedure_path],
procedure: procedure,
administrateur: procedure.administrateur
})
2016-07-12 15:20:10 +02:00
if new_procedure_path.validate
new_procedure_path.delete
else
flash.alert = 'Lien de la procédure invalide'
return redirect_to admin_procedures_path
end
procedure_path = ProcedurePath.find_by_path(params[:procedure_path])
2016-07-12 15:20:10 +02:00
if procedure_path
if procedure_path.administrateur_id == current_administrateur.id
procedure_path.procedure.archive
procedure_path.delete
else
2016-06-30 12:33:43 +02:00
@mine = false
return render '/admin/procedures/publish', formats: 'js'
end
end
2016-07-12 15:20:10 +02:00
procedure.publish!(params[:procedure_path])
flash.notice = "Procédure publiée"
2016-06-30 12:33:43 +02:00
render js: "window.location = '#{admin_procedures_path}'"
rescue ActiveRecord::RecordNotFound
flash.alert = 'Procédure inéxistante'
redirect_to admin_procedures_path
end
2015-11-26 18:41:41 +01:00
def transfer
admin = Administrateur.find_by_email(params[:email_admin].downcase)
2016-07-22 12:25:23 +02:00
if admin.nil?
render '/admin/procedures/transfer', formats: 'js', status: 404
else
procedure = current_administrateur.procedures.find(params[:procedure_id])
clone_procedure = procedure.clone
2016-07-22 12:25:23 +02:00
clone_procedure.administrateur = admin
clone_procedure.save
flash.now.notice = "La procédure a correctement été clonée vers le nouvel administrateur."
render '/admin/procedures/transfer', formats: 'js', status: 200
end
end
def archive
2016-06-30 10:24:01 +02:00
procedure = current_administrateur.procedures.find(params[:procedure_id])
procedure.archive
flash.notice = "Procédure archivée"
redirect_to admin_procedures_path
rescue ActiveRecord::RecordNotFound
flash.alert = 'Procédure inéxistante'
redirect_to admin_procedures_path
2015-11-26 18:41:41 +01:00
end
2016-06-15 11:34:05 +02:00
def clone
2016-06-30 10:24:01 +02:00
procedure = current_administrateur.procedures.find(params[:procedure_id])
2016-06-15 11:34:05 +02:00
2016-06-30 10:24:01 +02:00
new_procedure = procedure.clone
2016-06-15 11:34:05 +02:00
if new_procedure
flash.notice = 'Procédure clonée'
redirect_to edit_admin_procedure_path(id: new_procedure.id)
else
2016-06-30 10:24:01 +02:00
flash.now.alert = procedure.errors.full_messages.join('<br />').html_safe
2016-06-15 11:34:05 +02:00
render 'index'
end
rescue ActiveRecord::RecordNotFound
flash.alert = 'Procédure inéxistante'
redirect_to admin_procedures_path
end
def active_class
@active_class = 'active'
end
def archived_class
@archived_class = 'active'
end
def draft_class
@draft_class = 'active'
end
def path_list
2016-11-07 17:08:33 +01:00
render json: ProcedurePath
.joins(', procedures')
2017-02-21 16:49:32 +01:00
.where("procedures.id = procedure_paths.procedure_id")
.where("procedures.archived" => false)
2016-11-07 17:08:33 +01:00
.where("path LIKE '%#{params[:request]}%'")
.pluck(:path, :administrateur_id)
.inject([]) {
|acc, value| acc.push({label: value.first, mine: value.second == current_administrateur.id})
}.to_json
end
2015-10-26 15:54:20 +01:00
private
def procedure_params
2017-03-14 10:45:59 +01:00
editable_params = [:libelle, :description, :organisation, :direction, :lien_site_web, :lien_notice, :euro_flag, :logo, :auto_archive_on]
if @procedure.try(:locked?)
2017-03-14 10:45:59 +01:00
params.require(:procedure).permit(*editable_params)
else
2017-03-14 10:45:59 +01:00
params.require(:procedure).permit(*editable_params, :lien_demarche, :cerfa_flag, :for_individual, :individual_with_siret, module_api_carto_attributes: [:id, :use_api_carto, :quartiers_prioritaires, :cadastre]).merge(administrateur_id: current_administrateur.id)
end
end
def create_module_api_carto_params
params.require(:procedure).require(:module_api_carto_attributes).permit(:id, :use_api_carto, :quartiers_prioritaires, :cadastre)
2015-10-26 15:54:20 +01:00
end
end