[#1421] Use delegation rather than STI for TypeDeChamp

This commit is contained in:
Frederic Merizen 2018-09-11 18:57:51 +02:00
parent 4bb00e9875
commit 9beaa293cd
26 changed files with 82 additions and 65 deletions

View file

@ -1,5 +1,5 @@
FactoryBot.define do
factory :type_de_champ, class: 'TypesDeChamp::TextTypeDeChamp' do
factory :type_de_champ do
private { false }
# Previous line is kept blank so that rubocop does not complain
@ -9,79 +9,79 @@ FactoryBot.define do
order_place { 1 }
mandatory { false }
factory :type_de_champ_text, class: 'TypesDeChamp::TextTypeDeChamp' do
factory :type_de_champ_text do
type_champ { TypeDeChamp.type_champs.fetch(:text) }
end
factory :type_de_champ_textarea, class: 'TypesDeChamp::TextareaTypeDeChamp' do
factory :type_de_champ_textarea do
type_champ { TypeDeChamp.type_champs.fetch(:textarea) }
end
factory :type_de_champ_number, class: 'TypesDeChamp::NumberTypeDeChamp' do
factory :type_de_champ_number do
type_champ { TypeDeChamp.type_champs.fetch(:number) }
end
factory :type_de_champ_checkbox, class: 'TypesDeChamp::CheckboxTypeDeChamp' do
factory :type_de_champ_checkbox do
type_champ { TypeDeChamp.type_champs.fetch(:checkbox) }
end
factory :type_de_champ_civilite, class: 'TypesDeChamp::CiviliteTypeDeChamp' do
factory :type_de_champ_civilite do
type_champ { TypeDeChamp.type_champs.fetch(:civilite) }
end
factory :type_de_champ_email, class: 'TypesDeChamp::EmailTypeDeChamp' do
factory :type_de_champ_email do
type_champ { TypeDeChamp.type_champs.fetch(:email) }
end
factory :type_de_champ_phone, class: 'TypesDeChamp::PhoneTypeDeChamp' do
factory :type_de_champ_phone do
type_champ { TypeDeChamp.type_champs.fetch(:phone) }
end
factory :type_de_champ_address, class: 'TypesDeChamp::AddressTypeDeChamp' do
factory :type_de_champ_address do
type_champ { TypeDeChamp.type_champs.fetch(:address) }
end
factory :type_de_champ_yes_no, class: 'TypesDeChamp::YesNoTypeDeChamp' do
factory :type_de_champ_yes_no do
libelle { 'Yes/no' }
type_champ { TypeDeChamp.type_champs.fetch(:yes_no) }
end
factory :type_de_champ_date, class: 'TypesDeChamp::DateTypeDeChamp' do
factory :type_de_champ_date do
type_champ { TypeDeChamp.type_champs.fetch(:date) }
end
factory :type_de_champ_datetime, class: 'TypesDeChamp::DatetimeTypeDeChamp' do
factory :type_de_champ_datetime do
type_champ { TypeDeChamp.type_champs.fetch(:datetime) }
end
factory :type_de_champ_drop_down_list, class: 'TypesDeChamp::DropDownListTypeDeChamp' do
factory :type_de_champ_drop_down_list do
libelle { 'Menu déroulant' }
type_champ { TypeDeChamp.type_champs.fetch(:drop_down_list) }
drop_down_list { create(:drop_down_list) }
end
factory :type_de_champ_multiple_drop_down_list, class: 'TypesDeChamp::MultipleDropDownListTypeDeChamp' do
factory :type_de_champ_multiple_drop_down_list do
type_champ { TypeDeChamp.type_champs.fetch(:multiple_drop_down_list) }
drop_down_list { create(:drop_down_list) }
end
factory :type_de_champ_linked_drop_down_list, class: 'TypesDeChamp::LinkedDropDownListTypeDeChamp' do
factory :type_de_champ_linked_drop_down_list do
type_champ { TypeDeChamp.type_champs.fetch(:linked_drop_down_list) }
drop_down_list { create(:drop_down_list) }
end
factory :type_de_champ_pays, class: 'TypesDeChamp::PaysTypeDeChamp' do
factory :type_de_champ_pays do
type_champ { TypeDeChamp.type_champs.fetch(:pays) }
end
factory :type_de_champ_regions, class: 'TypesDeChamp::RegionTypeDeChamp' do
factory :type_de_champ_regions do
type_champ { TypeDeChamp.type_champs.fetch(:regions) }
end
factory :type_de_champ_departements, class: 'TypesDeChamp::DepartementTypeDeChamp' do
factory :type_de_champ_departements do
type_champ { TypeDeChamp.type_champs.fetch(:departements) }
end
factory :type_de_champ_engagement, class: 'TypesDeChamp::EngagementTypeDeChamp' do
factory :type_de_champ_engagement do
type_champ { TypeDeChamp.type_champs.fetch(:engagement) }
end
factory :type_de_champ_header_section, class: 'TypesDeChamp::HeaderSectionTypeDeChamp' do
factory :type_de_champ_header_section do
type_champ { TypeDeChamp.type_champs.fetch(:header_section) }
end
factory :type_de_champ_explication, class: 'TypesDeChamp::ExplicationTypeDeChamp' do
factory :type_de_champ_explication do
type_champ { TypeDeChamp.type_champs.fetch(:explication) }
end
factory :type_de_champ_dossier_link, class: 'TypesDeChamp::DossierLinkTypeDeChamp' do
factory :type_de_champ_dossier_link do
libelle { 'Référence autre dossier' }
type_champ { TypeDeChamp.type_champs.fetch(:dossier_link) }
end
factory :type_de_champ_piece_justificative, class: 'TypesDeChamp::PieceJustificativeTypeDeChamp' do
factory :type_de_champ_piece_justificative do
type_champ { TypeDeChamp.type_champs.fetch(:piece_justificative) }
end
factory :type_de_champ_siret, class: 'TypesDeChamp::SiretTypeDeChamp' do
factory :type_de_champ_siret do
type_champ { TypeDeChamp.type_champs.fetch(:siret) }
end

View file

@ -3,7 +3,7 @@ describe '2018_05_14_add_annotation_privee_to_procedure' do
let!(:procedure) do
procedure = create(:procedure)
10.times do |i|
TypesDeChamp::NumberTypeDeChamp.create(
TypeDeChamp.create(
procedure: procedure,
private: false,
libelle: 'libelle',
@ -11,7 +11,7 @@ describe '2018_05_14_add_annotation_privee_to_procedure' do
type_champ: 'number'
)
TypesDeChamp::NumberTypeDeChamp.create(
TypeDeChamp.create(
procedure: procedure,
private: true,
libelle: 'libelle',

View file

@ -95,7 +95,7 @@ describe '2018_07_31_nutriscore' do
context 'with champ type mismatch' do
let!(:type_champ_to) { create(:type_de_champ_text, order_place: 1, libelle: 'texte', procedure: proc_to) }
it { expect { run_task }.to raise_exception(/incorrect type TypesDeChamp::TextareaTypeDeChamp \(expected TypesDeChamp::TextTypeDeChamp\), incorrect type champ textarea \(expected text\)$/) }
it { expect { run_task }.to raise_exception(/incorrect type champ textarea \(expected text\)$/) }
end
context 'with champ mandatoriness mismatch' do
@ -114,13 +114,13 @@ describe '2018_07_31_nutriscore' do
context 'with siret mismatch on source' do
let!(:type_champ_siret_from) { create(:type_de_champ_textarea, order_place: 2, libelle: 'Numéro SIRET', procedure: proc_from) }
it { expect { run_task }.to raise_exception(/incorrect type TypesDeChamp::TextareaTypeDeChamp \(expected TypesDeChamp::TextTypeDeChamp\), incorrect type champ textarea \(expected text\)$/) }
it { expect { run_task }.to raise_exception(/incorrect type champ textarea \(expected text\)$/) }
end
context 'with siret mismatch on destination' do
let!(:type_champ_siret_to) { create(:type_de_champ_text, order_place: 2, libelle: 'Numéro SIRET', procedure: proc_to) }
it { expect { run_task }.to raise_exception(/incorrect type TypesDeChamp::TextTypeDeChamp \(expected TypesDeChamp::SiretTypeDeChamp\), incorrect type champ text \(expected siret\)$/) }
it { expect { run_task }.to raise_exception(/incorrect type champ text \(expected siret\)$/) }
end
end
end

View file

@ -21,7 +21,7 @@ shared_examples 'type_de_champ_spec' do
type_de_champ = create(:"type_de_champ_#{type_champ}")
champ = type_de_champ.champ.create
expect(type_de_champ.class.name).to match(/^TypesDeChamp::/)
expect(type_de_champ.dynamic_type.class.name).to match(/^TypesDeChamp::/)
expect(champ.class.name).to match(/^Champs::/)
end
end

View file

@ -3,12 +3,14 @@ require 'spec_helper'
describe TypesDeChamp::LinkedDropDownListTypeDeChamp do
describe '#unpack_options' do
let(:drop_down_list) { build(:drop_down_list, value: menu_options) }
let(:type_de_champ) { described_class.new(drop_down_list: drop_down_list) }
let(:type_de_champ) { build(:type_de_champ_linked_drop_down_list, drop_down_list: drop_down_list) }
subject { type_de_champ.dynamic_type }
context 'with no options' do
let(:menu_options) { '' }
it { expect(type_de_champ.secondary_options).to eq({}) }
it { expect(type_de_champ.primary_options).to eq([]) }
it { expect(subject.secondary_options).to eq({}) }
it { expect(subject.primary_options).to eq([]) }
end
context 'with two primary options' do
@ -25,7 +27,7 @@ describe TypesDeChamp::LinkedDropDownListTypeDeChamp do
end
it do
expect(type_de_champ.secondary_options).to eq(
expect(subject.secondary_options).to eq(
{
'' => [],
'Primary 1' => [ '', 'secondary 1.1', 'secondary 1.2'],
@ -34,7 +36,7 @@ describe TypesDeChamp::LinkedDropDownListTypeDeChamp do
)
end
it { expect(type_de_champ.primary_options).to eq([ '', 'Primary 1', 'Primary 2' ]) }
it { expect(subject.primary_options).to eq([ '', 'Primary 1', 'Primary 2' ]) }
end
end
end