diff --git a/app/models/champ.rb b/app/models/champ.rb index 697678f18..552a62210 100644 --- a/app/models/champ.rb +++ b/app/models/champ.rb @@ -49,7 +49,7 @@ class Champ < ApplicationRecord def for_export if value.present? - value_for_export + value else nil end @@ -58,10 +58,4 @@ class Champ < ApplicationRecord def main_value_name :value end - - private - - def value_for_export - value - end end diff --git a/app/models/champs/decimal_number_champ.rb b/app/models/champs/decimal_number_champ.rb index b22fb2379..465d4ec5c 100644 --- a/app/models/champs/decimal_number_champ.rb +++ b/app/models/champs/decimal_number_champ.rb @@ -1,7 +1,7 @@ class Champs::DecimalNumberChamp < Champ validates :value, numericality: { allow_nil: true, allow_blank: true } - def value_for_export - value.to_f + def for_export + value.present? ? value.to_f : nil end end diff --git a/app/models/champs/integer_number_champ.rb b/app/models/champs/integer_number_champ.rb index d7ca399a7..a483ab80c 100644 --- a/app/models/champs/integer_number_champ.rb +++ b/app/models/champs/integer_number_champ.rb @@ -1,7 +1,7 @@ class Champs::IntegerNumberChamp < Champ validates :value, numericality: { only_integer: true, allow_nil: true, allow_blank: true } - def value_for_export - value.to_i + def for_export + value.present? ? value.to_i : nil end end diff --git a/app/models/champs/linked_drop_down_list_champ.rb b/app/models/champs/linked_drop_down_list_champ.rb index e9871eab6..d0fade636 100644 --- a/app/models/champs/linked_drop_down_list_champ.rb +++ b/app/models/champs/linked_drop_down_list_champ.rb @@ -33,6 +33,10 @@ class Champs::LinkedDropDownListChamp < Champ value.present? ? [primary_value, secondary_value].compact.join(' / ') : "" end + def for_export + value.present? ? "#{primary_value || ''};#{secondary_value || ''}" : nil + end + def mandatory_and_blank? mandatory? && (primary_value.blank? || secondary_value.blank?) end @@ -43,10 +47,6 @@ class Champs::LinkedDropDownListChamp < Champ private - def value_for_export - "#{primary_value || ''};#{secondary_value || ''}" - end - def pack_value(primary, secondary) self.value = JSON.generate([primary, secondary]) end diff --git a/app/models/champs/multiple_drop_down_list_champ.rb b/app/models/champs/multiple_drop_down_list_champ.rb index 520367620..dc13aa8d7 100644 --- a/app/models/champs/multiple_drop_down_list_champ.rb +++ b/app/models/champs/multiple_drop_down_list_champ.rb @@ -13,6 +13,10 @@ class Champs::MultipleDropDownListChamp < Champ value.present? ? selected_options.join(', ') : '' end + def for_export + value.present? ? selected_options.join(', ') : nil + end + private def format_before_save @@ -26,8 +30,4 @@ class Champs::MultipleDropDownListChamp < Champ end end end - - def value_for_export - selected_options.join(', ') - end end diff --git a/app/models/champs/textarea_champ.rb b/app/models/champs/textarea_champ.rb index 58e8d2db2..726705ad8 100644 --- a/app/models/champs/textarea_champ.rb +++ b/app/models/champs/textarea_champ.rb @@ -1,7 +1,5 @@ class Champs::TextareaChamp < Champs::TextChamp - private - - def value_for_export - ActionView::Base.full_sanitizer.sanitize(value) + def for_export + value.present? ? ActionView::Base.full_sanitizer.sanitize(value) : nil end end diff --git a/app/models/champs/yes_no_champ.rb b/app/models/champs/yes_no_champ.rb index 7c29930fb..85723b846 100644 --- a/app/models/champs/yes_no_champ.rb +++ b/app/models/champs/yes_no_champ.rb @@ -6,12 +6,16 @@ class Champs::YesNoChamp < Champs::CheckboxChamp end def to_s - value_for_export + processed_value + end + + def for_export + processed_value end private - def value_for_export + def processed_value value == 'true' ? 'oui' : 'non' end end diff --git a/spec/models/champ_spec.rb b/spec/models/champ_spec.rb index e2c80d524..19cc5b2b6 100644 --- a/spec/models/champ_spec.rb +++ b/spec/models/champ_spec.rb @@ -104,19 +104,19 @@ describe Champ do context 'if yes' do let(:value) { 'true' } - it { expect(champ.for_export).to eq('oui') } + it { expect(champ.for_export).to eq('Oui') } end context 'if no' do let(:value) { 'false' } - it { expect(champ.for_export).to eq('non') } + it { expect(champ.for_export).to eq('Non') } end context 'if nil' do let(:value) { nil } - it { expect(champ.for_export).to eq(nil) } + it { expect(champ.for_export).to eq('non') } end end