diff --git a/app/models/champs/decimal_number_champ.rb b/app/models/champs/decimal_number_champ.rb index 57091da53..dae0ce3aa 100644 --- a/app/models/champs/decimal_number_champ.rb +++ b/app/models/champs/decimal_number_champ.rb @@ -22,6 +22,6 @@ class Champs::DecimalNumberChamp < Champ def format_value return if value.blank? - self.value = value.tr(",", ".") + self.value = value.tr(",", ".").gsub(/[[:space:]]/, "") end end diff --git a/app/models/champs/integer_number_champ.rb b/app/models/champs/integer_number_champ.rb index 39adbb5fc..bd4945a16 100644 --- a/app/models/champs/integer_number_champ.rb +++ b/app/models/champs/integer_number_champ.rb @@ -1,4 +1,6 @@ class Champs::IntegerNumberChamp < Champ + before_validation :format_value + validates :value, numericality: { only_integer: true, allow_nil: true, @@ -8,4 +10,10 @@ class Champs::IntegerNumberChamp < Champ object.errors.generate_message(:value, :not_an_integer) } }, if: :validate_champ_value_or_prefill? + + def format_value + return if value.blank? + + self.value = value.gsub(/[[:space:]]/, "") + end end diff --git a/spec/models/champs/decimal_number_champ_spec.rb b/spec/models/champs/decimal_number_champ_spec.rb index 3a5674f73..57ac73cf8 100644 --- a/spec/models/champs/decimal_number_champ_spec.rb +++ b/spec/models/champs/decimal_number_champ_spec.rb @@ -24,6 +24,12 @@ describe Champs::DecimalNumberChamp do end end + context 'when value contain space' do + let(:champ) { create(:champ_decimal_number, :private, value:) } + let(:value) { ' 2.6666 ' } + it { expect(champ.value).to eq('2.6666') } + end + context 'when the value has too many decimal' do let(:value) { '2.6666' } @@ -65,7 +71,7 @@ describe Champs::DecimalNumberChamp do end context 'with number having spaces' do let(:value) { " 120 " } - it { is_expected.to be_nil } + it { is_expected.to eq(120) } end end end diff --git a/spec/models/champs/integer_number_champ_spec.rb b/spec/models/champs/integer_number_champ_spec.rb index f405b1835..7c2a4ad68 100644 --- a/spec/models/champs/integer_number_champ_spec.rb +++ b/spec/models/champs/integer_number_champ_spec.rb @@ -27,6 +27,16 @@ describe Champs::IntegerNumberChamp do end end + context 'when the value is a number with sapces' do + let(:value) { ' 120 ' } + + it 'is valid and is formated' do + is_expected.to be_truthy + champ.save! + expect(champ.value).to eq('120') + end + end + context 'when the value is blank' do let(:value) { '' }