Refacto to_assignable_attributes for prefill_repetition

This commit is contained in:
Damien Le Thiec 2023-02-12 13:02:30 +01:00
parent fc94aaaa21
commit ea126612a1
2 changed files with 53 additions and 6 deletions

View file

@ -19,12 +19,7 @@ class TypesDeChamp::PrefillRepetitionTypeDeChamp < TypesDeChamp::PrefillTypeDeCh
return [] unless value.is_a?(Array)
value.map.with_index do |repetition, index|
row = champ.rows[index] || champ.add_row(champ.dossier_revision)
JSON.parse(repetition).map do |key, value|
id = row.find { |champ| champ.libelle == key }&.id
next unless id
{ id: id, value: value }
end.compact
PrefillRepetitionRow.new(champ, repetition, index).to_assignable_attributes
rescue JSON::ParserError
end.compact
end
@ -54,4 +49,23 @@ class TypesDeChamp::PrefillRepetitionTypeDeChamp < TypesDeChamp::PrefillTypeDeCh
@prefillable_subchamps ||=
TypesDeChamp::PrefillTypeDeChamp.wrap(active_revision_type_de_champ.revision_types_de_champ.map(&:type_de_champ).filter(&:prefillable?))
end
class PrefillRepetitionRow
def initialize(champ, repetition, index)
@champ = champ
@repetition = repetition
@index = index
end
def to_assignable_attributes
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
TypesDeChamp::PrefillTypeDeChamp.build(subchamp.type_de_champ).to_assignable_attributes(subchamp, value)
end.compact
end
end
end

View file

@ -3,6 +3,7 @@
RSpec.describe TypesDeChamp::PrefillRepetitionTypeDeChamp, type: :model, vcr: { cassette_name: 'api_geo_regions' } do
let(:procedure) { build(:procedure) }
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(:region_repetition) { prefillable_subchamps.third }
let(:memory_store) { ActiveSupport::Cache.lookup_store(:memory_store) }
@ -35,4 +36,36 @@ RSpec.describe TypesDeChamp::PrefillRepetitionTypeDeChamp, type: :model, vcr: {
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 }
it { is_expected.to match([]) }
end
context 'when the value is empty' do
let(:value) { '' }
it { is_expected.to match([]) }
end
context 'when the value is a string' do
let(:value) { 'hello' }
it { is_expected.to match([]) }
end
context 'when the value is an array with wrong keys' do
let(:value) { ["{\"blabla\":\"value\"}", "{\"blabla\":\"value2\"}"] }
it { is_expected.to match([]) }
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" }]]) }
end
end
end