Use Stable ID for repetitions prefill

This commit is contained in:
Damien Le Thiec 2023-02-15 12:15:34 +01:00
parent 2533ca50d4
commit 5a89a2dbe4
4 changed files with 18 additions and 14 deletions

View file

@ -72,6 +72,8 @@ class Champ < ApplicationRecord
:refresh_after_update?,
to: :type_de_champ
delegate :to_typed_id, to: :type_de_champ, prefix: true
delegate :revision, to: :dossier, prefix: true
scope :updated_since?, -> (date) { where('champs.updated_at > ?', date) }

View file

@ -1,5 +1,5 @@
class TypesDeChamp::PrefillDepartementTypeDeChamp < TypesDeChamp::PrefillTypeDeChamp
def possible_values
def all_possible_values
departements.map { |departement| "#{departement[:code]} (#{departement[:name]})" }
end

View file

@ -20,21 +20,21 @@ class TypesDeChamp::PrefillRepetitionTypeDeChamp < TypesDeChamp::PrefillTypeDeCh
value.map.with_index do |repetition, index|
PrefillRepetitionRow.new(champ, repetition, index).to_assignable_attributes
end.compact
end.reject(&:blank?)
end
private
def subchamps_all_possible_values
"<ul>" + prefillable_subchamps.map do |prefill_type_de_champ|
"<li>#{prefill_type_de_champ.libelle}: #{prefill_type_de_champ.possible_values}</li>"
"<li>#{prefill_type_de_champ.to_typed_id}: #{prefill_type_de_champ.possible_values}</li>"
end.join + "</ul>"
end
def row_values_format
@row_example_value ||=
prefillable_subchamps.map do |prefill_type_de_champ|
[prefill_type_de_champ.libelle, prefill_type_de_champ.example_value.to_s]
[prefill_type_de_champ.to_typed_id, prefill_type_de_champ.example_value.to_s]
end.to_h
end
@ -56,8 +56,8 @@ class TypesDeChamp::PrefillRepetitionTypeDeChamp < TypesDeChamp::PrefillTypeDeCh
row = @champ.rows[@index] || @champ.add_row(@champ.dossier_revision)
JSON.parse(@repetition).map do |key, value|
subchamp = row.find { |champ| champ.libelle == key }
return unless subchamp
subchamp = row.find { |champ| champ.type_de_champ_to_typed_id == key }
next unless subchamp
TypesDeChamp::PrefillTypeDeChamp.build(subchamp.type_de_champ).to_assignable_attributes(subchamp, value)
rescue JSON::ParserError # On ignore les valeurs qu'on n'arrive pas à parser

View file

@ -5,6 +5,8 @@ RSpec.describe TypesDeChamp::PrefillRepetitionTypeDeChamp, type: :model, vcr: {
let(:type_de_champ) { build(:type_de_champ_repetition, :with_types_de_champ, :with_region_types_de_champ, procedure: procedure) }
let(:champ) { create(:champ_repetition, type_de_champ: type_de_champ) }
let(:prefillable_subchamps) { TypesDeChamp::PrefillRepetitionTypeDeChamp.new(type_de_champ).send(:prefillable_subchamps) }
let(:text_repetition) { prefillable_subchamps.first }
let(:integer_repetition) { prefillable_subchamps.second }
let(:region_repetition) { prefillable_subchamps.third }
let(:memory_store) { ActiveSupport::Cache.lookup_store(:memory_store) }
@ -22,7 +24,7 @@ RSpec.describe TypesDeChamp::PrefillRepetitionTypeDeChamp, type: :model, vcr: {
describe '#possible_values' do
subject(:possible_values) { described_class.new(type_de_champ).possible_values }
let(:expected_value) {
"Un tableau de dictionnaires avec les valeurs possibles pour chaque champ de la répétition.</br><ul><li>sub type de champ: Un texte court</li><li>sub type de champ2: Un nombre entier</li><li>region sub_champ: Un <a href=\"https://fr.wikipedia.org/wiki/R%C3%A9gion_fran%C3%A7aise\" target=\"_blank\">code INSEE de région</a><br><a title=\"Toutes les valeurs possibles — Nouvel onglet\" target=\"_blank\" rel=\"noopener noreferrer\" href=\"/procedures/#{procedure.path}/prefill_type_de_champs/#{region_repetition.id}\">Voir toutes les valeurs possibles</a></li></ul>"
"Un tableau de dictionnaires avec les valeurs possibles pour chaque champ de la répétition.</br><ul><li>#{text_repetition.to_typed_id}: Un texte court<br></li><li>#{integer_repetition.to_typed_id}: Un nombre entier<br></li><li>#{region_repetition.to_typed_id}: Un <a href=\"https://fr.wikipedia.org/wiki/R%C3%A9gion_fran%C3%A7aise\" target=\"_blank\">code INSEE de région</a><br><a title=\"Toutes les valeurs possibles — Nouvel onglet\" target=\"_blank\" rel=\"noopener noreferrer\" href=\"/procedures/#{procedure.path}/prefill_type_de_champs/#{region_repetition.id}\">Voir toutes les valeurs possibles</a></li></ul>"
}
it {
@ -32,14 +34,13 @@ RSpec.describe TypesDeChamp::PrefillRepetitionTypeDeChamp, type: :model, vcr: {
describe '#example_value' do
subject(:example_value) { described_class.new(type_de_champ).example_value }
let(:expected_value) { ["{\"sub type de champ\":\"Texte court\", \"sub type de champ2\":\"42\", \"region sub_champ\":\"53\"}", "{\"sub type de champ\":\"Texte court\", \"sub type de champ2\":\"42\", \"region sub_champ\":\"53\"}"] }
let(:expected_value) { ["{\"#{text_repetition.to_typed_id}\":\"Texte court\", \"#{integer_repetition.to_typed_id}\":\"42\", \"#{region_repetition.to_typed_id}\":\"53\"}", "{\"#{text_repetition.to_typed_id}\":\"Texte court\", \"#{integer_repetition.to_typed_id}\":\"42\", \"#{region_repetition.to_typed_id}\":\"53\"}"] }
it { expect(example_value).to eq(expected_value) }
end
describe '#to_assignable_attributes' do
subject(:to_assignable_attributes) { described_class.build(type_de_champ).to_assignable_attributes(champ, value) }
let(:type_de_champ_child) { champ.rows.first.first.type_de_champ }
context 'when the value is nil' do
let(:value) { nil }
@ -63,15 +64,16 @@ RSpec.describe TypesDeChamp::PrefillRepetitionTypeDeChamp, type: :model, vcr: {
end
context 'when the value is an array with some wrong keys' do
let(:value) { ["{\"#{type_de_champ_child.libelle}\":\"value\"}", "{\"blabla\":\"value2\"}"] }
let(:value) { ["{\"#{text_repetition.to_typed_id}\":\"value\", \"blabla\":\"value2\"}", "{\"#{integer_repetition.to_typed_id}\":\"value3\"}", "{\"blabla\":\"false\"}"] }
it { is_expected.to match([[{ id: type_de_champ_child.champ.first.id, value: "value" }]]) }
it { is_expected.to match([[{ id: text_repetition.champ.first.id, value: "value" }], [{ id: integer_repetition.champ.second.id, value: "value3" }]]) }
end
context 'when the value is an array with right keys' do
let(:value) { ["{\"#{type_de_champ_child.libelle}\":\"value\"}", "{\"#{type_de_champ_child.libelle}\":\"value2\"}"] }
it { is_expected.to match([[{ id: type_de_champ_child.champ.first.id, value: "value" }], [{ id: type_de_champ_child.champ.second.id, value: "value2" }]]) }
context 'when the value is an array with right keys' do
let(:value) { ["{\"#{text_repetition.to_typed_id}\":\"value\"}", "{\"#{text_repetition.to_typed_id}\":\"value2\"}"] }
it { is_expected.to match([[{ id: text_repetition.champ.first.id, value: "value" }], [{ id: text_repetition.champ.second.id, value: "value2" }]]) }
end
end
end