extract a procedure_presentation controller

This commit is contained in:
simon lehericey 2024-10-25 10:27:46 +02:00
parent 11d4465bf5
commit d8e63221b5
No known key found for this signature in database
GPG key ID: CDE670D827C7B3C5
3 changed files with 129 additions and 0 deletions

View file

@ -0,0 +1,44 @@
# frozen_string_literal: true
module Instructeurs
class ProcedurePresentationController < InstructeurController
before_action :set_procedure_presentation
def update
if !@procedure_presentation.update(procedure_presentation_params)
# complicated way to display inner error messages
flash.alert = @procedure_presentation.errors
.flat_map { _1.detail[:value].flat_map { |c| c.errors.full_messages } }
end
redirect_back_or_to([:instructeur, procedure])
end
private
def procedure = @procedure_presentation.procedure
def procedure_presentation_params
filters = [
:tous_filters, :a_suivre_filters, :suivis_filters, :traites_filters,
:expirant_filters, :archives_filters, :supprimes_filters
].index_with { [:id, :filter] }
h = params.permit(displayed_columns: [], sorted_column: [:order, :id], **filters).to_h
# React ComboBox/MultiComboBox return [''] when no value is selected
# We need to remove them
if h[:displayed_columns].present?
h[:displayed_columns] = h[:displayed_columns].reject(&:empty?)
end
h
end
def set_procedure_presentation
@procedure_presentation = ProcedurePresentation
.includes(:assign_to)
.find_by!(id: params[:id], assign_to: { instructeur: current_instructeur })
end
end
end

View file

@ -461,6 +461,8 @@ Rails.application.routes.draw do
end
end
resources :procedure_presentation, only: [:update]
resources :procedures, only: [:index, :show], param: :procedure_id do
member do
resources :archives, only: [:index, :create]

View file

@ -0,0 +1,83 @@
# frozen_string_literal: true
describe Instructeurs::ProcedurePresentationController, type: :controller do
describe '#update' do
subject { patch :update, params: }
let(:procedure) { create(:procedure) }
let(:instructeur) { create(:instructeur) }
let(:procedure_presentation) do
groupe_instructeur = procedure.defaut_groupe_instructeur
assign_to = create(:assign_to, instructeur:, groupe_instructeur:)
assign_to.procedure_presentation_or_default_and_errors.first
end
let(:state_column) { procedure.dossier_state_column }
let(:params) { { id: procedure_presentation.id }.merge(presentation_params) }
context 'nominal case' do
before { sign_in(instructeur.user) }
let(:presentation_params) do
{
displayed_columns: [state_column.id],
sorted_column: { order: 'asc', id: state_column.id },
tous_filters: [{ id: state_column.id, filter: 'en_construction' }]
}
end
it 'updates the procedure_presentation' do
expect(procedure_presentation.displayed_columns).to eq(procedure.default_displayed_columns)
expect(procedure_presentation.sorted_column).to eq(procedure.default_sorted_column)
expect(procedure_presentation.tous_filters).to eq([])
subject
expect(response).to redirect_to(instructeur_procedure_url(procedure))
procedure_presentation.reload
expect(procedure_presentation.displayed_columns).to eq([state_column])
expect(procedure_presentation.sorted_column.column).to eq(state_column)
expect(procedure_presentation.sorted_column.order).to eq('asc')
filtered_column = FilteredColumn.new(column: state_column, filter: 'en_construction')
expect(procedure_presentation.tous_filters).to eq([filtered_column])
end
end
context 'with a wrong instructeur' do
let(:another_instructeur) { create(:instructeur) }
before { sign_in(another_instructeur.user) }
let(:presentation_params) { { displayed_columns: [state_column.id] } }
it 'does not update the procedure_presentation' do
expect { subject }.to raise_error(ActiveRecord::RecordNotFound)
end
end
context 'with an empty string in displayed_columns' do
before { sign_in(instructeur.user) }
let(:presentation_params) { { displayed_columns: [''] } }
it 'removes the empty string' do
subject
expect(procedure_presentation.reload.displayed_columns).to eq([])
end
end
context 'with an error in filters' do
before { sign_in(instructeur.user) }
let(:presentation_params) { { tous_filters: [{ id: state_column.id, filter: '' }] } }
it 'does not update the procedure_presentation' do
subject
expect(flash.alert).to include(/ne peut pas être vide/)
end
end
end
end