Skip to content

Commit

Permalink
Fix min,max constraints not validated on integer & decimal champ
Browse files Browse the repository at this point in the history
  • Loading branch information
maatinito committed Dec 22, 2023
1 parent bcee4e0 commit bdaa3e5
Show file tree
Hide file tree
Showing 6 changed files with 107 additions and 16 deletions.
17 changes: 13 additions & 4 deletions app/models/champs/decimal_number_champ.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,20 @@
class Champs::DecimalNumberChamp < Champ
validates :value, numericality: {
allow_nil: true,
allow_blank: true,
message: -> (object, _data) {
"« #{object.libelle} » " + object.errors.generate_message(:value, :not_a_number)
}
allow_blank: true
}
validate :min_max_validation

def min_max_validation
return if value.blank?

if type_de_champ.min.present? && value.to_i < type_de_champ.min.to_i
errors.add(:value, :greater_than_or_equal_to, value: value, count: type_de_champ.min.to_i)
end
if type_de_champ.max.present? && value.to_i > type_de_champ.max.to_i
errors.add(:value, :less_than_or_equal_to, value: value, count: type_de_champ.max.to_i)
end
end

def for_export
processed_value
Expand Down
18 changes: 13 additions & 5 deletions app/models/champs/integer_number_champ.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,20 @@ class Champs::IntegerNumberChamp < Champ
validates :value, numericality: {
only_integer: true,
allow_nil: true,
allow_blank: true,
message: -> (object, _data) {
# i18n-tasks-use t('errors.messages.not_an_integer')
"« #{object.libelle} » " + object.errors.generate_message(:value, :not_an_integer)
}
allow_blank: true
}
validate :min_max_validation

def min_max_validation
return if value.blank?

if type_de_champ.min.present? && value.to_i < type_de_champ.min.to_i
errors.add(:value, :greater_than_or_equal_to, value: value, count: type_de_champ.min.to_i)
end
if type_de_champ.max.present? && value.to_i > type_de_champ.max.to_i
errors.add(:value, :less_than_or_equal_to, value: value, count: type_de_champ.max.to_i)
end
end

def for_export
processed_value
Expand Down
2 changes: 1 addition & 1 deletion config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -711,7 +711,7 @@ en:
# cadastres_empty:
# one: "Aucune parcelle cadastrale sur la zone sélectionnée"
# other: "Aucune parcelle cadastrale sur les zones sélectionnées"
not_an_integer: "must be an integer (without decimal)"
# not_an_integer: "must be an integer (without decimal)"
not_a_date: "must be a correct date"
not_a_datetime: "must be a correct datetime"
blank: "can't be blank"
Expand Down
2 changes: 1 addition & 1 deletion config/locales/fr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -722,7 +722,7 @@ fr:
cadastres_empty:
one: "Aucune parcelle cadastrale sur la zone sélectionnée"
other: "Aucune parcelle cadastrale sur les zones sélectionnées"
not_an_integer: "doit être un nombre entier (sans chiffres après la virgule)"
# not_an_integer: "doit être un nombre entier (sans chiffres après la virgule)"
not_a_date: "doit être une date correctement formatée"
not_a_datetime: "doit être une date et heure correctement formatée"
blank: "doit être rempli"
Expand Down
41 changes: 39 additions & 2 deletions spec/models/champs/decimal_number_champ_spec.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
describe Champs::DecimalNumberChamp do
subject { build(:champ_decimal_number, value: value).tap(&:valid?) }
let(:min) { nil }
let(:max) { nil }

let(:type_de_champ) { create(:type_de_champ, min:, max:) }

subject { build(:champ_decimal_number, value: value, type_de_champ:).tap(&:valid?) }

describe '#valid?' do
context 'when the value is integer number' do
Expand All @@ -18,7 +23,7 @@
let(:value) { 'toto' }

it { is_expected.to_not be_valid }
it { expect(subject.errors[:value]).to eq(["« #{subject.libelle} » n'est pas un nombre"]) }
it { expect(subject.errors[:value]).to eq(["n'est pas un nombre"]) }
end

context 'when the value is blank' do
Expand All @@ -32,5 +37,37 @@

it { is_expected.to be_valid }
end

context "when max is specified" do
let(:max) { 10 }
context 'when the value is equal to max' do
let(:value) { '10' }

it { is_expected.to be_valid }
end

context 'when the value is greater than max' do
let(:value) { 11 }

it { is_expected.to_not be_valid }
it { expect(subject.errors[:value]).to eq(["doit être inférieur ou égal à 10"]) }
end
end

context "when min is specified" do
let(:min) { 10 }
context 'when the value is equal to min' do
let(:value) { '10' }

it { is_expected.to be_valid }
end

context 'when the value is less than min' do
let(:value) { 9 }

it { is_expected.to_not be_valid }
it { expect(subject.errors[:value]).to eq(["doit être supérieur ou égal à 10"]) }
end
end
end
end
43 changes: 40 additions & 3 deletions spec/models/champs/integer_number_champ_spec.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
describe Champs::IntegerNumberChamp do
subject { build(:champ_integer_number, value: value).tap(&:valid?) }
let(:min) { nil }
let(:max) { nil }

let(:type_de_champ) { create(:type_de_champ, min:, max:) }

subject { build(:champ_integer_number, value: value, type_de_champ:).tap(&:valid?) }

describe '#valid?' do
context 'when the value is integer number' do
Expand All @@ -12,14 +17,14 @@
let(:value) { 2.6 }

it { is_expected.to_not be_valid }
it { expect(subject.errors[:value]).to eq(["« #{subject.libelle} » doit être un nombre entier (sans chiffres après la virgule)"]) }
it { expect(subject.errors[:value]).to eq(["doit être un nombre entier"]) }
end

context 'when the value is not a number' do
let(:value) { 'toto' }

it { is_expected.to_not be_valid }
it { expect(subject.errors[:value]).to eq(["« #{subject.libelle} » doit être un nombre entier (sans chiffres après la virgule)"]) }
it { expect(subject.errors[:value]).to eq(["n'est pas un nombre"]) }
end

context 'when the value is blank' do
Expand All @@ -33,5 +38,37 @@

it { is_expected.to be_valid }
end

context "when max is specified" do
let(:max) { 10 }
context 'when the value is equal to max' do
let(:value) { '10' }

it { is_expected.to be_valid }
end

context 'when the value is greater than max' do
let(:value) { 11 }

it { is_expected.to_not be_valid }
it { expect(subject.errors[:value]).to eq(["doit être inférieur ou égal à 10"]) }
end
end

context "when min is specified" do
let(:min) { 10 }
context 'when the value is equal to min' do
let(:value) { '10' }

it { is_expected.to be_valid }
end

context 'when the value is less than min' do
let(:value) { 9 }

it { is_expected.to_not be_valid }
it { expect(subject.errors[:value]).to eq(["doit être supérieur ou égal à 10"]) }
end
end
end
end

0 comments on commit bdaa3e5

Please sign in to comment.