From 96bbcd68eaa0cbcf990deb1bb7c79bcd86799719 Mon Sep 17 00:00:00 2001 From: Mathieu Magnin Date: Fri, 4 Oct 2024 11:07:16 +0200 Subject: [PATCH] [#9513] Move path related things in a ProcedurePathConcern --- app/models/concerns/procedure_path_concern.rb | 46 +++++++++++++++++++ app/models/procedure.rb | 38 +-------------- 2 files changed, 47 insertions(+), 37 deletions(-) create mode 100644 app/models/concerns/procedure_path_concern.rb diff --git a/app/models/concerns/procedure_path_concern.rb b/app/models/concerns/procedure_path_concern.rb new file mode 100644 index 000000000..9c983eab5 --- /dev/null +++ b/app/models/concerns/procedure_path_concern.rb @@ -0,0 +1,46 @@ +# frozen_string_literal: true + +module ProcedurePathConcern + extend ActiveSupport::Concern + + included do + validates :path, presence: true, format: { with: /\A[a-z0-9_\-]{3,200}\z/ }, uniqueness: { scope: [:path, :closed_at, :hidden_at, :unpublished_at], case_sensitive: false } + + after_initialize :ensure_path_exists + before_save :ensure_path_exists + + def ensure_path_exists + if self.path.blank? + self.path = SecureRandom.uuid + end + end + + def other_procedure_with_path(path) + Procedure.publiees + .where.not(id: self.id) + .find_by(path: path) + end + + def path_available?(path) + other_procedure_with_path(path).blank? + end + + def path_customized? + !path.match?(/[[:xdigit:]]{8}-[[:xdigit:]]{4}-[[:xdigit:]]{4}-[[:xdigit:]]{4}-[[:xdigit:]]{12}/) + end + + def suggested_path(administrateur) + if path_customized? + return path + end + slug = libelle&.parameterize&.first(50) + suggestion = slug + counter = 1 + while !path_available?(suggestion) + counter = counter + 1 + suggestion = "#{slug}-#{counter}" + end + suggestion + end + end +end diff --git a/app/models/procedure.rb b/app/models/procedure.rb index 90344edc9..3e449ef59 100644 --- a/app/models/procedure.rb +++ b/app/models/procedure.rb @@ -9,6 +9,7 @@ class Procedure < ApplicationRecord include ProcedureSVASVRConcern include ProcedureChorusConcern include ProcedurePublishConcern + include ProcedurePathConcern include PiecesJointesListConcern include ColumnsConcern @@ -232,7 +233,6 @@ class Procedure < ApplicationRecord validates :replaced_by_procedure_id, presence: true, if: :closing_reason_internal_procedure? - validates :path, presence: true, format: { with: /\A[a-z0-9_\-]{3,200}\z/ }, uniqueness: { scope: [:path, :closed_at, :hidden_at, :unpublished_at], case_sensitive: false } validates :duree_conservation_dossiers_dans_ds, allow_nil: false, numericality: { only_integer: true, @@ -294,8 +294,6 @@ class Procedure < ApplicationRecord before_save :update_juridique_required after_save :extend_conservation_for_dossiers - after_initialize :ensure_path_exists - before_save :ensure_path_exists after_create :ensure_defaut_groupe_instructeur include AASM @@ -331,30 +329,6 @@ class Procedure < ApplicationRecord dossiers.close_to_expiration.count end - def suggested_path(administrateur) - if path_customized? - return path - end - slug = libelle&.parameterize&.first(50) - suggestion = slug - counter = 1 - while !path_available?(suggestion) - counter = counter + 1 - suggestion = "#{slug}-#{counter}" - end - suggestion - end - - def other_procedure_with_path(path) - Procedure.publiees - .where.not(id: self.id) - .find_by(path: path) - end - - def path_available?(path) - other_procedure_with_path(path).blank? - end - def canonical_procedure_child?(procedure) !canonical_procedure || canonical_procedure == procedure || canonical_procedure == procedure.canonical_procedure end @@ -425,10 +399,6 @@ class Procedure < ApplicationRecord Flipper.enabled?(feature, self) end - def path_customized? - !path.match?(/[[:xdigit:]]{8}-[[:xdigit:]]{4}-[[:xdigit:]]{4}-[[:xdigit:]]{4}-[[:xdigit:]]{12}/) - end - def organisation_name service&.nom || organisation end @@ -802,12 +772,6 @@ class Procedure < ApplicationRecord end end - def ensure_path_exists - if self.path.blank? - self.path = SecureRandom.uuid - end - end - def extend_conservation_for_dossiers return if !previous_changes.include?(:duree_conservation_dossiers_dans_ds) before, after = duree_conservation_dossiers_dans_ds_previous_change