Add new admin procedure update action

This commit is contained in:
Paul Chavard 2018-11-14 16:24:34 +01:00
parent d9d0b29cbf
commit e1a1a2b2ad
7 changed files with 73 additions and 5 deletions

View file

@ -1,5 +1,28 @@
module NewAdministrateur
class AdministrateurController < ApplicationController
before_action :authenticate_administrateur!
def retrieve_procedure
id = params[:procedure_id] || params[:id]
@procedure = current_administrateur.procedures.find(id)
rescue ActiveRecord::RecordNotFound
flash.alert = 'Démarche inexistante'
redirect_to admin_procedures_path
end
def procedure_locked?
if @procedure.locked?
flash.alert = 'Démarche verrouillée'
redirect_to admin_procedure_path(@procedure)
end
end
def reset_procedure
if @procedure.brouillon_avec_lien?
@procedure.reset!
end
end
end
end

View file

@ -1,10 +1,37 @@
module NewAdministrateur
class ProceduresController < AdministrateurController
before_action :retrieve_procedure, only: [:update]
before_action :procedure_locked?, only: [:update]
TYPE_DE_CHAMP_ATTRIBUTES = [
:_destroy,
:libelle,
:description,
:order_place,
:type_champ,
:id,
:mandatory,
:piece_justificative_template,
:quartiers_prioritaires,
:cadastres,
:parcelles_agricoles,
drop_down_list_attributes: [:value]
]
def apercu
@dossier = procedure_without_control.new_dossier
@tab = apercu_tab
end
def update
if @procedure.update(procedure_params)
flash.now.notice = 'Démarche enregistrée.'
reset_procedure
else
flash.now.alert = @procedure.errors.full_messages
end
end
private
def apercu_tab
@ -14,5 +41,12 @@ module NewAdministrateur
def procedure_without_control
Procedure.find(params[:id])
end
def procedure_params
params.required(:procedure).permit(
types_de_champ_attributes: TYPE_DE_CHAMP_ATTRIBUTES,
types_de_champ_private_attributes: TYPE_DE_CHAMP_ATTRIBUTES
)
end
end
end

View file

@ -1,6 +1,8 @@
class DropDownList < ApplicationRecord
belongs_to :type_de_champ
before_validation :clean_value
def options
result = value.split(/[\r\n]|[\r]|[\n]|[\n\r]/).reject(&:empty?)
result.blank? ? [] : [''] + result
@ -13,4 +15,12 @@ class DropDownList < ApplicationRecord
def multiple
type_de_champ.type_champ == TypeDeChamp.type_champs.fetch(:multiple_drop_down_list)
end
private
def clean_value
value = read_attribute(:value)
value = value ? value.split("\r\n").map(&:strip).join("\r\n") : ''
write_attribute(:value, value)
end
end

View file

@ -30,9 +30,9 @@ class Procedure < ApplicationRecord
has_one_attached :notice
has_one_attached :deliberation
accepts_nested_attributes_for :types_de_champ, :reject_if => proc { |attributes| attributes['libelle'].blank? }, :allow_destroy => true
accepts_nested_attributes_for :types_de_piece_justificative, :reject_if => proc { |attributes| attributes['libelle'].blank? }, :allow_destroy => true
accepts_nested_attributes_for :types_de_champ_private
accepts_nested_attributes_for :types_de_champ, reject_if: proc { |attributes| attributes['libelle'].blank? }, allow_destroy: true
accepts_nested_attributes_for :types_de_champ_private, reject_if: proc { |attributes| attributes['libelle'].blank? }, allow_destroy: true
accepts_nested_attributes_for :types_de_piece_justificative, reject_if: proc { |attributes| attributes['libelle'].blank? }, allow_destroy: true
mount_uploader :logo, ProcedureLogoUploader

View file

@ -83,7 +83,7 @@ class TypeDeChamp < ApplicationRecord
has_one_attached :piece_justificative_template
accepts_nested_attributes_for :drop_down_list
accepts_nested_attributes_for :drop_down_list, update_only: true
accepts_nested_attributes_for :types_de_champ, allow_destroy: true
validates :libelle, presence: true, allow_blank: false, allow_nil: false

View file

@ -0,0 +1 @@
<%= render_flash timeout: 2000 %>

View file

@ -357,7 +357,7 @@ Rails.application.routes.draw do
#
scope module: 'new_administrateur' do
resources :procedures, only: [] do
resources :procedures, only: [:update] do
member do
get 'apercu'
end