GeoArea on API expose fields per source type

This commit is contained in:
Paul Chavard 2018-10-23 15:40:39 +02:00
parent 5a8a4b393f
commit fd490efb59
3 changed files with 61 additions and 21 deletions

View file

@ -1,16 +1,34 @@
class GeoAreaSerializer < ActiveModel::Serializer
attributes :geometry,
:source,
:surface_intersection,
:surface_parcelle,
:numero,
:feuille,
:section,
:code_dep,
:nom_com,
:code_com,
:code_arr,
:code,
:nom,
:commune
attributes :geometry, :source
attribute :surface_intersection, if: :include_cadastre?
attribute :surface_parcelle, if: :include_cadastre?
attribute :numero, if: :include_cadastre?
attribute :feuille, if: :include_cadastre?
attribute :section, if: :include_cadastre?
attribute :code_dep, if: :include_cadastre?
attribute :nom_com, if: :include_cadastre?
attribute :code_com, if: :include_cadastre?
attribute :code_arr, if: :include_cadastre?
attribute :code, if: :include_quartier_prioritaire?
attribute :nom, if: :include_quartier_prioritaire?
attribute :commune, if: :include_quartier_prioritaire?
attribute :culture, if: :include_parcelle_agricole?
attribute :code_culture, if: :include_parcelle_agricole?
attribute :surface, if: :include_parcelle_agricole?
attribute :bio, if: :include_parcelle_agricole?
def include_cadastre?
object.source == GeoArea.sources.fetch(:cadastre)
end
def include_quartier_prioritaire?
object.source == GeoArea.sources.fetch(:quartier_prioritaire)
end
def include_parcelle_agricole?
object.source == GeoArea.sources.fetch(:parcelle_agricole)
end
end

View file

@ -3,5 +3,11 @@ FactoryBot.define do
source { GeoArea.sources.fetch(:cadastre) }
numero { '42' }
feuille { 'A11' }
trait :quartier_prioritaire do
source { GeoArea.sources.fetch(:quartier_prioritaire) }
nom { 'XYZ' }
commune { 'Paris' }
end
end
end

View file

@ -6,13 +6,29 @@ describe Champs::CarteChampSerializer do
let(:geo_area) { create(:geo_area) }
let(:champ) { create(:type_de_champ_carte).champ.create(geo_areas: [geo_area]) }
it {
expect(subject[:geo_areas].first).to include(
source: GeoArea.sources.fetch(:cadastre),
numero: '42',
feuille: 'A11'
)
}
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