Merge pull request #8948 from colinux/fix-geoarea-geometry-blank

ETQ Usager, champ carte: ne permet pas d'enregistrer une geometry null pour ne pas casser les exports
This commit is contained in:
LeSim 2023-04-26 10:15:25 +02:00 committed by GitHub
commit c8ed0532ed
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 30 additions and 2 deletions

View file

@ -51,7 +51,7 @@ class GeoArea < ApplicationRecord
scope :selections_utilisateur, -> { where(source: sources.fetch(:selection_utilisateur)) }
scope :cadastres, -> { where(source: sources.fetch(:cadastre)) }
validates :geometry, geo_json: true, allow_blank: false
validates :geometry, geo_json: true, allow_nil: false
before_validation :normalize_geometry
def to_feature

View file

@ -1,9 +1,13 @@
class GeoJSONValidator < ActiveModel::EachValidator
def validate_each(record, attribute, value)
if options[:allow_nil] == false && value.nil?
record.errors.add(attribute, :blank, message: options[:message] || "ne peut pas être vide")
end
begin
RGeo::GeoJSON.decode(value.to_json, geo_factory: RGeo::Geographic.simple_mercator_factory)
rescue RGeo::Error::InvalidGeometry
record.errors[attribute] << (options[:message] || "n'est pas un GeoJSON valide")
record.errors.add(attribute, :invalid_geometry, message: options[:message] || "n'est pas un GeoJSON valide")
end
end
end

View file

@ -0,0 +1,13 @@
namespace :after_party do
desc 'Deployment task: fix_geo_area_without_geometry_again'
task fix_geo_area_without_geometry_again: :environment do
puts "Running deploy task 'fix_geo_area_without_geometry_again'"
Rake::Task['after_party:fix_geo_area_without_geometry'].invoke
# Update task as completed. If you remove the line below, the task will
# run with every deploy (or every time you call after_party:run).
AfterParty::TaskRecord
.create version: AfterParty::TaskRecorder.new(__FILE__).timestamp
end
end

View file

@ -2,6 +2,7 @@ FactoryBot.define do
factory :geo_area do
association :champ
properties { {} }
geometry { {} }
trait :cadastre do
source { GeoArea.sources.fetch(:cadastre) }

View file

@ -81,6 +81,16 @@ RSpec.describe GeoArea, type: :model do
let(:geo_area) { build(:geo_area, :invalid_right_hand_rule_polygon, champ: nil) }
it { expect(geo_area.errors).to have_key(:geometry) }
end
context "nil" do
let(:geo_area) { build(:geo_area, geometry: nil) }
it { expect(geo_area.errors).to have_key(:geometry) }
end
context "allow empty {}" do
let(:geo_area) { build(:geo_area, geometry: {}) }
it { expect(geo_area.errors).not_to have_key(:geometry) }
end
end
end