From 96bbcd68eaa0cbcf990deb1bb7c79bcd86799719 Mon Sep 17 00:00:00 2001 From: Mathieu Magnin Date: Fri, 4 Oct 2024 11:07:16 +0200 Subject: [PATCH 1/2] [#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 From 80c1a1af2cd77e940e4d11c38e687348d9a10864 Mon Sep 17 00:00:00 2001 From: Mathieu Magnin Date: Tue, 8 Oct 2024 11:31:02 +0200 Subject: [PATCH 2/2] Remove not used param --- app/controllers/administrateurs/procedures_controller.rb | 2 +- app/models/concerns/procedure_path_concern.rb | 2 +- spec/models/procedure_spec.rb | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/app/controllers/administrateurs/procedures_controller.rb b/app/controllers/administrateurs/procedures_controller.rb index 38a2fdf2e..b296b08eb 100644 --- a/app/controllers/administrateurs/procedures_controller.rb +++ b/app/controllers/administrateurs/procedures_controller.rb @@ -286,7 +286,7 @@ module Administrateurs flash.alert = "La date limite de dépôt des dossiers doit être postérieure à la date du jour pour réactiver la procédure. #{view_context.link_to('Veuillez la modifier', edit_admin_procedure_path(@procedure))}" redirect_to admin_procedure_path(@procedure) else - @procedure.path = @procedure.suggested_path(current_administrateur) + @procedure.path = @procedure.suggested_path @current_administrateur = current_administrateur @closed_procedures = current_administrateur.procedures.with_discarded.closes.map { |p| ["#{p.libelle} (#{p.id})", p.id] }.to_h end diff --git a/app/models/concerns/procedure_path_concern.rb b/app/models/concerns/procedure_path_concern.rb index 9c983eab5..878a66593 100644 --- a/app/models/concerns/procedure_path_concern.rb +++ b/app/models/concerns/procedure_path_concern.rb @@ -29,7 +29,7 @@ module ProcedurePathConcern !path.match?(/[[:xdigit:]]{8}-[[:xdigit:]]{4}-[[:xdigit:]]{4}-[[:xdigit:]]{4}-[[:xdigit:]]{12}/) end - def suggested_path(administrateur) + def suggested_path if path_customized? return path end diff --git a/spec/models/procedure_spec.rb b/spec/models/procedure_spec.rb index 50db50eb1..51d11ce8f 100644 --- a/spec/models/procedure_spec.rb +++ b/spec/models/procedure_spec.rb @@ -1365,7 +1365,7 @@ describe Procedure do describe 'suggested_path' do let(:procedure) { create(:procedure, aasm_state: :publiee, libelle: 'Inscription au Collège', zones: [create(:zone)]) } - subject { procedure.suggested_path(procedure.administrateurs.first) } + subject { procedure.suggested_path } context 'when the path has been customized' do before { procedure.path = 'custom_path' }