fix: do not allow empty filter in models
This commit is contained in:
parent
c56aa67297
commit
b22f049318
6 changed files with 46 additions and 13 deletions
|
@ -13,6 +13,9 @@ class FilteredColumn
|
|||
|
||||
validate :check_filter_max_length
|
||||
validate :check_filter_max_integer
|
||||
validates :filter, presence: {
|
||||
message: -> (object, _data) { "Le filtre « #{object.label} » ne peut pas être vide" }
|
||||
}
|
||||
|
||||
def initialize(column:, filter:)
|
||||
@column = column
|
||||
|
@ -33,14 +36,14 @@ class FilteredColumn
|
|||
if @filter.present? && @filter.length.to_i > FILTERS_VALUE_MAX_LENGTH
|
||||
errors.add(
|
||||
:base,
|
||||
"Le filtre #{label} est trop long (maximum: #{FILTERS_VALUE_MAX_LENGTH} caractères)"
|
||||
"Le filtre « #{label} » est trop long (maximum: #{FILTERS_VALUE_MAX_LENGTH} caractères)"
|
||||
)
|
||||
end
|
||||
end
|
||||
|
||||
def check_filter_max_integer
|
||||
if @column.column == 'id' && @filter.to_i > PG_INTEGER_MAX_VALUE
|
||||
errors.add(:base, "Le filtre #{label} n'est pas un numéro de dossier possible")
|
||||
errors.add(:base, "Le filtre « #{label} » n'est pas un numéro de dossier possible")
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -103,6 +103,7 @@ ignore_unused:
|
|||
- 'activerecord.attributes.*'
|
||||
- 'activemodel.attributes.map_filter.*'
|
||||
- 'activemodel.attributes.helpscout/form.*'
|
||||
- 'activemodel.errors.models.*'
|
||||
- 'activerecord.errors.*'
|
||||
- 'errors.messages.blank'
|
||||
- 'errors.messages.content_type_invalid'
|
||||
|
|
7
config/locales/models/filtered_column/en.yml
Normal file
7
config/locales/models/filtered_column/en.yml
Normal file
|
@ -0,0 +1,7 @@
|
|||
en:
|
||||
activemodel:
|
||||
errors:
|
||||
models:
|
||||
filtered_column:
|
||||
format: "%{message}"
|
||||
|
7
config/locales/models/filtered_column/fr.yml
Normal file
7
config/locales/models/filtered_column/fr.yml
Normal file
|
@ -0,0 +1,7 @@
|
|||
fr:
|
||||
activemodel:
|
||||
errors:
|
||||
models:
|
||||
filtered_column:
|
||||
format: "%{message}"
|
||||
|
|
@ -911,7 +911,7 @@ describe Instructeurs::ProceduresController, type: :controller do
|
|||
|
||||
it 'should render the error' do
|
||||
subject
|
||||
expect(flash.alert[0]).to include("Le filtre Nom est trop long (maximum: 4048 caractères)")
|
||||
expect(flash.alert[0]).to include("Le filtre « Nom » est trop long (maximum: 4048 caractères)")
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -11,7 +11,7 @@ describe FilteredColumn do
|
|||
let(:filter) { 'a' * (FilteredColumn::FILTERS_VALUE_MAX_LENGTH + 1) }
|
||||
|
||||
it 'adds an error' do
|
||||
expect(filtered_column.errors.map(&:message)).to include(/Le filtre label est trop long/)
|
||||
expect(filtered_column.errors.map(&:message)).to include(/Le filtre « label » est trop long/)
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -22,14 +22,6 @@ describe FilteredColumn do
|
|||
expect(filtered_column.errors).to be_empty
|
||||
end
|
||||
end
|
||||
|
||||
context 'when the filter is empty' do
|
||||
let(:filter) { nil }
|
||||
|
||||
it 'does not add an error' do
|
||||
expect(filtered_column.errors).to be_empty
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe '#check_filters_max_integer' do
|
||||
|
@ -43,7 +35,7 @@ describe FilteredColumn do
|
|||
let(:filter) { (FilteredColumn::PG_INTEGER_MAX_VALUE + 1).to_s }
|
||||
|
||||
it 'adds an error' do
|
||||
expect(filtered_column.errors.map(&:message)).to include(/Le filtre label n'est pas un numéro de dossier possible/)
|
||||
expect(filtered_column.errors.map(&:message)).to include(/Le filtre « label » n'est pas un numéro de dossier possible/)
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -56,4 +48,27 @@ describe FilteredColumn do
|
|||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe '#check_filter_is_not_blank' do
|
||||
let(:column) { Column.new(procedure_id: 1, table: 'table', column: 'column', label: 'label') }
|
||||
let(:filtered_column) { described_class.new(column:, filter:) }
|
||||
|
||||
before { filtered_column.valid? }
|
||||
|
||||
context 'when the filter is blank' do
|
||||
let(:filter) { '' }
|
||||
|
||||
it 'adds an error' do
|
||||
expect(filtered_column.errors.map(&:message)).to include(/Le filtre « label » ne peut pas être vide/)
|
||||
end
|
||||
end
|
||||
|
||||
context 'when the filter is not blank' do
|
||||
let(:filter) { 'a' }
|
||||
|
||||
it 'does not add an error' do
|
||||
expect(filtered_column.errors).to be_empty
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue