2018-03-06 13:44:29 +01:00
|
|
|
class ProcedurePath < ApplicationRecord
|
2018-09-07 18:43:37 +02:00
|
|
|
validates :path, format: { with: /\A[a-z0-9_\-]{3,50}\z/ }, uniqueness: true, presence: true, allow_blank: false, allow_nil: false
|
2016-06-24 16:41:44 +02:00
|
|
|
validates :administrateur_id, presence: true, allow_blank: false, allow_nil: false
|
|
|
|
validates :procedure_id, presence: true, allow_blank: false, allow_nil: false
|
|
|
|
|
|
|
|
belongs_to :procedure
|
|
|
|
belongs_to :administrateur
|
2018-05-17 15:32:36 +02:00
|
|
|
|
2018-09-07 18:39:41 +02:00
|
|
|
def self.valid?(procedure, path)
|
|
|
|
create_with(procedure: procedure, administrateur: procedure.administrateur)
|
|
|
|
.find_or_initialize_by(path: path).validate
|
|
|
|
end
|
|
|
|
|
2018-09-04 14:53:18 +02:00
|
|
|
def self.mine?(administrateur, path)
|
|
|
|
procedure_path = find_by(path: path)
|
|
|
|
procedure_path && administrateur.owns?(procedure_path)
|
|
|
|
end
|
|
|
|
|
2018-09-07 18:28:27 +02:00
|
|
|
def self.find_with_path(path)
|
|
|
|
joins(:procedure)
|
|
|
|
.where.not(procedures: { aasm_state: :archivee })
|
|
|
|
.where("path LIKE ?", "%#{path}%")
|
|
|
|
.order(:id)
|
|
|
|
end
|
|
|
|
|
2018-08-14 13:54:39 +02:00
|
|
|
def hide!
|
|
|
|
destroy!
|
2018-05-17 15:34:51 +02:00
|
|
|
end
|
2018-05-29 11:44:44 +02:00
|
|
|
|
|
|
|
def publish!(new_procedure)
|
2018-09-07 18:43:21 +02:00
|
|
|
if procedure&.publiee? && procedure != new_procedure
|
2018-08-14 13:54:39 +02:00
|
|
|
procedure.archive!
|
2018-05-29 11:44:44 +02:00
|
|
|
end
|
2018-08-14 13:54:39 +02:00
|
|
|
update!(procedure: new_procedure)
|
2018-05-29 11:44:44 +02:00
|
|
|
end
|
2017-04-04 15:27:04 +02:00
|
|
|
end
|