[#9513] Move path related things in a ProcedurePathConcern
This commit is contained in:
parent
4b740f8f29
commit
96bbcd68ea
2 changed files with 47 additions and 37 deletions
46
app/models/concerns/procedure_path_concern.rb
Normal file
46
app/models/concerns/procedure_path_concern.rb
Normal file
|
@ -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
|
|
@ -9,6 +9,7 @@ class Procedure < ApplicationRecord
|
||||||
include ProcedureSVASVRConcern
|
include ProcedureSVASVRConcern
|
||||||
include ProcedureChorusConcern
|
include ProcedureChorusConcern
|
||||||
include ProcedurePublishConcern
|
include ProcedurePublishConcern
|
||||||
|
include ProcedurePathConcern
|
||||||
include PiecesJointesListConcern
|
include PiecesJointesListConcern
|
||||||
include ColumnsConcern
|
include ColumnsConcern
|
||||||
|
|
||||||
|
@ -232,7 +233,6 @@ class Procedure < ApplicationRecord
|
||||||
|
|
||||||
validates :replaced_by_procedure_id, presence: true, if: :closing_reason_internal_procedure?
|
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,
|
validates :duree_conservation_dossiers_dans_ds, allow_nil: false,
|
||||||
numericality: {
|
numericality: {
|
||||||
only_integer: true,
|
only_integer: true,
|
||||||
|
@ -294,8 +294,6 @@ class Procedure < ApplicationRecord
|
||||||
before_save :update_juridique_required
|
before_save :update_juridique_required
|
||||||
after_save :extend_conservation_for_dossiers
|
after_save :extend_conservation_for_dossiers
|
||||||
|
|
||||||
after_initialize :ensure_path_exists
|
|
||||||
before_save :ensure_path_exists
|
|
||||||
after_create :ensure_defaut_groupe_instructeur
|
after_create :ensure_defaut_groupe_instructeur
|
||||||
|
|
||||||
include AASM
|
include AASM
|
||||||
|
@ -331,30 +329,6 @@ class Procedure < ApplicationRecord
|
||||||
dossiers.close_to_expiration.count
|
dossiers.close_to_expiration.count
|
||||||
end
|
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)
|
def canonical_procedure_child?(procedure)
|
||||||
!canonical_procedure || canonical_procedure == procedure || canonical_procedure == procedure.canonical_procedure
|
!canonical_procedure || canonical_procedure == procedure || canonical_procedure == procedure.canonical_procedure
|
||||||
end
|
end
|
||||||
|
@ -425,10 +399,6 @@ class Procedure < ApplicationRecord
|
||||||
Flipper.enabled?(feature, self)
|
Flipper.enabled?(feature, self)
|
||||||
end
|
end
|
||||||
|
|
||||||
def path_customized?
|
|
||||||
!path.match?(/[[:xdigit:]]{8}-[[:xdigit:]]{4}-[[:xdigit:]]{4}-[[:xdigit:]]{4}-[[:xdigit:]]{12}/)
|
|
||||||
end
|
|
||||||
|
|
||||||
def organisation_name
|
def organisation_name
|
||||||
service&.nom || organisation
|
service&.nom || organisation
|
||||||
end
|
end
|
||||||
|
@ -802,12 +772,6 @@ class Procedure < ApplicationRecord
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def ensure_path_exists
|
|
||||||
if self.path.blank?
|
|
||||||
self.path = SecureRandom.uuid
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
def extend_conservation_for_dossiers
|
def extend_conservation_for_dossiers
|
||||||
return if !previous_changes.include?(:duree_conservation_dossiers_dans_ds)
|
return if !previous_changes.include?(:duree_conservation_dossiers_dans_ds)
|
||||||
before, after = duree_conservation_dossiers_dans_ds_previous_change
|
before, after = duree_conservation_dossiers_dans_ds_previous_change
|
||||||
|
|
Loading…
Reference in a new issue