Explicitly pass current_administrateur on publish

This commit is contained in:
Paul Chavard 2019-03-06 18:09:55 +01:00
parent 0e4b6e1ba2
commit bb8ba75d22
4 changed files with 29 additions and 25 deletions

View file

@ -44,7 +44,7 @@ class Admin::ProceduresController < AdminController
def edit def edit
@path = @procedure.path || @procedure.default_path @path = @procedure.path || @procedure.default_path
@availability = @procedure.path_availability(@path) @availability = @procedure.path_availability(current_administrateur, @path)
end end
def destroy def destroy
@ -69,7 +69,7 @@ class Admin::ProceduresController < AdminController
def create def create
@procedure = Procedure.new(procedure_params.merge(administrateurs: [current_administrateur])) @procedure = Procedure.new(procedure_params.merge(administrateurs: [current_administrateur]))
@path = @procedure.path @path = @procedure.path
@availability = Procedure.path_availability([current_administrateur], @procedure.path) @availability = Procedure.path_availability(current_administrateur, @procedure.path)
if !@procedure.save if !@procedure.save
flash.now.alert = @procedure.errors.full_messages flash.now.alert = @procedure.errors.full_messages
@ -96,7 +96,7 @@ class Admin::ProceduresController < AdminController
flash.now.alert = @procedure.errors.full_messages flash.now.alert = @procedure.errors.full_messages
@path = procedure_params[:path] @path = procedure_params[:path]
if @path.present? if @path.present?
@availability = @procedure.path_availability(@path) @availability = @procedure.path_availability(current_administrateur, @path)
end end
render 'edit' render 'edit'
elsif @procedure.brouillon? elsif @procedure.brouillon?
@ -121,7 +121,7 @@ class Admin::ProceduresController < AdminController
procedure.path = nil procedure.path = nil
end end
if procedure.publish_or_reopen!(path) if procedure.publish_or_reopen!(current_administrateur, path)
flash.notice = "Démarche publiée" flash.notice = "Démarche publiée"
redirect_to admin_procedures_path redirect_to admin_procedures_path
else else
@ -233,9 +233,9 @@ class Admin::ProceduresController < AdminController
if procedure_id.present? if procedure_id.present?
procedure = current_administrateur.procedures.find(procedure_id) procedure = current_administrateur.procedures.find(procedure_id)
@availability = procedure.path_availability(path) @availability = procedure.path_availability(current_administrateur, path)
else else
@availability = Procedure.path_availability([current_administrateur], path) @availability = Procedure.path_availability(current_administrateur, path)
end end
end end

View file

@ -102,12 +102,12 @@ class Procedure < ApplicationRecord
end end
end end
def publish_or_reopen!(path) def publish_or_reopen!(administrateur, path)
if archivee? && may_reopen?(path) if archivee? && may_reopen?(administrateur, path)
reopen!(path) reopen!(administrateur, path)
elsif may_publish?(path) elsif may_publish?(administrateur, path)
reset! reset!
publish!(path) publish!(administrateur, path)
end end
end end
@ -340,11 +340,11 @@ class Procedure < ApplicationRecord
PATH_NOT_AVAILABLE_BROUILLON = :not_available_brouillon PATH_NOT_AVAILABLE_BROUILLON = :not_available_brouillon
PATH_CAN_PUBLISH = [PATH_AVAILABLE, PATH_AVAILABLE_PUBLIEE] PATH_CAN_PUBLISH = [PATH_AVAILABLE, PATH_AVAILABLE_PUBLIEE]
def path_availability(path) def path_availability(administrateur, path)
Procedure.path_availability(administrateurs, path, id) Procedure.path_availability(administrateur, path, id)
end end
def self.path_availability(administrateurs, path, exclude_id = nil) def self.path_availability(administrateur, path, exclude_id = nil)
if exclude_id.present? if exclude_id.present?
procedure = where.not(id: exclude_id).find_by(path: path) procedure = where.not(id: exclude_id).find_by(path: path)
else else
@ -353,7 +353,7 @@ class Procedure < ApplicationRecord
if procedure.blank? if procedure.blank?
PATH_AVAILABLE PATH_AVAILABLE
elsif administrateurs.any? { |administrateur| administrateur.owns?(procedure) } elsif administrateur.owns?(procedure)
if procedure.brouillon? if procedure.brouillon?
PATH_NOT_AVAILABLE_BROUILLON PATH_NOT_AVAILABLE_BROUILLON
else else
@ -402,17 +402,21 @@ class Procedure < ApplicationRecord
update!(path: path) update!(path: path)
end end
def can_publish?(path) def can_publish?(administrateur, path)
path_availability(path).in?(PATH_CAN_PUBLISH) path_availability(administrateur, path).in?(PATH_CAN_PUBLISH)
end end
def after_publish(path) def can_reopen?(administrateur, path)
path_availability(administrateur, path).in?(PATH_CAN_PUBLISH)
end
def after_publish(administrateur, path)
update!(published_at: Time.zone.now) update!(published_at: Time.zone.now)
claim_path_ownership!(path) claim_path_ownership!(path)
end end
def after_reopen(path) def after_reopen(administrateur, path)
update!(published_at: Time.zone.now, archived_at: nil) update!(published_at: Time.zone.now, archived_at: nil)
claim_path_ownership!(path) claim_path_ownership!(path)

View file

@ -37,7 +37,7 @@ FactoryBot.define do
after(:build) do |procedure, _evaluator| after(:build) do |procedure, _evaluator|
procedure.for_individual = true procedure.for_individual = true
procedure.types_de_champ << create(:type_de_champ, libelle: 'Texte obligatoire', mandatory: true) procedure.types_de_champ << create(:type_de_champ, libelle: 'Texte obligatoire', mandatory: true)
procedure.publish!(generate(:published_path)) procedure.publish!(procedure.administrateurs.first, generate(:published_path))
end end
end end
@ -135,13 +135,13 @@ FactoryBot.define do
trait :published do trait :published do
after(:build) do |procedure, _evaluator| after(:build) do |procedure, _evaluator|
procedure.publish!(generate(:published_path)) procedure.publish!(procedure.administrateurs.first, generate(:published_path))
end end
end end
trait :archived do trait :archived do
after(:build) do |procedure, _evaluator| after(:build) do |procedure, _evaluator|
procedure.publish!(generate(:published_path)) procedure.publish!(procedure.administrateurs.first, generate(:published_path))
procedure.archive! procedure.archive!
end end
end end
@ -150,14 +150,14 @@ FactoryBot.define do
# For now the behavior is the same than :archived # For now the behavior is the same than :archived
# (it may be different in the future though) # (it may be different in the future though)
after(:build) do |procedure, _evaluator| after(:build) do |procedure, _evaluator|
procedure.publish!(generate(:published_path)) procedure.publish!(procedure.administrateurs.first, generate(:published_path))
procedure.archive! procedure.archive!
end end
end end
trait :hidden do trait :hidden do
after(:build) do |procedure, _evaluator| after(:build) do |procedure, _evaluator|
procedure.publish!(generate(:published_path)) procedure.publish!(procedure.administrateurs.first, generate(:published_path))
procedure.hide! procedure.hide!
end end
end end

View file

@ -493,7 +493,7 @@ describe Procedure do
before do before do
Timecop.freeze(now) Timecop.freeze(now)
procedure.publish!("example-path") procedure.publish!(procedure.administrateurs.first, "example-path")
end end
after { Timecop.return } after { Timecop.return }