diff --git a/app/models/champs/carte_champ.rb b/app/models/champs/carte_champ.rb index 3aa35bac9..d01f5e31b 100644 --- a/app/models/champs/carte_champ.rb +++ b/app/models/champs/carte_champ.rb @@ -76,7 +76,7 @@ class Champs::CarteChamp < Champ type: 'FeatureCollection', id: stable_id, bbox: bounding_box, - features: (legacy_selections_utilisateur + except_selections_utilisateur).map(&:to_feature) + features: geo_areas.map(&:to_feature) } end @@ -84,23 +84,6 @@ class Champs::CarteChamp < Champ geo_areas.present? end - def selection_utilisateur_legacy_geometry - if selection_utilisateur_legacy? - selections_utilisateur.first.geometry - elsif selections_utilisateur.present? - { - type: 'MultiPolygon', - coordinates: selections_utilisateur.filter do |selection_utilisateur| - selection_utilisateur.geometry['type'] == 'Polygon' - end.map do |selection_utilisateur| - selection_utilisateur.geometry['coordinates'] - end - } - else - nil - end - end - def selection_utilisateur_legacy_geo_area geometry = selection_utilisateur_legacy_geometry if geometry.present? @@ -131,35 +114,18 @@ class Champs::CarteChamp < Champ private - def selection_utilisateur_legacy? - if selections_utilisateur.size == 1 - geometry = selections_utilisateur.first.geometry - return geometry && geometry['type'] == 'MultiPolygon' - end - - false - end - - def legacy_selections_utilisateur - if selection_utilisateur_legacy? - selections_utilisateur.first.geometry['coordinates'].map do |coordinates| - GeoArea.new( - geometry: { - type: 'Polygon', - coordinates: coordinates - }, - properties: {}, - source: GeoArea.sources.fetch(:selection_utilisateur) - ) - end + def selection_utilisateur_legacy_geometry + if selections_utilisateur.present? + { + type: 'MultiPolygon', + coordinates: selections_utilisateur.filter do |selection_utilisateur| + selection_utilisateur.geometry['type'] == 'Polygon' + end.map do |selection_utilisateur| + selection_utilisateur.geometry['coordinates'] + end + } else - selections_utilisateur - end - end - - def except_selections_utilisateur - geo_areas.filter do |area| - area.source != GeoArea.sources.fetch(:selection_utilisateur) + nil end end end diff --git a/lib/tasks/deployment/20200414104712_split_geo_area_selection_multipolygons.rake b/lib/tasks/deployment/20200414104712_split_geo_area_selection_multipolygons.rake deleted file mode 100644 index 930a650aa..000000000 --- a/lib/tasks/deployment/20200414104712_split_geo_area_selection_multipolygons.rake +++ /dev/null @@ -1,18 +0,0 @@ -namespace :after_party do - desc 'Deployment task: split_geo_area_selection_multipolygons' - task split_geo_area_selection_multipolygons: :environment do - puts "Running deploy task 'split_geo_area_selection_multipolygons'" - - Champs::CarteChamp.where.not(value: ['', '[]']).includes(:geo_areas).find_each do |champ| - if champ.send(:selection_utilisateur_legacy?) - legacy_selection_utilisateur = champ.selections_utilisateur.first - champ.send(:legacy_selections_utilisateur).each do |area| - champ.geo_areas << area - end - legacy_selection_utilisateur.destroy - end - end - - AfterParty::TaskRecord.create version: '20200414104712' - end -end diff --git a/spec/factories/geo_area.rb b/spec/factories/geo_area.rb index e46aa7bea..3f65aef6d 100644 --- a/spec/factories/geo_area.rb +++ b/spec/factories/geo_area.rb @@ -58,5 +58,27 @@ FactoryBot.define do } end end + + trait :multi_polygon do + geometry do + { + "type": "MultiPolygon", + "coordinates": [ + [ + [ + [2.428439855575562, 46.538476837725796], + [2.4284291267395024, 46.53842148758162], + [2.4282521009445195, 46.53841410755813], + [2.42824137210846, 46.53847314771794], + [2.428284287452698, 46.53847314771794], + [2.428364753723145, 46.538487907747864], + [2.4284291267395024, 46.538491597754714], + [2.428439855575562, 46.538476837725796] + ] + ] + ] + } + end + end end end diff --git a/spec/serializers/champ_serializer_spec.rb b/spec/serializers/champ_serializer_spec.rb index 54af2b224..1543bac2b 100644 --- a/spec/serializers/champ_serializer_spec.rb +++ b/spec/serializers/champ_serializer_spec.rb @@ -24,16 +24,9 @@ describe ChampSerializer do end 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, :cadastre, geometry: geo_json) } - let(:geo_json) do - { - "type" => 'MultiPolygon', - "coordinates" => coordinates - } - end - let(:coordinates) { [[[2.3859214782714844, 48.87442541960633], [2.3850631713867183, 48.87273183590832], [2.3809432983398438, 48.87081237174292], [2.3859214782714844, 48.87442541960633]]] } + let(:champ) { create(:champ_carte, geo_areas: [geo_area].compact) } + let(:geo_area) { create(:geo_area, :cadastre, :multi_polygon) } + let(:geo_json) { attributes_for(:geo_area, :multi_polygon)[:geometry].stringify_keys } let(:serialized_champ) { { @@ -56,29 +49,7 @@ describe ChampSerializer do 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 } - - it { expect(champ.selection_utilisateur_legacy_geo_area).to be_nil } - end - - context 'when value is empty array' do - let(:value) { '[]' } - - it { expect(champ.selection_utilisateur_legacy_geo_area).to be_nil } - end - - context 'when value is blank' do - let(:value) { '' } - - it { expect(champ.selection_utilisateur_legacy_geo_area).to be_nil } - end - end + let(:geo_area) { create(:geo_area, :selection_utilisateur, :polygon) } context 'old_api' do let(:serialized_libelle) { "user geometry" } @@ -87,14 +58,6 @@ describe ChampSerializer do let(:serializable_object) { champ.selection_utilisateur_legacy_geo_area } context 'when value is coordinates' do - let(:value) { coordinates.to_json } - - it { expect(subject).to eq(serialized_champ) } - end - - context 'when value is geojson' do - let(:value) { geo_json.to_json } - it { expect(subject).to eq(serialized_champ) } end end @@ -120,33 +83,6 @@ describe ChampSerializer do it { expect(subject).to eq(serialized_champ) } end - - context 'when value is geojson' do - let(:value) { geo_json.to_json } - - it { expect(subject).to eq(serialized_champ) } - end - - context 'when value is nil' do - let(:value) { nil } - let(:serialized_value) { nil } - - it { expect(subject).to eq(serialized_champ) } - end - - context 'when value is empty array' do - let(:value) { '[]' } - let(:serialized_value) { nil } - - it { expect(subject).to eq(serialized_champ) } - end - - context 'when value is blank' do - let(:value) { '' } - let(:serialized_value) { nil } - - it { expect(subject).to eq(serialized_champ) } - end end end