From 550ddc8d97ba7c2c3caea18257a267f716eec9f4 Mon Sep 17 00:00:00 2001 From: Paul Chavard Date: Thu, 17 May 2018 15:39:37 +0200 Subject: [PATCH] procedure AASM callbacks --- app/models/administrateur.rb | 4 +++ app/models/procedure.rb | 47 ++++++++++++++++++++++++++++++------ 2 files changed, 44 insertions(+), 7 deletions(-) diff --git a/app/models/administrateur.rb b/app/models/administrateur.rb index fbf62a854..e1063b294 100644 --- a/app/models/administrateur.rb +++ b/app/models/administrateur.rb @@ -101,6 +101,10 @@ class Administrateur < ApplicationRecord save end + def owns?(procedure) + id == procedure.administrateur_id + end + private def generate_api_token diff --git a/app/models/procedure.rb b/app/models/procedure.rb index 78078fdc6..113d7a60d 100644 --- a/app/models/procedure.rb +++ b/app/models/procedure.rb @@ -68,6 +68,46 @@ class Procedure < ApplicationRecord transitions from: :archivee, to: :hidden end end + + def after_publish(path) + now = Time.now + update( + test_started_at: now, + archived_at: nil, + published_at: now + ) + procedure_path = ProcedurePath.find_by(path: path) + + if procedure_path.present? + if procedure_path.procedure != self + procedure_path.procedure.archive! + procedure_path.update(procedure: self) + end + else + ProcedurePath.create(procedure: self, administrateur: administrateur, path: path) + end + end + + def after_archive + update(archived_at: Time.now) + end + + def after_hide + now = Time.now + update(hidden_at: now) + procedure_path&.hide!(self) + dossiers.update_all(hidden_at: now) + end + + def can_publish?(path) + procedure_path = ProcedurePath.find_by(path: path) + if procedure_path.present? + administrateur.owns?(procedure_path) + else + true + end + end + # Warning: dossier after_save build_default_champs must be removed # to save a dossier created from this method def new_dossier @@ -86,13 +126,6 @@ class Procedure < ApplicationRecord ProcedurePath.find_with_procedure(self) end - def hide! - now = DateTime.now - update(hidden_at: now, aasm_state: :hidden) - procedure_path&.hide!(self) - dossiers.update_all(hidden_at: now) - end - def path procedure_path.path if procedure_path.present? end