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:
commit
c8ed0532ed
5 changed files with 30 additions and 2 deletions
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
|
@ -2,6 +2,7 @@ FactoryBot.define do
|
|||
factory :geo_area do
|
||||
association :champ
|
||||
properties { {} }
|
||||
geometry { {} }
|
||||
|
||||
trait :cadastre do
|
||||
source { GeoArea.sources.fetch(:cadastre) }
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
Loading…
Reference in a new issue