fix(dossier): when a champ type changes on update, ensure we have an instance of the right class

This commit is contained in:
Paul Chavard 2024-12-04 12:51:36 +01:00
parent 2164d72308
commit 99a9bc3829
No known key found for this signature in database
2 changed files with 48 additions and 1 deletions

View file

@ -177,9 +177,11 @@ module DossierChampsConcern
attributes[:value_json] = nil
attributes[:external_id] = nil
attributes[:data] = nil
champ = champ.becomes!(attributes[:type].constantize)
champ.save!
end
reset_champs_cache
reset_champ_cache(champ)
[champ, attributes]
end
@ -202,6 +204,11 @@ module DossierChampsConcern
@project_champs_private = nil
end
def reset_champ_cache(champ)
champs_by_public_id[champ.public_id]&.reload
reset_champs_cache
end
def reload_champs_cache
champs.reload if persisted?
reset_champs_cache

View file

@ -268,6 +268,25 @@ RSpec.describe DossierChampsConcern do
}
end
end
context "champ with type change" do
let(:procedure) { create(:procedure, :published, types_de_champ_public: [{ type: :text, libelle: "Un champ text", stable_id: 99 }]) }
let(:dossier) { create(:dossier, :with_populated_champs, procedure:) }
before do
tdc = dossier.procedure.draft_revision.find_and_ensure_exclusive_use(99)
tdc.update!(type_champ: TypeDeChamp.type_champs.fetch(:checkbox))
dossier.procedure.publish_revision!
perform_enqueued_jobs
dossier.reload
end
it {
expect(subject.persisted?).to be_truthy
expect(subject.is_a?(Champs::CheckboxChamp)).to be_truthy
expect(subject.value).to be_nil
}
end
end
context "private champ" do
@ -323,6 +342,27 @@ RSpec.describe DossierChampsConcern do
expect(champ_994.value).to eq("Greer")
}
end
context "champ with type change" do
let(:procedure) { create(:procedure, :published, types_de_champ_public: [{ type: :text, libelle: "Un champ text", stable_id: 99 }]) }
let(:dossier) { create(:dossier, :with_populated_champs, procedure:) }
let(:attributes) { { "99" => { primary_value: "primary" } } }
before do
tdc = dossier.procedure.draft_revision.find_and_ensure_exclusive_use(99)
tdc.update!(type_champ: TypeDeChamp.type_champs.fetch(:linked_drop_down_list), drop_down_options: ["--primary--", "secondary"])
dossier.procedure.publish_revision!
perform_enqueued_jobs
dossier.reload
end
it {
subject
expect(dossier.champs.any?(&:changed_for_autosave?)).to be_truthy
expect(champ_99.changed?).to be_truthy
expect(champ_99.value).to eq('["primary",""]')
}
end
end
describe "#update_champs_attributes(private)" do