Simplify API carte serializer

This commit is contained in:
Paul Chavard 2018-11-27 15:55:14 +01:00
parent 36f6f54337
commit d988638c58
6 changed files with 18 additions and 83 deletions

View file

@ -11,7 +11,7 @@ class ChampSerializer < ActiveModel::Serializer
def value
case object
when GeoArea, UserGeometry, Cadastre, QuartierPrioritaire
when GeoArea
object.geometry
when Champs::CarteChamp
if object.value.present?
@ -40,7 +40,7 @@ class ChampSerializer < ActiveModel::Serializer
def type_de_champ
case object
when GeoArea, UserGeometry, Cadastre, QuartierPrioritaire
when GeoArea
legacy_type_de_champ
else
object.type_de_champ
@ -76,28 +76,18 @@ class ChampSerializer < ActiveModel::Serializer
end
def legacy_carto_libelle
case object
when UserGeometry, Cadastre, QuartierPrioritaire
object.class.name.underscore.tr('_', ' ')
if object.source == GeoArea.sources.fetch(:selection_utilisateur)
'user geometry'
else
if object.source == GeoArea.sources.fetch(:selection_utilisateur)
'user geometry'
else
object.source.to_s.tr('_', ' ')
end
object.source.to_s.tr('_', ' ')
end
end
def legacy_carto_type_champ
case object
when UserGeometry, Cadastre, QuartierPrioritaire
object.class.name.underscore
if object.source == GeoArea.sources.fetch(:selection_utilisateur)
'user_geometry'
else
if object.source == GeoArea.sources.fetch(:selection_utilisateur)
'user_geometry'
else
object.source.to_s
end
object.source.to_s
end
end
end

View file

@ -28,14 +28,7 @@ class DossierSerializer < ActiveModel::Serializer
def champs
champs = object.champs.to_a
if object.use_legacy_carto?
champs += object.quartier_prioritaires
champs += object.cadastres
if object.user_geometry.present?
champs << object.user_geometry
end
elsif object.expose_legacy_carto_api?
if object.expose_legacy_carto_api?
champ_carte = champs.find do |champ|
champ.type_de_champ.type_champ == TypeDeChamp.type_champs.fetch(:carte)
end

View file

@ -37,6 +37,10 @@ class ProcedureSerializer < ActiveModel::Serializer
end
def geographic_information
object.module_api_carto
if object.expose_legacy_carto_api?
object.module_api_carto
else
ModuleAPICarto.new(procedure: object)
end
end
end

View file

@ -3,7 +3,7 @@ require 'spec_helper'
describe API::V1::DossiersController do
let(:admin) { create(:administrateur) }
let(:token) { admin.renew_api_token }
let(:procedure) { create(:procedure, :with_api_carto, :with_two_type_de_piece_justificative, :with_type_de_champ, :with_type_de_champ_private, administrateur: admin) }
let(:procedure) { create(:procedure, :with_two_type_de_piece_justificative, :with_type_de_champ, :with_type_de_champ_private, administrateur: admin) }
let(:wrong_procedure) { create(:procedure) }
it { expect(described_class).to be < APIController }
@ -253,46 +253,6 @@ describe API::V1::DossiersController do
it { expect(subject[:type_champ]).to eq('text') }
end
end
context 'when the dossier includes a quartier prioritaire' do
before do
dossier.quartier_prioritaires << create(:quartier_prioritaire)
end
subject do
super().find { |champ| champ[:type_de_champ][:type_champ] == 'quartier_prioritaire' }
end
it { expect(subject[:type_de_champ]).to match({ id: -1, libelle: 'quartier prioritaire', type_champ: 'quartier_prioritaire', order_place: -1, descripton: '' }) }
it { expect(subject[:value]).to match(dossier.quartier_prioritaires.first.geometry.symbolize_keys) }
end
context 'when the dossier includes a cadastre' do
before do
dossier.cadastres << create(:cadastre)
end
subject do
super().find { |champ| champ[:type_de_champ][:type_champ] == 'cadastre' }
end
it { expect(subject[:type_de_champ]).to match({ id: -1, libelle: 'cadastre', type_champ: 'cadastre', order_place: -1, descripton: '' }) }
it { expect(subject[:value]).to match(dossier.cadastres.first.geometry.symbolize_keys) }
end
context 'when the dossier includes some user geometry' do
before do
dossier.json_latlngs = '[[{"lat": 2.0, "lng": 102.0}, {"lat": 3.0, "lng": 103.0}, {"lat": 2.0, "lng": 102.0}]]'
dossier.save
end
subject do
super().find { |champ| champ[:type_de_champ][:type_champ] == 'user_geometry' }
end
it { expect(subject[:type_de_champ]).to match({ id: -1, libelle: 'user geometry', type_champ: 'user_geometry', order_place: -1, descripton: '' }) }
it { expect(subject[:value]).to match(UserGeometry.new(dossier.json_latlngs).geometry) }
end
end
describe 'champs_private' do

View file

@ -25,8 +25,7 @@ describe API::V1::ProceduresController, type: :controller do
it { is_expected.to have_http_status(200) }
describe 'body' do
let(:module_api_carto) { create(:module_api_carto, use_api_carto: true, quartiers_prioritaires: true, cadastre: true) }
let(:procedure) { create(:procedure, :with_type_de_champ, :with_two_type_de_piece_justificative, module_api_carto: module_api_carto, administrateur: admin) }
let(:procedure) { create(:procedure, :with_type_de_champ, :with_two_type_de_piece_justificative, administrateur: admin) }
let(:response) { get :show, params: { id: procedure.id, token: token } }
subject { JSON.parse(response.body, symbolize_names: true)[:procedure] }
@ -65,16 +64,6 @@ describe API::V1::ProceduresController, type: :controller do
it { expect(subject[:libelle]).to eq(pj.libelle) }
it { expect(subject[:description]).to eq(pj.description) }
end
it { is_expected.to have_key(:geographic_information) }
describe 'geographic_information' do
subject { super()[:geographic_information] }
it { expect(subject[:use_api_carto]).to be_truthy }
it { expect(subject[:quartiers_prioritaires]).to be_truthy }
it { expect(subject[:cadastre]).to be_truthy }
end
end
end
end

View file

@ -18,7 +18,7 @@ describe DossierSerializer do
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)) }
let(:dossier) { create(:dossier, :en_construction, procedure: create(:procedure, :published, :with_type_de_champ)) }
before do
dossier.champs << create(:champ_carte)
@ -29,12 +29,11 @@ describe DossierSerializer do
end
it {
expect(subject.size).to eq(8)
expect(subject.size).to eq(6)
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