Skip to content

Commit

Permalink
fix(dossier): when a champ type changes on update, ensure we have an …
Browse files Browse the repository at this point in the history
…instance of the right class
  • Loading branch information
tchak committed Dec 4, 2024
1 parent 2164d72 commit 99a9bc3
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 1 deletion.
9 changes: 8 additions & 1 deletion app/models/concerns/dossier_champs_concern.rb
Original file line number Diff line number Diff line change
Expand Up @@ -177,9 +177,11 @@ def champ_with_attributes_for_update(type_de_champ, row_id, updated_by:)
attributes[:value_json] = nil
attributes[:external_id] = nil
attributes[:data] = nil
champ = champ.becomes!(attributes[:type].constantize)
champ.save!

Check warning on line 181 in app/models/concerns/dossier_champs_concern.rb

View check run for this annotation

Codecov / codecov/patch

app/models/concerns/dossier_champs_concern.rb#L180-L181

Added lines #L180 - L181 were not covered by tests
end

reset_champs_cache
reset_champ_cache(champ)

[champ, attributes]
end
Expand All @@ -202,6 +204,11 @@ def reset_champs_cache
@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
Expand Down
40 changes: 40 additions & 0 deletions spec/models/concerns/dossier_champs_concern_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,25 @@
}
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
Expand Down Expand Up @@ -323,6 +342,27 @@
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
Expand Down

0 comments on commit 99a9bc3

Please sign in to comment.