fix(instructeur): don't crash when adding a filter too long
This commit is contained in:
parent
5a8859cd28
commit
5ab1d45b8a
3 changed files with 29 additions and 7 deletions
|
@ -148,7 +148,9 @@ module Instructeurs
|
||||||
end
|
end
|
||||||
|
|
||||||
def add_filter
|
def add_filter
|
||||||
procedure_presentation.add_filter(statut, params[:field], params[:value])
|
if !procedure_presentation.add_filter(statut, params[:field], params[:value])
|
||||||
|
flash.alert = procedure_presentation.errors.full_messages
|
||||||
|
end
|
||||||
|
|
||||||
redirect_back(fallback_location: instructeur_procedure_url(procedure))
|
redirect_back(fallback_location: instructeur_procedure_url(procedure))
|
||||||
end
|
end
|
||||||
|
|
|
@ -319,7 +319,7 @@ class ProcedurePresentation < ApplicationRecord
|
||||||
'value' => value
|
'value' => value
|
||||||
}
|
}
|
||||||
|
|
||||||
update!(filters: updated_filters)
|
update(filters: updated_filters)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -457,11 +457,11 @@ class ProcedurePresentation < ApplicationRecord
|
||||||
end
|
end
|
||||||
|
|
||||||
def check_filters_max_length
|
def check_filters_max_length
|
||||||
individual_filters = filters.values.flatten.filter { |f| f.is_a?(Hash) }
|
filters.values.flatten.each do |filter|
|
||||||
individual_filters.each do |filter|
|
next if !filter.is_a?(Hash)
|
||||||
if filter['value']&.length.to_i > FILTERS_VALUE_MAX_LENGTH
|
next if filter['value']&.length.to_i <= FILTERS_VALUE_MAX_LENGTH
|
||||||
errors.add(:filters, :too_long)
|
|
||||||
end
|
errors.add(:base, "Le filtre #{filter['label']} est trop long (maximum: #{FILTERS_VALUE_MAX_LENGTH} caractères)")
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -830,4 +830,24 @@ describe Instructeurs::ProceduresController, type: :controller do
|
||||||
it { is_expected.to have_http_status(:forbidden) }
|
it { is_expected.to have_http_status(:forbidden) }
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
describe '#add_filter' do
|
||||||
|
let(:instructeur) { create(:instructeur) }
|
||||||
|
let(:procedure) { create(:procedure, :for_individual) }
|
||||||
|
|
||||||
|
before do
|
||||||
|
create(:assign_to, instructeur:, groupe_instructeur: build(:groupe_instructeur, procedure:))
|
||||||
|
|
||||||
|
sign_in(instructeur.user)
|
||||||
|
end
|
||||||
|
|
||||||
|
subject do
|
||||||
|
post :add_filter, params: { procedure_id: procedure.id, field: "individual/nom", value: "n" * 110, statut: "a-suivre" }
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'should render the error' do
|
||||||
|
subject
|
||||||
|
expect(flash.alert[0]).to include("Le filtre Nom est trop long (maximum: 100 caractères)")
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue