Merge pull request #2530 from tchak/refactor-publish

Refactor publish
This commit is contained in:
Paul Chavard 2018-09-11 10:51:35 +02:00 committed by GitHub
commit c5990bbb13
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 66 additions and 49 deletions

View file

@ -94,24 +94,12 @@ class Admin::ProceduresController < AdminController
def publish
procedure = current_administrateur.procedures.find(params[:procedure_id])
new_procedure_path = ProcedurePath.new(
{
path: params[:procedure_path],
procedure: procedure,
administrateur: procedure.administrateur
}
)
if new_procedure_path.validate
new_procedure_path.delete
else
if !ProcedurePath.valid?(procedure, params[:procedure_path])
flash.alert = 'Lien de la démarche invalide'
return redirect_to admin_procedures_path
end
if procedure.may_publish?(params[:procedure_path])
procedure.publish!(params[:procedure_path])
if procedure.publish_or_reopen!(params[:procedure_path])
flash.notice = "Démarche publiée"
redirect_to admin_procedures_path
else
@ -205,10 +193,7 @@ class Admin::ProceduresController < AdminController
def path_list
json_path_list = ProcedurePath
.joins(:procedure)
.where(procedures: { archived_at: nil })
.where("path LIKE ?", "%#{params[:request]}%")
.order(:id)
.find_with_path(params[:request])
.pluck(:path, :administrateur_id)
.map do |path, administrateur_id|
{

View file

@ -70,6 +70,9 @@ class Procedure < ApplicationRecord
event :publish, after: :after_publish, guard: :can_publish? do
transitions from: :brouillon, to: :publiee
end
event :reopen, after: :after_reopen, guard: :can_publish? do
transitions from: :archivee, to: :publiee
end
@ -84,33 +87,27 @@ class Procedure < ApplicationRecord
end
end
def after_publish(path)
now = Time.now
update(
test_started_at: now,
archived_at: nil,
published_at: now
)
procedure_path = ProcedurePath.find_by(path: path)
def publish_or_reopen!(path)
if archivee? && may_reopen?(path)
reopen!(path)
elsif may_publish?(path)
reset!
publish!(path)
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
ProcedurePath.create(procedure: self, administrateur: administrateur, path: path)
create_procedure_path!(administrateur: administrateur, path: path)
end
end
def after_archive
update(archived_at: Time.now)
end
def after_hide
now = Time.now
update(hidden_at: now)
procedure_path&.hide!
dossiers.update_all(hidden_at: now)
end
def reset!
if locked?
raise "Can not reset a locked procedure."
@ -132,15 +129,6 @@ class Procedure < ApplicationRecord
publiee? || archivee?
end
def can_publish?(path)
procedure_path = ProcedurePath.find_by(path: path)
if procedure_path.present?
administrateur.owns?(procedure_path)
else
true
end
end
# Warning: dossier after_save build_default_champs must be removed
# to save a dossier created from this method
def new_dossier
@ -372,6 +360,38 @@ class Procedure < ApplicationRecord
private
def can_publish?(path)
procedure_path = ProcedurePath.find_by(path: path)
if procedure_path.present?
administrateur.owns?(procedure_path)
else
true
end
end
def after_publish(path)
update!(published_at: Time.now)
publish_with_path!(path)
end
def after_archive
update!(archived_at: Time.now)
end
def after_hide
now = Time.now
update!(hidden_at: now)
procedure_path&.hide!
dossiers.update_all(hidden_at: now)
end
def after_reopen(path)
update!(published_at: Time.now, archived_at: nil)
publish_with_path!(path)
end
def update_juridique_required
self.juridique_required ||= (cadre_juridique.present? || deliberation.attached?)
true

View file

@ -1,17 +1,29 @@
class ProcedurePath < ApplicationRecord
validates :path, format: { with: /\A[a-z0-9_\-]{3,50}\z/ }, presence: true, allow_blank: false, allow_nil: false
validates :path, format: { with: /\A[a-z0-9_\-]{3,50}\z/ }, uniqueness: true, presence: true, allow_blank: false, allow_nil: false
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
def self.valid?(procedure, path)
create_with(procedure: procedure, administrateur: procedure.administrateur)
.find_or_initialize_by(path: path).validate
end
def self.find_with_path(path)
joins(:procedure)
.where.not(procedures: { aasm_state: :archivee })
.where("path LIKE ?", "%#{path}%")
.order(:id)
end
def hide!
destroy!
end
def publish!(new_procedure)
if procedure&.publiee?
if procedure&.publiee? && procedure != new_procedure
procedure.archive!
end
update!(procedure: new_procedure)