Refactor procedures_controller

This commit is contained in:
Paul Chavard 2018-10-25 17:42:09 +02:00
parent b42095363e
commit f1fd1b38ec
5 changed files with 23 additions and 50 deletions

View file

@ -41,7 +41,7 @@ class Admin::ProceduresController < AdminController
def edit def edit
@path = @procedure.path || @procedure.default_path @path = @procedure.path || @procedure.default_path
@available = @procedure.path_available?(@path) @available = @procedure.path_available?(@path)
@mine = @procedure.path_is_mine?(@path) @mine = @procedure.path_mine?(@path)
end end
def destroy def destroy
@ -71,22 +71,14 @@ class Admin::ProceduresController < AdminController
@procedure.module_api_carto = ModuleAPICarto.new(create_module_api_carto_params) @procedure.module_api_carto = ModuleAPICarto.new(create_module_api_carto_params)
end end
@path = params.require(:procedure).permit(:path)[:path] @path = @procedure.path
@available = !ProcedurePath.exists?(path: @path) @available = !Procedure.exists?(path: @path)
@mine = ProcedurePath.mine?(current_administrateur, @path) @mine = Procedure.path_mine?(current_administrateur, @path)
if !@procedure.validate if !@procedure.save
flash.now.alert = @procedure.errors.full_messages flash.now.alert = @procedure.errors.full_messages
return render 'new' return render 'new'
elsif Flipflop.publish_draft? && !ProcedurePath.valid?(Procedure.last, @path)
# FIXME: The code abow is a horrible hack that we need until we migrated path directly on procedure model
flash.now.alert = 'Lien de la démarche invalide.'
return render 'new'
else else
@procedure.save!
if Flipflop.publish_draft?
@procedure.publish_with_path!(@path)
end
flash.notice = 'Démarche enregistrée.' flash.notice = 'Démarche enregistrée.'
end end
@ -95,20 +87,13 @@ class Admin::ProceduresController < AdminController
def update def update
@procedure = current_administrateur.procedures.find(params[:id]) @procedure = current_administrateur.procedures.find(params[:id])
path = params.require(:procedure).permit(:path)[:path]
if !@procedure.update(procedure_params) if !@procedure.update(procedure_params)
flash.alert = @procedure.errors.full_messages flash.alert = @procedure.errors.full_messages
elsif Flipflop.publish_draft? && @procedure.brouillon? elsif Flipflop.publish_draft? && @procedure.brouillon?
if ProcedurePath.valid?(@procedure, path)
@procedure.publish_with_path!(path)
reset_procedure
flash.notice = 'Démarche modifiée. Tous les dossiers de cette démarche ont été supprimés.'
else
flash.alert = 'Lien de la démarche invalide.'
end
else
reset_procedure reset_procedure
flash.notice = 'Démarche modifiée. Tous les dossiers de cette démarche ont été supprimés.'
else
flash.notice = 'Démarche modifiée.' flash.notice = 'Démarche modifiée.'
end end
@ -116,14 +101,18 @@ class Admin::ProceduresController < AdminController
end end
def publish def publish
path = params[:procedure_path]
procedure = current_administrateur.procedures.find(params[:procedure_id]) procedure = current_administrateur.procedures.find(params[:procedure_id])
if !ProcedurePath.valid?(procedure, params[:procedure_path]) procedure.path = path
if !procedure.validate
flash.alert = 'Lien de la démarche invalide' flash.alert = 'Lien de la démarche invalide'
return redirect_to admin_procedures_path return redirect_to admin_procedures_path
else
procedure.path = nil
end end
if procedure.publish_or_reopen!(params[:procedure_path]) if procedure.publish_or_reopen!(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
@ -216,9 +205,10 @@ class Admin::ProceduresController < AdminController
end end
def path_list def path_list
json_path_list = ProcedurePath json_path_list = Procedure
.find_with_path(params[:request]) .find_with_path(params[:request])
.pluck('procedure_paths.path', :administrateur_id) .order(:id)
.pluck(:path, :administrateur_id)
.map do |path, administrateur_id| .map do |path, administrateur_id|
{ {
label: path, label: path,
@ -236,10 +226,10 @@ 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)
@available = procedure.path_available?(path) @available = procedure.path_available?(path)
@mine = procedure.path_is_mine?(path) @mine = procedure.path_mine?(path)
else else
@available = !ProcedurePath.exists?(path: path) @available = !Procedure.exists?(path: path)
@mine = ProcedurePath.mine?(current_administrateur, path) @mine = Procedure.path_mine?(current_administrateur, path)
end end
end end
@ -276,6 +266,9 @@ class Admin::ProceduresController < AdminController
if @procedure&.locked? if @procedure&.locked?
params.require(:procedure).permit(*editable_params) params.require(:procedure).permit(*editable_params)
else else
if Flipflop.publish_draft?
editable_params << :path
end
params.require(:procedure).permit(*editable_params, :duree_conservation_dossiers_dans_ds, :duree_conservation_dossiers_hors_ds, :for_individual, :individual_with_siret, :ask_birthday, module_api_carto_attributes: [:id, :use_api_carto, :quartiers_prioritaires, :cadastre]).merge(administrateur_id: current_administrateur.id) params.require(:procedure).permit(*editable_params, :duree_conservation_dossiers_dans_ds, :duree_conservation_dossiers_hors_ds, :for_individual, :individual_with_siret, :ask_birthday, module_api_carto_attributes: [:id, :use_api_carto, :quartiers_prioritaires, :cadastre]).merge(administrateur_id: current_administrateur.id)
end end
end end

View file

@ -1,7 +1,7 @@
- if mine - if mine
Ce lien est déjà utilisé par une de vos démarche. Ce lien est déjà utilisé par une de vos démarche.
%br %br
Si vous voulez lutiliser, lancienne démarche sera archivée (plus accessible du public). Si vous voulez lutiliser, lancienne démarche sera archivée lors de la publication de la démarche (plus accessible du public).
- else - else
Ce lien est déjà utilisé par une démarche. Ce lien est déjà utilisé par une démarche.
%br %br

View file

@ -391,22 +391,6 @@ describe Admin::ProceduresController, type: :controller do
end end
end end
context 'procedure path exists and has archived procedure' do
let(:procedure_path) { procedure2.path }
let(:procedure2) { create(:procedure, :archived, administrateur: admin) }
it 'publish the given procedure' do
expect(procedure.publiee?).to be_truthy
expect(procedure.path).to eq(procedure_path)
expect(response.status).to eq 302
expect(flash[:notice]).to have_content 'Démarche publiée'
end
it 'archive previous procedure' do
expect(procedure2.archivee?).to be_truthy
end
end
context 'procedure path exists and is not owned by current administrator' do context 'procedure path exists and is not owned by current administrator' do
let(:procedure_path) { procedure3.path } let(:procedure_path) { procedure3.path }

View file

@ -7,8 +7,6 @@ feature 'As an administrateur I wanna clone a procedure', js: true do
let(:administrateur) { create(:administrateur) } let(:administrateur) { create(:administrateur) }
before do before do
# FIXME: needed to make procedure_path validation work
create(:procedure)
Flipflop::FeatureSet.current.test!.switch!(:publish_draft, true) Flipflop::FeatureSet.current.test!.switch!(:publish_draft, true)
login_as administrateur, scope: :administrateur login_as administrateur, scope: :administrateur
visit root_path visit root_path

View file

@ -7,8 +7,6 @@ feature 'As an administrateur I wanna create a new procedure', js: true do
let(:administrateur) { create(:administrateur) } let(:administrateur) { create(:administrateur) }
before do before do
# FIXME: needed to make procedure_path validation work
create(:procedure)
Flipflop::FeatureSet.current.test!.switch!(:publish_draft, true) Flipflop::FeatureSet.current.test!.switch!(:publish_draft, true)
login_as administrateur, scope: :administrateur login_as administrateur, scope: :administrateur
visit root_path visit root_path