fix(dossier.export): should not use default value when types_de_champ changed to a similar one

This commit is contained in:
mfo 2024-09-06 16:57:39 +02:00
parent ad33934059
commit 7ec17ff7f5
No known key found for this signature in database
GPG key ID: 7CE3E1F5B794A8EC
3 changed files with 27 additions and 9 deletions

View file

@ -744,11 +744,27 @@ class TypeDeChamp < ApplicationRecord
# no champ
return true if champ.nil?
# type de champ on the revision changed
return true if type_champ_to_champ_class_name(type_champ) != champ.type
if type_champ != champ.type_champ
return !castable_on_change?(type_champ, champ.type_champ)
end
# special case for linked drop down champ it's blank implementation is not what you think
return champ.value.blank? if type_champ == TypeDeChamp.type_champs.fetch(:linked_drop_down_list)
champ.blank?
end
def castable_on_change?(from_type, to_type)
case [from_type, to_type]
when ['integer_number', 'decimal_number'], # recast numbers automatically
['decimal_number', 'integer_number'], # may lose some data, but who cares ?
['text', 'textarea'], # allow short text to long text
['drop_down_list', 'multiple_drop_down_list'], # single list can become multi
['date', 'datetime'], # date <=> datetime
['datetime', 'date'] # may lose some data, but who cares ?
true
else
false
end
end
end
private

View file

@ -241,7 +241,10 @@ describe Champ do
let(:value) { :noop }
let(:champ_yes_no) { Champs::YesNoChamp.new(value: 'true') }
let(:champ_text) { Champs::TextChamp.new(value: 'hello') }
before do
allow(champ_yes_no).to receive(:type_de_champ).and_return(build(:type_de_champ_yes_no))
allow(champ_text).to receive(:type_de_champ).and_return(build(:type_de_champ_text))
end
it { expect(TypeDeChamp.champ_value_for_export('text', champ_yes_no)).to eq(nil) }
it { expect(TypeDeChamp.champ_value_for_export('yes_no', champ_text)).to eq('Non') }
end

View file

@ -1996,18 +1996,17 @@ describe Dossier, type: :model do
describe "champs_for_export" do
context 'with integer_number' do
let(:procedure) { create(:procedure, types_de_champ_public: [{ type: :integer_number, libelle: 'c1' }]) }
let(:procedure) { create(:procedure, :published, types_de_champ_public: [{ type: :integer_number, libelle: 'c1' }]) }
let(:dossier) { create(:dossier, :with_populated_champs, procedure:) }
let(:integer_number_type_de_champ) { procedure.active_revision.types_de_champ_public.find { |type_de_champ| type_de_champ.type_champ == TypeDeChamp.type_champs.fetch(:integer_number) } }
it 'give me back my decimal number' do
procedure.publish!
dossier
integer_number_type_de_champ.update(type_champ: :decimal_number)
procedure.update(published_revision: procedure.draft_revision, draft_revision: procedure.create_new_revision)
dossier.reload
expect(dossier.champs_for_export(procedure.types_de_champ_for_procedure_presentation.not_repetition.to_a)).to eq([['c1', 42]])
expect {
integer_number_type_de_champ.update(type_champ: :decimal_number)
procedure.update(published_revision: procedure.draft_revision, draft_revision: procedure.create_new_revision)
}.to change { dossier.reload.champs_for_export(procedure.types_de_champ_for_procedure_presentation.not_repetition.to_a) }
.from([["c1", 42]]).to([["c1", 42.0]])
end
end
context 'with a unconditionnal procedure' do