Add ProcedurePath#valid?

This commit is contained in:
Paul Chavard 2018-09-07 17:39:41 +01:00
parent f2f3c54147
commit e6485603c4
2 changed files with 6 additions and 11 deletions

View file

@ -94,17 +94,7 @@ class Admin::ProceduresController < AdminController
def publish def publish
procedure = current_administrateur.procedures.find(params[:procedure_id]) procedure = current_administrateur.procedures.find(params[:procedure_id])
new_procedure_path = ProcedurePath.new( if !ProcedurePath.valid?(procedure, params[:procedure_path])
{
path: params[:procedure_path],
procedure: procedure,
administrateur: procedure.administrateur
}
)
if new_procedure_path.validate
new_procedure_path.delete
else
flash.alert = 'Lien de la démarche invalide' flash.alert = 'Lien de la démarche invalide'
return redirect_to admin_procedures_path return redirect_to admin_procedures_path
end end

View file

@ -6,6 +6,11 @@ class ProcedurePath < ApplicationRecord
belongs_to :procedure belongs_to :procedure
belongs_to :administrateur belongs_to :administrateur
def self.valid?(procedure, path)
create_with(procedure: procedure, administrateur: procedure.administrateur)
.find_or_initialize_by(path: path).validate
end
def self.find_with_path(path) def self.find_with_path(path)
joins(:procedure) joins(:procedure)
.where.not(procedures: { aasm_state: :archivee }) .where.not(procedures: { aasm_state: :archivee })