fix(geoarea): geometry must not be nil
This commit is contained in:
parent
925ebef551
commit
25956c5141
4 changed files with 17 additions and 2 deletions
|
@ -51,7 +51,7 @@ class GeoArea < ApplicationRecord
|
||||||
scope :selections_utilisateur, -> { where(source: sources.fetch(:selection_utilisateur)) }
|
scope :selections_utilisateur, -> { where(source: sources.fetch(:selection_utilisateur)) }
|
||||||
scope :cadastres, -> { where(source: sources.fetch(:cadastre)) }
|
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
|
before_validation :normalize_geometry
|
||||||
|
|
||||||
def to_feature
|
def to_feature
|
||||||
|
|
|
@ -1,9 +1,13 @@
|
||||||
class GeoJSONValidator < ActiveModel::EachValidator
|
class GeoJSONValidator < ActiveModel::EachValidator
|
||||||
def validate_each(record, attribute, value)
|
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
|
begin
|
||||||
RGeo::GeoJSON.decode(value.to_json, geo_factory: RGeo::Geographic.simple_mercator_factory)
|
RGeo::GeoJSON.decode(value.to_json, geo_factory: RGeo::Geographic.simple_mercator_factory)
|
||||||
rescue RGeo::Error::InvalidGeometry
|
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
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -2,6 +2,7 @@ FactoryBot.define do
|
||||||
factory :geo_area do
|
factory :geo_area do
|
||||||
association :champ
|
association :champ
|
||||||
properties { {} }
|
properties { {} }
|
||||||
|
geometry { {} }
|
||||||
|
|
||||||
trait :cadastre do
|
trait :cadastre do
|
||||||
source { GeoArea.sources.fetch(:cadastre) }
|
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) }
|
let(:geo_area) { build(:geo_area, :invalid_right_hand_rule_polygon, champ: nil) }
|
||||||
it { expect(geo_area.errors).to have_key(:geometry) }
|
it { expect(geo_area.errors).to have_key(:geometry) }
|
||||||
end
|
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
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue