transform value to also populate external id

This commit is contained in:
sebastiencarceles 2023-02-28 08:22:43 +01:00
parent a7b21fcd7c
commit 00fa8dc895
4 changed files with 56 additions and 8 deletions

View file

@ -0,0 +1,8 @@
# frozen_string_literal: true
class TypesDeChamp::PrefillAddressTypeDeChamp < TypesDeChamp::PrefillTypeDeChamp
def to_assignable_attributes(champ, value)
return if value.blank?
{ id: champ.id, value: value, external_id: value }
end
end

View file

@ -25,6 +25,8 @@ class TypesDeChamp::PrefillTypeDeChamp < SimpleDelegator
TypesDeChamp::PrefillDepartementTypeDeChamp.new(type_de_champ, revision)
when TypeDeChamp.type_champs.fetch(:communes)
TypesDeChamp::PrefillCommuneTypeDeChamp.new(type_de_champ, revision)
when TypeDeChamp.type_champs.fetch(:address)
TypesDeChamp::PrefillAddressTypeDeChamp.new(type_de_champ, revision)
when TypeDeChamp.type_champs.fetch(:epci)
TypesDeChamp::PrefillEpciTypeDeChamp.new(type_de_champ, revision)
when TypeDeChamp.type_champs.fetch(:annuaire_education)

View file

@ -0,0 +1,32 @@
# frozen_string_literal: true
RSpec.describe TypesDeChamp::PrefillAddressTypeDeChamp do
let(:procedure) { create(:procedure) }
let(:type_de_champ) { build(:type_de_champ_address, procedure: procedure) }
describe 'ancestors' do
subject { described_class.new(type_de_champ, procedure.active_revision) }
it { is_expected.to be_kind_of(TypesDeChamp::PrefillTypeDeChamp) }
end
describe '#to_assignable_attributes' do
let(:champ) { create(:champ_address, type_de_champ: type_de_champ) }
subject { described_class.build(type_de_champ, procedure.active_revision).to_assignable_attributes(champ, value) }
context 'when the value is nil' do
let(:value) { nil }
it { is_expected.to match(nil) }
end
context 'when the value is empty' do
let(:value) { nil }
it { is_expected.to match(nil) }
end
context 'when the value is present' do
let(:value) { 'hello' }
it { is_expected.to match({ id: champ.id, external_id: 'hello', value: 'hello' }) }
end
end
end

View file

@ -9,49 +9,55 @@ RSpec.describe TypesDeChamp::PrefillTypeDeChamp, type: :model do
describe '.build' do
subject(:built) { described_class.build(type_de_champ, procedure.active_revision) }
context 'when the type de champ is a drop_down_list' do
context 'when type de champ is drop_down_list' do
let(:type_de_champ) { build(:type_de_champ_drop_down_list, procedure: procedure) }
it { expect(built).to be_kind_of(TypesDeChamp::PrefillDropDownListTypeDeChamp) }
end
context 'when the type de champ is a multiple_drop_down_list' do
context 'when type de champ is multiple_drop_down_list' do
let(:type_de_champ) { build(:type_de_champ_multiple_drop_down_list, procedure: procedure) }
it { expect(built).to be_kind_of(TypesDeChamp::PrefillMultipleDropDownListTypeDeChamp) }
end
context 'when the type de champ is a pays' do
context 'when type de champ is pays' do
let(:type_de_champ) { build(:type_de_champ_pays, procedure: procedure) }
it { expect(built).to be_kind_of(TypesDeChamp::PrefillPaysTypeDeChamp) }
end
context 'when the type de champ is a regions' do
context 'when type de champ is regions' do
let(:type_de_champ) { build(:type_de_champ_regions, procedure: procedure) }
it { expect(built).to be_kind_of(TypesDeChamp::PrefillRegionTypeDeChamp) }
end
context 'when the type de champ is a repetition' do
context 'when type de champ is repetition' do
let(:type_de_champ) { build(:type_de_champ_repetition, procedure: procedure) }
it { expect(built).to be_kind_of(TypesDeChamp::PrefillRepetitionTypeDeChamp) }
end
context 'when the type de champ is a departements' do
context 'when type de champ is departements' do
let(:type_de_champ) { build(:type_de_champ_departements, procedure: procedure) }
it { expect(built).to be_kind_of(TypesDeChamp::PrefillDepartementTypeDeChamp) }
end
context 'when the type de champ is a communes' do
context 'when type de champ is communes' do
let(:type_de_champ) { build(:type_de_champ_communes) }
it { expect(built).to be_kind_of(TypesDeChamp::PrefillCommuneTypeDeChamp) }
end
context 'when the type de champ is a epci' do
context 'when type de champ is address' do
let(:type_de_champ) { build(:type_de_champ_address) }
it { expect(built).to be_kind_of(TypesDeChamp::PrefillAddressTypeDeChamp) }
end
context 'when type de champ is epci' do
let(:type_de_champ) { build(:type_de_champ_epci, procedure: procedure) }
it { expect(built).to be_kind_of(TypesDeChamp::PrefillEpciTypeDeChamp) }