From 7cf225d443b77d4b9202c927dbb442be07e6f52f Mon Sep 17 00:00:00 2001 From: Paul Chavard Date: Mon, 22 Oct 2018 19:27:52 +0200 Subject: [PATCH] Add Carte champs support to the API --- .../champs/carte_champ_serializer.rb | 11 +++++++++++ app/serializers/geo_area_serializer.rb | 16 ++++++++++++++++ spec/factories/geo_area.rb | 7 +++++++ .../champs/carte_champ_serializer_spec.rb | 18 ++++++++++++++++++ 4 files changed, 52 insertions(+) create mode 100644 app/serializers/champs/carte_champ_serializer.rb create mode 100644 app/serializers/geo_area_serializer.rb create mode 100644 spec/factories/geo_area.rb create mode 100644 spec/serializers/champs/carte_champ_serializer_spec.rb diff --git a/app/serializers/champs/carte_champ_serializer.rb b/app/serializers/champs/carte_champ_serializer.rb new file mode 100644 index 000000000..da41b34f9 --- /dev/null +++ b/app/serializers/champs/carte_champ_serializer.rb @@ -0,0 +1,11 @@ +class Champs::CarteChampSerializer < ChampSerializer + has_many :geo_areas + + def value + if object.value.present? + JSON.parse(object.value) + else + nil + end + end +end diff --git a/app/serializers/geo_area_serializer.rb b/app/serializers/geo_area_serializer.rb new file mode 100644 index 000000000..7d501ff4c --- /dev/null +++ b/app/serializers/geo_area_serializer.rb @@ -0,0 +1,16 @@ +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 +end diff --git a/spec/factories/geo_area.rb b/spec/factories/geo_area.rb new file mode 100644 index 000000000..3050dc075 --- /dev/null +++ b/spec/factories/geo_area.rb @@ -0,0 +1,7 @@ +FactoryBot.define do + factory :geo_area do + source { GeoArea.sources.fetch(:cadastre) } + numero { '42' } + feuille { 'A11' } + end +end diff --git a/spec/serializers/champs/carte_champ_serializer_spec.rb b/spec/serializers/champs/carte_champ_serializer_spec.rb new file mode 100644 index 000000000..66419ac57 --- /dev/null +++ b/spec/serializers/champs/carte_champ_serializer_spec.rb @@ -0,0 +1,18 @@ +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]) } + + it { + expect(subject[:geo_areas].first).to include( + source: GeoArea.sources.fetch(:cadastre), + numero: '42', + feuille: 'A11' + ) + } + end + end +end