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

148 lines
4.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]
before_action :procedure_locked?, only: [:edit]
2015-10-26 15:54:20 +01:00
def index
2016-01-07 15:29:29 +01:00
@procedures = smart_listing_create :procedures,
2016-06-20 17:37:04 +02:00
current_administrateur.procedures.where(published: true, archived: false).order(created_at: :desc),
2016-01-07 15:29:29 +01:00
partial: "admin/procedures/list",
array: true
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,
2016-06-20 17:37:04 +02:00
current_administrateur.procedures.where(archived: true).order(created_at: :desc),
2016-01-07 15:29:29 +01:00
partial: "admin/procedures/list",
array: true
archived_class
2016-01-07 15:29:29 +01:00
render 'index'
end
def draft
@procedures = smart_listing_create :procedures,
2016-06-20 17:37:04 +02:00
current_administrateur.procedures.where(published: false, archived: false).order(created_at: :desc),
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
2016-01-07 15:29:29 +01:00
2015-10-26 15:54:20 +01:00
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
2015-11-04 17:27:01 +01:00
@procedure = Procedure.new(create_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(create_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
2015-10-26 15:54:20 +01:00
flash.notice = 'Préocédure modifiée'
redirect_to edit_admin_procedure_path(id: @procedure.id)
2015-10-26 15:54:20 +01:00
end
def publish
change_status({published: true})
end
2015-11-26 18:41:41 +01:00
def archive
2016-06-13 12:03:05 +02:00
change_status({archived: params[:archive]})
2015-11-26 18:41:41 +01:00
end
2016-06-15 11:34:05 +02:00
def clone
@procedure = current_administrateur.procedures.find(params[:procedure_id])
new_procedure = @procedure.clone
if new_procedure
flash.notice = 'Procédure clonée'
redirect_to edit_admin_procedure_path(id: new_procedure.id)
else
flash.now.alert = @procedure.errors.full_messages.join('<br />').html_safe
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
2015-10-26 15:54:20 +01:00
private
def create_procedure_params
2016-02-02 18:37:38 +01:00
params.require(:procedure).permit(:libelle, :description, :organisation, :direction, :lien_demarche, :euro_flag, :logo, :cerfa_flag, module_api_carto_attributes: [:id, :use_api_carto, :quartiers_prioritaires, :cadastre]).merge(administrateur_id: current_administrateur.id)
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
def change_status(status_options)
@procedure = current_administrateur.procedures.find(params[:procedure_id])
@procedure.update_attributes(status_options)
flash.notice = 'Procédure éditée'
redirect_to admin_procedures_path
rescue ActiveRecord::RecordNotFound
flash.alert = 'Procédure inéxistante'
redirect_to admin_procedures_path
end
2015-10-26 15:54:20 +01:00
end