Merge pull request #2391 from tchak/reset-procedure

Drop procedure dossiers when updating brouillon
This commit is contained in:
Pierre de La Morinerie 2018-08-14 11:26:36 +02:00 committed by GitHub
commit bd8eec56d2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 69 additions and 13 deletions

View file

@ -1,6 +1,7 @@
class Admin::PiecesJustificativesController < AdminController class Admin::PiecesJustificativesController < AdminController
before_action :retrieve_procedure before_action :retrieve_procedure
before_action :procedure_locked? before_action :procedure_locked?
before_action :reset_procedure, only: [:update, :destroy, :move_up, :move_down]
def show def show
end end
@ -22,12 +23,6 @@ class Admin::PiecesJustificativesController < AdminController
render json: { message: 'Type de piece justificative not found' }, status: 404 render json: { message: 'Type de piece justificative not found' }, status: 404
end end
def update_params
params
.require(:procedure)
.permit(types_de_piece_justificative_attributes: [:libelle, :description, :id, :order_place, :mandatory, :lien_demarche])
end
def move_up def move_up
index = params[:index].to_i - 1 index = params[:index].to_i - 1
if @procedure.switch_types_de_piece_justificative index if @procedure.switch_types_de_piece_justificative index
@ -44,4 +39,12 @@ class Admin::PiecesJustificativesController < AdminController
render json: {}, status: 400 render json: {}, status: 400
end end
end end
private
def update_params
params
.require(:procedure)
.permit(types_de_piece_justificative_attributes: [:libelle, :description, :id, :order_place, :mandatory, :lien_demarche])
end
end end

View file

@ -83,11 +83,12 @@ class Admin::ProceduresController < AdminController
@procedure = current_administrateur.procedures.find(params[:id]) @procedure = current_administrateur.procedures.find(params[:id])
if !@procedure.update(procedure_params) if !@procedure.update(procedure_params)
flash.now.alert = @procedure.errors.full_messages flash.alert = @procedure.errors.full_messages
return render 'edit' else
reset_procedure
flash.notice = 'Procédure modifiée'
end end
flash.notice = 'Procédure modifiée'
redirect_to edit_admin_procedure_path(id: @procedure.id) redirect_to edit_admin_procedure_path(id: @procedure.id)
end end

View file

@ -1,6 +1,7 @@
class Admin::TypesDeChampController < AdminController class Admin::TypesDeChampController < AdminController
before_action :retrieve_procedure before_action :retrieve_procedure
before_action :procedure_locked? before_action :procedure_locked?
before_action :reset_procedure, only: [:update, :destroy, :move_up, :move_down]
def destroy def destroy
@procedure.types_de_champ.destroy(params[:id]) @procedure.types_de_champ.destroy(params[:id])

View file

@ -1,6 +1,7 @@
class Admin::TypesDeChampPrivateController < AdminController class Admin::TypesDeChampPrivateController < AdminController
before_action :retrieve_procedure before_action :retrieve_procedure
before_action :procedure_locked? before_action :procedure_locked?
before_action :reset_procedure, only: [:update, :destroy, :move_up, :move_down]
def destroy def destroy
@procedure.types_de_champ_private.destroy(params[:id]) @procedure.types_de_champ_private.destroy(params[:id])

View file

@ -21,4 +21,10 @@ class AdminController < ApplicationController
redirect_to admin_procedure_path(id: @procedure.id) redirect_to admin_procedure_path(id: @procedure.id)
end end
end end
def reset_procedure
if @procedure.brouillon_avec_lien?
@procedure.reset!
end
end
end end

View file

@ -109,10 +109,23 @@ class Procedure < ApplicationRecord
dossiers.update_all(hidden_at: now) dossiers.update_all(hidden_at: now)
end end
def reset!
if locked?
raise "Can not reset a locked procedure."
else
dossiers.delete_all
end
end
def locked? def locked?
publiee_ou_archivee? publiee_ou_archivee?
end end
# This method is needed for transition. Eventually this will be the same as brouillon?.
def brouillon_avec_lien?
Flipflop.publish_draft? && brouillon? && procedure_path.present?
end
def publiee_ou_archivee? def publiee_ou_archivee?
publiee? || archivee? publiee? || archivee?
end end

View file

@ -16,6 +16,7 @@ Flipflop.configure do
end end
feature :web_hook feature :web_hook
feature :publish_draft
feature :new_dossier_details, feature :new_dossier_details,
title: "Nouvelle page « Dossier »" title: "Nouvelle page « Dossier »"

View file

@ -258,7 +258,7 @@ describe Admin::ProceduresController, type: :controller do
end end
context 'when administrateur is connected' do context 'when administrateur is connected' do
before do def update_procedure
put :update, params: { id: procedure.id, procedure: procedure_params } put :update, params: { id: procedure.id, procedure: procedure_params }
procedure.reload procedure.reload
end end
@ -274,6 +274,8 @@ describe Admin::ProceduresController, type: :controller do
let(:duree_conservation_dossiers_dans_ds) { 7 } let(:duree_conservation_dossiers_dans_ds) { 7 }
let(:duree_conservation_dossiers_hors_ds) { 5 } let(:duree_conservation_dossiers_hors_ds) { 5 }
before { update_procedure }
describe 'procedure attributs in database' do describe 'procedure attributs in database' do
subject { procedure } subject { procedure }
@ -299,6 +301,7 @@ describe Admin::ProceduresController, type: :controller do
end end
context 'when many attributs are not valid' do context 'when many attributs are not valid' do
before { update_procedure }
let(:libelle) { '' } let(:libelle) { '' }
let(:description) { '' } let(:description) { '' }
@ -315,12 +318,30 @@ describe Admin::ProceduresController, type: :controller do
end end
end end
context 'when procedure is brouillon' do
let(:procedure) { create(:procedure_with_dossiers, :with_path, :with_type_de_champ, :with_two_type_de_piece_justificative, administrateur: admin) }
let!(:dossiers_count) { procedure.dossiers.count }
describe 'dossiers are dropped' do
before do
Flipflop::FeatureSet.current.test!.switch!(:publish_draft, true)
end
subject { update_procedure }
it {
expect(dossiers_count).to eq(1)
expect(subject.dossiers.count).to eq(0)
}
end
end
context 'when procedure is published' do context 'when procedure is published' do
let!(:procedure) { create(:procedure, :with_type_de_champ, :with_two_type_de_piece_justificative, :published, administrateur: admin) } let(:procedure) { create(:procedure, :with_type_de_champ, :with_two_type_de_piece_justificative, :published, administrateur: admin) }
subject { update_procedure }
describe 'only some properties can be updated' do describe 'only some properties can be updated' do
subject { procedure }
it { expect(subject.libelle).to eq procedure_params[:libelle] } it { expect(subject.libelle).to eq procedure_params[:libelle] }
it { expect(subject.description).to eq procedure_params[:description] } it { expect(subject.description).to eq procedure_params[:description] }
it { expect(subject.organisation).to eq procedure_params[:organisation] } it { expect(subject.organisation).to eq procedure_params[:organisation] }

View file

@ -29,6 +29,15 @@ FactoryBot.define do
end end
end end
trait :with_path do
after(:create) do |procedure|
create(:procedure_path,
procedure: procedure,
administrateur: procedure.administrateur,
path: generate(:published_path))
end
end
trait :with_service do trait :with_service do
after(:build) do |procedure, _evaluator| after(:build) do |procedure, _evaluator|
procedure.service = create(:service) procedure.service = create(:service)