Refactor ChampSerializer
This commit is contained in:
parent
0b017580c5
commit
e374a5c726
11 changed files with 148 additions and 96 deletions
|
@ -5,16 +5,36 @@ class ChampSerializer < ActiveModel::Serializer
|
|||
|
||||
has_one :type_de_champ
|
||||
|
||||
has_many :geo_areas, if: :include_geo_areas?
|
||||
has_one :etablissement, if: :include_etablissement?
|
||||
has_one :entreprise, if: :include_etablissement?
|
||||
|
||||
def value
|
||||
case object
|
||||
when GeoArea, UserGeometry, Cadastre, QuartierPrioritaire
|
||||
object.geometry
|
||||
else
|
||||
when Champs::CarteChamp
|
||||
if object.value.present?
|
||||
JSON.parse(object.value)
|
||||
end
|
||||
when Champs::DecimalNumberChamp
|
||||
if object.value.present?
|
||||
object.value.to_f
|
||||
end
|
||||
when Champs::IntegerNumberChamp
|
||||
if object.value.present?
|
||||
object.value.to_i
|
||||
end
|
||||
when Champs::LinkedDropDownListChamp
|
||||
if object.value.present?
|
||||
{ primary: object.primary_value, secondary: object.secondary_value }
|
||||
end
|
||||
when Champs::PieceJustificativeChamp
|
||||
if object.piece_justificative_file.attached?
|
||||
url_for(object.piece_justificative_file)
|
||||
else
|
||||
object.value
|
||||
end
|
||||
else
|
||||
object.value
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -27,6 +47,22 @@ class ChampSerializer < ActiveModel::Serializer
|
|||
end
|
||||
end
|
||||
|
||||
def etablissement
|
||||
object.etablissement
|
||||
end
|
||||
|
||||
def entreprise
|
||||
object.etablissement&.entreprise
|
||||
end
|
||||
|
||||
def include_etablissement?
|
||||
object.is_a?(Champs::SiretChamp)
|
||||
end
|
||||
|
||||
def include_geo_areas?
|
||||
object.is_a?(Champs::CarteChamp)
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def legacy_type_de_champ
|
||||
|
|
|
@ -1,11 +0,0 @@
|
|||
class Champs::CarteChampSerializer < ChampSerializer
|
||||
has_many :geo_areas
|
||||
|
||||
def value
|
||||
if object.value.present?
|
||||
JSON.parse(object.value)
|
||||
else
|
||||
nil
|
||||
end
|
||||
end
|
||||
end
|
|
@ -1,7 +0,0 @@
|
|||
class Champs::DecimalNumberChampSerializer < ChampSerializer
|
||||
def value
|
||||
if object.value.present?
|
||||
object.value.to_f
|
||||
end
|
||||
end
|
||||
end
|
|
@ -1,7 +0,0 @@
|
|||
class Champs::IntegerNumberChampSerializer < ChampSerializer
|
||||
def value
|
||||
if object.value.present?
|
||||
object.value.to_i
|
||||
end
|
||||
end
|
||||
end
|
|
@ -1,5 +0,0 @@
|
|||
class Champs::LinkedDropDownListChampSerializer < ChampSerializer
|
||||
def value
|
||||
{ primary: object.primary_value, secondary: object.secondary_value }
|
||||
end
|
||||
end
|
|
@ -1,12 +0,0 @@
|
|||
class Champs::SiretChampSerializer < ChampSerializer
|
||||
has_one :etablissement
|
||||
has_one :entreprise
|
||||
|
||||
def etablissement
|
||||
object.etablissement
|
||||
end
|
||||
|
||||
def entreprise
|
||||
object.etablissement&.entreprise
|
||||
end
|
||||
end
|
|
@ -28,4 +28,41 @@ FactoryBot.define do
|
|||
end
|
||||
end
|
||||
end
|
||||
|
||||
factory :champ_integer_number, class: 'Champs::IntegerNumberChamp' do
|
||||
type_de_champ { create(:type_de_champ_integer_number) }
|
||||
value { '42' }
|
||||
end
|
||||
|
||||
factory :champ_decimal_number, class: 'Champs::DecimalNumberChamp' do
|
||||
type_de_champ { create(:type_de_champ_decimal_number) }
|
||||
value { '42.1' }
|
||||
end
|
||||
|
||||
factory :champ_linked_drop_down_list, class: 'Champs::LinkedDropDownListChamp' do
|
||||
type_de_champ { create(:type_de_champ_linked_drop_down_list) }
|
||||
value { '{}' }
|
||||
end
|
||||
|
||||
factory :champ_carte, class: 'Champs::CarteChamp' do
|
||||
type_de_champ { create(:type_de_champ_carte) }
|
||||
end
|
||||
|
||||
factory :champ_siret, class: 'Champs::SiretChamp' do
|
||||
type_de_champ { create(:type_de_champ_siret) }
|
||||
value { '44011762001530' }
|
||||
etablissement { create(:etablissement) }
|
||||
|
||||
before(:create) do |champ, evaluator|
|
||||
champ.etablissement.signature = champ.etablissement.sign
|
||||
end
|
||||
end
|
||||
|
||||
factory :champ_piece_justificative, class: 'Champs::PieceJustificativeChamp' do
|
||||
type_de_champ { create(:type_de_champ_piece_justificative) }
|
||||
|
||||
after(:create) do |champ, evaluator|
|
||||
champ.piece_justificative_file.attach(io: StringIO.new("toto"), filename: "toto.txt", content_type: "text/plain")
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -5,7 +5,7 @@ describe ChampSerializer do
|
|||
context 'when type champ is piece justificative' do
|
||||
include Rails.application.routes.url_helpers
|
||||
|
||||
let(:champ) { create(:champ, type_de_champ: create(:type_de_champ_piece_justificative)) }
|
||||
let(:champ) { create(:champ_piece_justificative) }
|
||||
|
||||
before { champ.piece_justificative_file.attach({ filename: __FILE__, io: File.open(__FILE__) }) }
|
||||
after { champ.piece_justificative_file.purge }
|
||||
|
@ -18,5 +18,45 @@ describe ChampSerializer do
|
|||
|
||||
it { is_expected.to include(value: "blah") }
|
||||
end
|
||||
|
||||
context 'when type champ is carte' do
|
||||
let(:geo_area) { create(:geo_area) }
|
||||
let(:champ) { create(:type_de_champ_carte).champ.create(geo_areas: [geo_area]) }
|
||||
|
||||
context 'and geo_area is cadastre' do
|
||||
it {
|
||||
expect(subject[:geo_areas].first).to include(
|
||||
source: GeoArea.sources.fetch(:cadastre),
|
||||
numero: '42',
|
||||
feuille: 'A11'
|
||||
)
|
||||
expect(subject[:geo_areas].first.key?(:nom)).to be_falsey
|
||||
}
|
||||
end
|
||||
|
||||
context 'and geo_area is quartier_prioritaire' do
|
||||
let(:geo_area) { create(:geo_area, :quartier_prioritaire) }
|
||||
|
||||
it {
|
||||
expect(subject[:geo_areas].first).to include(
|
||||
source: GeoArea.sources.fetch(:quartier_prioritaire),
|
||||
nom: 'XYZ',
|
||||
commune: 'Paris'
|
||||
)
|
||||
expect(subject[:geo_areas].first.key?(:numero)).to be_falsey
|
||||
}
|
||||
end
|
||||
end
|
||||
|
||||
context 'when type champ is siret' do
|
||||
let(:etablissement) { create(:etablissement) }
|
||||
let(:champ) { create(:type_de_champ_siret).champ.create(etablissement: etablissement, value: etablissement.siret) }
|
||||
|
||||
it {
|
||||
is_expected.to include(value: etablissement.siret)
|
||||
expect(subject[:etablissement]).to include(siret: etablissement.siret)
|
||||
expect(subject[:entreprise]).to include(capital_social: etablissement.entreprise_capital_social)
|
||||
}
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -1,34 +0,0 @@
|
|||
describe Champs::CarteChampSerializer do
|
||||
describe '#attributes' do
|
||||
subject { Champs::CarteChampSerializer.new(champ).serializable_hash }
|
||||
|
||||
context 'when type champ is carte' do
|
||||
let(:geo_area) { create(:geo_area) }
|
||||
let(:champ) { create(:type_de_champ_carte).champ.create(geo_areas: [geo_area]) }
|
||||
|
||||
context 'and geo_area is cadastre' do
|
||||
it {
|
||||
expect(subject[:geo_areas].first).to include(
|
||||
source: GeoArea.sources.fetch(:cadastre),
|
||||
numero: '42',
|
||||
feuille: 'A11'
|
||||
)
|
||||
expect(subject[:geo_areas].first.key?(:nom)).to be_falsey
|
||||
}
|
||||
end
|
||||
|
||||
context 'and geo_area is quartier_prioritaire' do
|
||||
let(:geo_area) { create(:geo_area, :quartier_prioritaire) }
|
||||
|
||||
it {
|
||||
expect(subject[:geo_areas].first).to include(
|
||||
source: GeoArea.sources.fetch(:quartier_prioritaire),
|
||||
nom: 'XYZ',
|
||||
commune: 'Paris'
|
||||
)
|
||||
expect(subject[:geo_areas].first.key?(:numero)).to be_falsey
|
||||
}
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
|
@ -1,16 +0,0 @@
|
|||
describe Champs::SiretChampSerializer do
|
||||
describe '#attributes' do
|
||||
subject { Champs::SiretChampSerializer.new(champ).serializable_hash }
|
||||
|
||||
context 'when type champ is siret' do
|
||||
let(:etablissement) { create(:etablissement) }
|
||||
let(:champ) { create(:type_de_champ_siret).champ.create(etablissement: etablissement, value: etablissement.siret) }
|
||||
|
||||
it {
|
||||
is_expected.to include(value: etablissement.siret)
|
||||
expect(subject[:etablissement]).to include(siret: etablissement.siret)
|
||||
expect(subject[:entreprise]).to include(capital_social: etablissement.entreprise_capital_social)
|
||||
}
|
||||
end
|
||||
end
|
||||
end
|
|
@ -14,5 +14,36 @@ describe DossierSerializer do
|
|||
|
||||
it { is_expected.to include(received_at: dossier.en_instruction_at) }
|
||||
end
|
||||
|
||||
context 'champs' do
|
||||
subject { super()[:champs] }
|
||||
|
||||
let(:dossier) { create(:dossier, :en_construction, :with_two_quartier_prioritaires, procedure: create(:procedure, :published, :with_api_carto, :with_type_de_champ)) }
|
||||
|
||||
before do
|
||||
dossier.champs << create(:champ_carte)
|
||||
dossier.champs << create(:champ_siret)
|
||||
dossier.champs << create(:champ_integer_number)
|
||||
dossier.champs << create(:champ_decimal_number)
|
||||
dossier.champs << create(:champ_linked_drop_down_list)
|
||||
end
|
||||
|
||||
it {
|
||||
expect(subject.size).to eq(8)
|
||||
|
||||
expect(subject[0][:type_de_champ][:type_champ]).to eq(TypeDeChamp.type_champs.fetch(:text))
|
||||
expect(subject[1][:type_de_champ][:type_champ]).to eq(TypeDeChamp.type_champs.fetch(:carte))
|
||||
expect(subject[2][:type_de_champ][:type_champ]).to eq(TypeDeChamp.type_champs.fetch(:siret))
|
||||
expect(subject[7][:type_de_champ][:type_champ]).to eq('quartier_prioritaire')
|
||||
|
||||
expect(subject[1][:geo_areas].size).to eq(0)
|
||||
expect(subject[2][:etablissement]).to be_present
|
||||
expect(subject[2][:entreprise]).to be_present
|
||||
|
||||
expect(subject[3][:value]).to eq(42)
|
||||
expect(subject[4][:value]).to eq(42.1)
|
||||
expect(subject[5][:value]).to eq({ primary: nil, secondary: nil })
|
||||
}
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue