diff --git a/app/controllers/admin/procedures_controller.rb b/app/controllers/admin/procedures_controller.rb index 23972fc57..2a8b8dc6b 100644 --- a/app/controllers/admin/procedures_controller.rb +++ b/app/controllers/admin/procedures_controller.rb @@ -44,7 +44,7 @@ class Admin::ProceduresController < AdminController def edit @path = @procedure.path || @procedure.default_path - @availability = @procedure.path_availability(@path) + @availability = @procedure.path_availability(current_administrateur, @path) end def destroy @@ -69,7 +69,7 @@ class Admin::ProceduresController < AdminController def create @procedure = Procedure.new(procedure_params.merge(administrateurs: [current_administrateur])) @path = @procedure.path - @availability = Procedure.path_availability([current_administrateur], @procedure.path) + @availability = Procedure.path_availability(current_administrateur, @procedure.path) if !@procedure.save flash.now.alert = @procedure.errors.full_messages @@ -96,7 +96,7 @@ class Admin::ProceduresController < AdminController flash.now.alert = @procedure.errors.full_messages @path = procedure_params[:path] if @path.present? - @availability = @procedure.path_availability(@path) + @availability = @procedure.path_availability(current_administrateur, @path) end render 'edit' elsif @procedure.brouillon? @@ -121,7 +121,7 @@ class Admin::ProceduresController < AdminController procedure.path = nil end - if procedure.publish_or_reopen!(path) + if procedure.publish_or_reopen!(current_administrateur, path) flash.notice = "Démarche publiée" redirect_to admin_procedures_path else @@ -233,9 +233,9 @@ class Admin::ProceduresController < AdminController if procedure_id.present? procedure = current_administrateur.procedures.find(procedure_id) - @availability = procedure.path_availability(path) + @availability = procedure.path_availability(current_administrateur, path) else - @availability = Procedure.path_availability([current_administrateur], path) + @availability = Procedure.path_availability(current_administrateur, path) end end diff --git a/app/models/procedure.rb b/app/models/procedure.rb index 643f704cb..e1bb60838 100644 --- a/app/models/procedure.rb +++ b/app/models/procedure.rb @@ -102,12 +102,12 @@ class Procedure < ApplicationRecord end end - def publish_or_reopen!(path) - if archivee? && may_reopen?(path) - reopen!(path) - elsif may_publish?(path) + def publish_or_reopen!(administrateur, path) + if archivee? && may_reopen?(administrateur, path) + reopen!(administrateur, path) + elsif may_publish?(administrateur, path) reset! - publish!(path) + publish!(administrateur, path) end end @@ -340,11 +340,11 @@ class Procedure < ApplicationRecord PATH_NOT_AVAILABLE_BROUILLON = :not_available_brouillon PATH_CAN_PUBLISH = [PATH_AVAILABLE, PATH_AVAILABLE_PUBLIEE] - def path_availability(path) - Procedure.path_availability(administrateurs, path, id) + def path_availability(administrateur, path) + Procedure.path_availability(administrateur, path, id) end - def self.path_availability(administrateurs, path, exclude_id = nil) + def self.path_availability(administrateur, path, exclude_id = nil) if exclude_id.present? procedure = where.not(id: exclude_id).find_by(path: path) else @@ -353,7 +353,7 @@ class Procedure < ApplicationRecord if procedure.blank? PATH_AVAILABLE - elsif administrateurs.any? { |administrateur| administrateur.owns?(procedure) } + elsif administrateur.owns?(procedure) if procedure.brouillon? PATH_NOT_AVAILABLE_BROUILLON else @@ -402,17 +402,21 @@ class Procedure < ApplicationRecord update!(path: path) end - def can_publish?(path) - path_availability(path).in?(PATH_CAN_PUBLISH) + def can_publish?(administrateur, path) + path_availability(administrateur, path).in?(PATH_CAN_PUBLISH) 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) claim_path_ownership!(path) end - def after_reopen(path) + def after_reopen(administrateur, path) update!(published_at: Time.zone.now, archived_at: nil) claim_path_ownership!(path) diff --git a/spec/factories/procedure.rb b/spec/factories/procedure.rb index 867c7699b..e45f1c2aa 100644 --- a/spec/factories/procedure.rb +++ b/spec/factories/procedure.rb @@ -37,7 +37,7 @@ FactoryBot.define do after(:build) do |procedure, _evaluator| procedure.for_individual = 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 @@ -135,13 +135,13 @@ FactoryBot.define do trait :published do after(:build) do |procedure, _evaluator| - procedure.publish!(generate(:published_path)) + procedure.publish!(procedure.administrateurs.first, generate(:published_path)) end end trait :archived do after(:build) do |procedure, _evaluator| - procedure.publish!(generate(:published_path)) + procedure.publish!(procedure.administrateurs.first, generate(:published_path)) procedure.archive! end end @@ -150,14 +150,14 @@ FactoryBot.define do # For now the behavior is the same than :archived # (it may be different in the future though) after(:build) do |procedure, _evaluator| - procedure.publish!(generate(:published_path)) + procedure.publish!(procedure.administrateurs.first, generate(:published_path)) procedure.archive! end end trait :hidden do after(:build) do |procedure, _evaluator| - procedure.publish!(generate(:published_path)) + procedure.publish!(procedure.administrateurs.first, generate(:published_path)) procedure.hide! end end diff --git a/spec/models/procedure_spec.rb b/spec/models/procedure_spec.rb index 3de514f16..d6a9df4dc 100644 --- a/spec/models/procedure_spec.rb +++ b/spec/models/procedure_spec.rb @@ -493,7 +493,7 @@ describe Procedure do before do Timecop.freeze(now) - procedure.publish!("example-path") + procedure.publish!(procedure.administrateurs.first, "example-path") end after { Timecop.return }