From c75e39884e2833bf21ed86cd586b6fb54620f4ed Mon Sep 17 00:00:00 2001 From: Paul Chavard Date: Wed, 19 Dec 2018 11:09:13 +0100 Subject: [PATCH] Save selection utilisateur as geo area --- app/controllers/champs/carte_controller.rb | 6 +- app/models/champs/carte_champ.rb | 80 ++++++++++++++-------- app/models/geo_area.rb | 4 ++ app/serializers/dossier_serializer.rb | 4 +- app/services/api_carto_service.rb | 6 ++ spec/factories/geo_area.rb | 4 ++ spec/models/champs/carte_champ_spec.rb | 12 ++-- spec/serializers/champ_serializer_spec.rb | 24 ++++--- 8 files changed, 94 insertions(+), 46 deletions(-) diff --git a/app/controllers/champs/carte_controller.rb b/app/controllers/champs/carte_controller.rb index 1410a9cde..47c15da93 100644 --- a/app/controllers/champs/carte_controller.rb +++ b/app/controllers/champs/carte_controller.rb @@ -55,11 +55,15 @@ class Champs::CarteController < ApplicationController end end + selection_utilisateur = ApiCartoService.generate_selection_utilisateur(coordinates) + selection_utilisateur[:source] = GeoArea.sources.fetch(:selection_utilisateur) + geo_areas << selection_utilisateur + @champ.geo_areas = geo_areas.map do |geo_area| GeoArea.new(geo_area) end - @champ.value = GeojsonService.to_json_polygon_for_selection_utilisateur(coordinates) + @champ.value = coordinates.to_json end if @champ.persisted? diff --git a/app/models/champs/carte_champ.rb b/app/models/champs/carte_champ.rb index 7992d50e1..cb11520e6 100644 --- a/app/models/champs/carte_champ.rb +++ b/app/models/champs/carte_champ.rb @@ -19,6 +19,10 @@ class Champs::CarteChamp < Champ end end + def selection_utilisateur + geo_areas.find(&:selection_utilisateur?) + end + def cadastres? type_de_champ&.cadastres && type_de_champ.cadastres != '0' end @@ -45,6 +49,49 @@ class Champs::CarteChamp < Champ def geo_json @geo_json ||= begin + geo_area = selection_utilisateur + + if geo_area + geo_area.geometry + else + geo_json_from_value + end + end + end + + def selection_utilisateur_size + if geo_json.present? + geo_json['coordinates'].size + else + 0 + end + end + + def to_render_data + { + position: position, + selection: user_geo_area&.geometry, + quartiersPrioritaires: quartiers_prioritaires? ? quartiers_prioritaires.as_json(except: :properties) : [], + cadastres: cadastres? ? cadastres.as_json(except: :properties) : [], + parcellesAgricoles: parcelles_agricoles? ? parcelles_agricoles.as_json(except: :properties) : [] + } + end + + def user_geo_area + geo_area = selection_utilisateur + + if geo_area.present? + geo_area + elsif geo_json_from_value.present? + GeoArea.new( + geometry: geo_json_from_value, + source: GeoArea.sources.fetch(:selection_utilisateur) + ) + end + end + + def geo_json_from_value + @geo_json_from_value ||= begin parsed_value = value.blank? ? nil : JSON.parse(value) # We used to store in the value column a json array with coordinates. if parsed_value.is_a?(Array) @@ -62,34 +109,11 @@ class Champs::CarteChamp < Champ end end - def selection_utilisateur_size - if geo_json.present? - geo_json['coordinates'].size - else - 0 - end - end - - def to_render_data - { - position: position, - selection: geo_json, - quartiersPrioritaires: quartiers_prioritaires? ? quartiers_prioritaires.as_json(except: :properties) : [], - cadastres: cadastres? ? cadastres.as_json(except: :properties) : [], - parcellesAgricoles: parcelles_agricoles? ? parcelles_agricoles.as_json(except: :properties) : [] - } - end - - def user_geo_area - if geo_json.present? - GeoArea.new( - geometry: geo_json, - source: GeoArea.sources.fetch(:selection_utilisateur) - ) - end - end - def for_api - geo_json&.to_json + nil + end + + def for_export + nil end end diff --git a/app/models/geo_area.rb b/app/models/geo_area.rb index 05ad92ebd..7109d0313 100644 --- a/app/models/geo_area.rb +++ b/app/models/geo_area.rb @@ -30,4 +30,8 @@ class GeoArea < ApplicationRecord scope :quartiers_prioritaires, -> { where(source: sources.fetch(:quartier_prioritaire)) } scope :cadastres, -> { where(source: sources.fetch(:cadastre)) } scope :parcelles_agricoles, -> { where(source: sources.fetch(:parcelle_agricole)) } + + def selection_utilisateur? + source == self.class.sources.fetch(:selection_utilisateur) + end end diff --git a/app/serializers/dossier_serializer.rb b/app/serializers/dossier_serializer.rb index dbd3e03ad..358a88a21 100644 --- a/app/serializers/dossier_serializer.rb +++ b/app/serializers/dossier_serializer.rb @@ -39,7 +39,9 @@ class DossierSerializer < ActiveModel::Serializer if champ_carte.present? carto_champs = champ_carte.geo_areas.to_a - carto_champs << champ_carte.user_geo_area + if !carto_champs.find(&:selection_utilisateur?) + carto_champs << champ_carte.user_geo_area + end champs += carto_champs.compact end end diff --git a/app/services/api_carto_service.rb b/app/services/api_carto_service.rb index ec0f64dfe..9bca82a6c 100644 --- a/app/services/api_carto_service.rb +++ b/app/services/api_carto_service.rb @@ -22,4 +22,10 @@ class ApiCartoService ).results end end + + def self.generate_selection_utilisateur(coordinates) + { + geometry: JSON.parse(GeojsonService.to_json_polygon_for_selection_utilisateur(coordinates)) + } + end end diff --git a/spec/factories/geo_area.rb b/spec/factories/geo_area.rb index 33b2decde..12e87c679 100644 --- a/spec/factories/geo_area.rb +++ b/spec/factories/geo_area.rb @@ -9,5 +9,9 @@ FactoryBot.define do nom { 'XYZ' } commune { 'Paris' } end + + trait :selection_utilisateur do + source { GeoArea.sources.fetch(:selection_utilisateur) } + end end end diff --git a/spec/models/champs/carte_champ_spec.rb b/spec/models/champs/carte_champ_spec.rb index 8d0875a88..6beed9886 100644 --- a/spec/models/champs/carte_champ_spec.rb +++ b/spec/models/champs/carte_champ_spec.rb @@ -3,9 +3,9 @@ require 'spec_helper' describe Champs::CarteChamp do let(:champ) { Champs::CarteChamp.new(value: value) } let(:value) { '' } - let(:geo_json) { GeojsonService.to_json_polygon_for_selection_utilisateur(coordinates) } let(:coordinates) { [[{ "lat" => 48.87442541960633, "lng" => 2.3859214782714844 }, { "lat" => 48.87273183590832, "lng" => 2.3850631713867183 }, { "lat" => 48.87081237174292, "lng" => 2.3809432983398438 }, { "lat" => 48.8712640169951, "lng" => 2.377510070800781 }, { "lat" => 48.87510283703279, "lng" => 2.3778533935546875 }, { "lat" => 48.87544154230615, "lng" => 2.382831573486328 }, { "lat" => 48.87442541960633, "lng" => 2.3859214782714844 }]] } - let(:parsed_geo_json) { JSON.parse(geo_json) } + let(:geo_json_as_string) { GeojsonService.to_json_polygon_for_selection_utilisateur(coordinates) } + let(:geo_json) { JSON.parse(geo_json_as_string) } describe '#to_render_data' do subject { champ.to_render_data } @@ -47,15 +47,15 @@ describe Champs::CarteChamp do context 'when the value is coordinates' do let(:value) { coordinates.to_json } - let(:selection) { parsed_geo_json } + let(:selection) { geo_json } it { is_expected.to eq(render_data) } end context 'when the value is geojson' do - let(:value) { geo_json } + let(:value) { geo_json.to_json } - let(:selection) { parsed_geo_json } + let(:selection) { geo_json } it { is_expected.to eq(render_data) } end @@ -89,7 +89,7 @@ describe Champs::CarteChamp do end context 'when the value is geojson' do - let(:value) { geo_json } + let(:value) { geo_json.to_json } it { is_expected.to eq(1) } end diff --git a/spec/serializers/champ_serializer_spec.rb b/spec/serializers/champ_serializer_spec.rb index f2b4d2775..07ff5e10e 100644 --- a/spec/serializers/champ_serializer_spec.rb +++ b/spec/serializers/champ_serializer_spec.rb @@ -26,9 +26,9 @@ describe ChampSerializer do context 'when type champ is carte' do let(:champ) { create(:champ_carte, value: value, geo_areas: [geo_area].compact) } let(:value) { nil } - let(:geo_area) { create(:geo_area, geometry: parsed_geo_json) } - let(:parsed_geo_json) { JSON.parse(geo_json) } - let(:geo_json) { GeojsonService.to_json_polygon_for_selection_utilisateur(coordinates) } + let(:geo_area) { create(:geo_area, geometry: geo_json) } + let(:geo_json_as_string) { GeojsonService.to_json_polygon_for_selection_utilisateur(coordinates) } + let(:geo_json) { JSON.parse(geo_json_as_string) } let(:coordinates) { [[{ "lat" => 48.87442541960633, "lng" => 2.3859214782714844 }, { "lat" => 48.87273183590832, "lng" => 2.3850631713867183 }, { "lat" => 48.87081237174292, "lng" => 2.3809432983398438 }, { "lat" => 48.8712640169951, "lng" => 2.377510070800781 }, { "lat" => 48.87510283703279, "lng" => 2.3778533935546875 }, { "lat" => 48.87544154230615, "lng" => 2.382831573486328 }, { "lat" => 48.87442541960633, "lng" => 2.3859214782714844 }]] } let(:serialized_champ) { @@ -49,10 +49,14 @@ describe ChampSerializer do let(:serialized_id) { -1 } let(:serialized_description) { "" } let(:serialized_order_place) { -1 } - let(:serialized_value) { parsed_geo_json } + let(:serialized_value) { geo_json } context 'and geo_area is selection_utilisateur' do + let(:geo_area) { create(:geo_area, :selection_utilisateur, geometry: geo_json) } + context 'value is empty' do + let(:geo_area) { nil } + context 'when value is nil' do let(:value) { nil } @@ -85,7 +89,7 @@ describe ChampSerializer do end context 'when value is geojson' do - let(:value) { geo_json } + let(:value) { geo_json.to_json } it { expect(subject).to eq(serialized_champ) } end @@ -105,7 +109,7 @@ describe ChampSerializer do let(:serialized_order_place) { champ.order_place } let(:serialized_libelle) { champ.libelle } let(:serialized_type_champ) { champ.type_champ } - let(:serialized_value) { geo_json } + let(:serialized_value) { nil } context 'when value is coordinates' do let(:value) { coordinates.to_json } @@ -114,7 +118,7 @@ describe ChampSerializer do end context 'when value is geojson' do - let(:value) { geo_json } + let(:value) { geo_json.to_json } it { expect(subject).to eq(serialized_champ) } end @@ -147,7 +151,7 @@ describe ChampSerializer do it { expect(subject[:geo_areas].first).to include( source: GeoArea.sources.fetch(:cadastre), - geometry: parsed_geo_json, + geometry: geo_json, numero: '42', feuille: 'A11' ) @@ -165,13 +169,13 @@ describe ChampSerializer do end context 'and geo_area is quartier_prioritaire' do - let(:geo_area) { create(:geo_area, :quartier_prioritaire, geometry: parsed_geo_json) } + let(:geo_area) { create(:geo_area, :quartier_prioritaire, geometry: geo_json) } context 'new_api' do it { expect(subject[:geo_areas].first).to include( source: GeoArea.sources.fetch(:quartier_prioritaire), - geometry: parsed_geo_json, + geometry: geo_json, nom: 'XYZ', commune: 'Paris' )