review: prefix typed ids with 'champ_'

This commit is contained in:
sebastiencarceles 2022-12-05 10:09:44 +01:00
parent 11fed2c934
commit bf03e3b35a
3 changed files with 23 additions and 11 deletions

View file

@ -13,7 +13,7 @@ class PrefillParams
def build_prefill_values def build_prefill_values
value_by_stable_id = @params value_by_stable_id = @params
.to_unsafe_hash .to_unsafe_hash
.map { |typed_id, value| [stable_id_from_typed_id(typed_id), value] } .map { |prefixed_typed_id, value| [stable_id_from_typed_id(prefixed_typed_id), value] }
.filter { |stable_id, value| stable_id.present? && value.present? } .filter { |stable_id, value| stable_id.present? && value.present? }
.to_h .to_h
@ -23,8 +23,10 @@ class PrefillParams
.map { |champ, value| PrefillValue.new(champ:, value:) } .map { |champ, value| PrefillValue.new(champ:, value:) }
end end
def stable_id_from_typed_id(typed_id) def stable_id_from_typed_id(prefixed_typed_id)
Champ.id_from_typed_id(typed_id).to_i return nil unless prefixed_typed_id.starts_with?("champ_")
Champ.id_from_typed_id(prefixed_typed_id.gsub("champ_", "")).to_i
rescue rescue
nil nil
end end

View file

@ -1097,8 +1097,8 @@ describe Users::DossiersController, type: :controller do
let(:params) { let(:params) {
{ {
procedure_id: procedure_id, procedure_id: procedure_id,
type_de_champ_1.to_typed_id => value_1, "champ_#{type_de_champ_1.to_typed_id}" => value_1,
type_de_champ_2.to_typed_id => value_2 "champ_#{type_de_champ_2.to_typed_id}" => value_2
} }
} }

View file

@ -16,8 +16,8 @@ RSpec.describe PrefillParams do
let(:params) { let(:params) {
{ {
type_de_champ_1.to_typed_id => value_1, "champ_#{type_de_champ_1.to_typed_id}" => value_1,
type_de_champ_2.to_typed_id => value_2 "champ_#{type_de_champ_2.to_typed_id}" => value_2
} }
} }
@ -29,8 +29,18 @@ RSpec.describe PrefillParams do
end end
end end
context "when the typed id is not prefixed by 'champ_'" do
let!(:type_de_champ) { create(:type_de_champ_text, procedure: procedure) }
let(:params) { { type_de_champ.to_typed_id => "value" } }
it "filters out the champ" do
expect(prefill_params_array).to match([])
end
end
context "when the typed id is unknown" do context "when the typed id is unknown" do
let(:params) { { "-1" => "value" } } let(:params) { { "champ_jane_doe" => "value" } }
it "filters out the unknown params" do it "filters out the unknown params" do
expect(prefill_params_array).to match([]) expect(prefill_params_array).to match([])
@ -40,7 +50,7 @@ RSpec.describe PrefillParams do
context 'when there is no Champ that matches the TypeDeChamp with the given stable id' do context 'when there is no Champ that matches the TypeDeChamp with the given stable id' do
let!(:type_de_champ) { create(:type_de_champ_text) } # goes to another procedure let!(:type_de_champ) { create(:type_de_champ_text) } # goes to another procedure
let(:params) { { type_de_champ.to_typed_id => "value" } } let(:params) { { "champ_#{type_de_champ.to_typed_id}" => "value" } }
it "filters out the param" do it "filters out the param" do
expect(prefill_params_array).to match([]) expect(prefill_params_array).to match([])
@ -52,7 +62,7 @@ RSpec.describe PrefillParams do
let!(:type_de_champ) { create(type_de_champ_name, procedure: procedure) } let!(:type_de_champ) { create(type_de_champ_name, procedure: procedure) }
let(:champ_id) { find_champ_by_stable_id(dossier, type_de_champ.stable_id).id } let(:champ_id) { find_champ_by_stable_id(dossier, type_de_champ.stable_id).id }
let(:params) { { type_de_champ.to_typed_id => value } } let(:params) { { "champ_#{type_de_champ.to_typed_id}" => value } }
it "builds an array of hash(id, value) matching the given params" do it "builds an array of hash(id, value) matching the given params" do
expect(prefill_params_array).to match([{ id: champ_id, value: value }]) expect(prefill_params_array).to match([{ id: champ_id, value: value }])
@ -63,7 +73,7 @@ RSpec.describe PrefillParams do
shared_examples "a champ public value that is unauthorized" do |type_de_champ_name, value| shared_examples "a champ public value that is unauthorized" do |type_de_champ_name, value|
let!(:type_de_champ) { create(type_de_champ_name, procedure: procedure) } let!(:type_de_champ) { create(type_de_champ_name, procedure: procedure) }
let(:params) { { type_de_champ.to_typed_id => value } } let(:params) { { "champ_#{type_de_champ.to_typed_id}" => value } }
context 'when the type de champ is unauthorized (type_de_champ_name)' do context 'when the type de champ is unauthorized (type_de_champ_name)' do
it "filters out the param" do it "filters out the param" do