Refactor procedure model

This commit is contained in:
Paul Chavard 2018-10-25 17:41:48 +02:00
parent 641831864b
commit b42095363e

View file

@ -45,12 +45,12 @@ class Procedure < ApplicationRecord
scope :by_libelle, -> { order(libelle: :asc) } scope :by_libelle, -> { order(libelle: :asc) }
scope :created_during, -> (range) { where(created_at: range) } scope :created_during, -> (range) { where(created_at: range) }
scope :cloned_from_library, -> { where(cloned_from_library: true) } scope :cloned_from_library, -> { where(cloned_from_library: true) }
scope :avec_lien, -> { joins(:procedure_path) } scope :avec_lien, -> { where.not(path: nil) }
validates :libelle, presence: true, allow_blank: false, allow_nil: false validates :libelle, presence: true, allow_blank: false, allow_nil: false
validates :description, presence: true, allow_blank: false, allow_nil: false validates :description, presence: true, allow_blank: false, allow_nil: false
validate :check_juridique validate :check_juridique
validates :path, format: { with: /\A[a-z0-9_\-]{3,50}\z/ }, uniqueness: true, presence: true, allow_blank: false, allow_nil: true validates :path, format: { with: /\A[a-z0-9_\-]{3,50}\z/ }, uniqueness: { scope: :aasm_state, case_sensitive: false }, presence: true, allow_blank: false, allow_nil: true
# FIXME: remove duree_conservation_required flag once all procedures are converted to the new style # FIXME: remove duree_conservation_required flag once all procedures are converted to the new style
validates :duree_conservation_dossiers_dans_ds, allow_nil: false, numericality: { only_integer: true, greater_than_or_equal_to: 1, less_than_or_equal_to: MAX_DUREE_CONSERVATION }, if: :durees_conservation_required validates :duree_conservation_dossiers_dans_ds, allow_nil: false, numericality: { only_integer: true, greater_than_or_equal_to: 1, less_than_or_equal_to: MAX_DUREE_CONSERVATION }, if: :durees_conservation_required
validates :duree_conservation_dossiers_hors_ds, allow_nil: false, numericality: { only_integer: true, greater_than_or_equal_to: 0 }, if: :durees_conservation_required validates :duree_conservation_dossiers_hors_ds, allow_nil: false, numericality: { only_integer: true, greater_than_or_equal_to: 0 }, if: :durees_conservation_required
@ -100,19 +100,6 @@ class Procedure < ApplicationRecord
end end
end end
def publish_with_path!(path)
procedure_path = ProcedurePath
.where(administrateur: administrateur)
.find_by(path: path)
if procedure_path.present?
procedure_path.publish!(self)
else
create_procedure_path!(administrateur: administrateur, path: path)
end
update!(path: path)
end
def reset! def reset!
if locked? if locked?
raise "Can not reset a locked procedure." raise "Can not reset a locked procedure."
@ -125,14 +112,6 @@ class Procedure < ApplicationRecord
publiee_ou_archivee? publiee_ou_archivee?
end end
def path_available?(path)
!ProcedurePath.where.not(procedure: self).exists?(path: path)
end
def path_is_mine?(path)
ProcedurePath.where.not(procedure: self).mine?(administrateur, path)
end
# This method is needed for transition. Eventually this will be the same as brouillon?. # This method is needed for transition. Eventually this will be the same as brouillon?.
def brouillon_avec_lien? def brouillon_avec_lien?
Flipflop.publish_draft? && brouillon? && path.present? Flipflop.publish_draft? && brouillon? && path.present?
@ -156,10 +135,6 @@ class Procedure < ApplicationRecord
Dossier.new(procedure: self, champs: champs, champs_private: champs_private) Dossier.new(procedure: self, champs: champs, champs_private: champs_private)
end end
def path
read_attribute(:path) || procedure_path&.path
end
def default_path def default_path
libelle&.parameterize&.first(50) libelle&.parameterize&.first(50)
end end
@ -331,21 +306,48 @@ class Procedure < ApplicationRecord
mean_time(:en_instruction_at, :processed_at) mean_time(:en_instruction_at, :processed_at)
end end
def path_available?(path)
!Procedure.where.not(id: id).exists?(path: path)
end
def path_mine?(path)
Procedure.path_mine?(administrateur, path)
end
def self.path_mine?(administrateur, path)
where(administrateur: administrateur).exists?(path: path)
end
def self.find_with_path(path)
where.not(aasm_state: :archivee).where("path LIKE ?", "%#{path}%")
end
private private
def can_publish?(path) def claim_path_ownership!(path)
procedure_path = ProcedurePath.find_by(path: path) procedure = Procedure.where(administrateur: administrateur).find_by(path: path)
if procedure_path.present?
administrateur.owns?(procedure_path) if procedure&.publiee? && procedure != self
else procedure.archive!
true
end end
update!(path: path)
end
def can_publish?(path)
path_available?(path) || path_mine?(path)
end end
def after_publish(path) def after_publish(path)
update!(published_at: Time.zone.now) update!(published_at: Time.zone.now)
publish_with_path!(path) claim_path_ownership!(path)
end
def after_reopen(path)
update!(published_at: Time.zone.now, archived_at: nil)
claim_path_ownership!(path)
end end
def after_archive def after_archive
@ -355,16 +357,9 @@ class Procedure < ApplicationRecord
def after_hide def after_hide
now = Time.zone.now now = Time.zone.now
update!(hidden_at: now, path: nil) update!(hidden_at: now, path: nil)
procedure_path&.hide!
dossiers.update_all(hidden_at: now) dossiers.update_all(hidden_at: now)
end end
def after_reopen(path)
update!(published_at: Time.zone.now, archived_at: nil)
publish_with_path!(path)
end
def after_draft def after_draft
update!(published_at: nil) update!(published_at: nil)
end end