From ba350476c912a64320ea6e22a632950046122c21 Mon Sep 17 00:00:00 2001 From: Jeffrey Warren Date: Tue, 21 Apr 2020 16:55:18 -0400 Subject: [PATCH 1/2] Fixing origin validation Re #1264 --- app/models/map.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/models/map.rb b/app/models/map.rb index a5ab25f99..1a924d498 100755 --- a/app/models/map.rb +++ b/app/models/map.rb @@ -13,8 +13,8 @@ class Map < ApplicationRecord # :message => " must not include spaces and must be alphanumeric, as it'll be used in the URL of your map, like: https://mapknitter.org/maps/your-map-name. You may use dashes and underscores.", # :on => :create # validates_format_of :tile_url, :with => /^(http|https):\/\/[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(:[0-9]{1,5})?(\/.*)?$/ix - validates_with NotAtOriginValidator, unless: Proc.new { |map| map.created_at < DateTime.new(2015, 1, 13, 0, 0, 0) } - validates :lat, :lon, NotAtOrigin: true, unless: Proc.new { |map| map.created_at < DateTime.new(2015, 1, 13, 0, 0, 0) } + validates_with NotAtOriginValidator + validates :lat, :lon, NotAtOrigin: true has_many :exports, dependent: :destroy has_many :tags, dependent: :destroy From 9558b8fffd0b7d5bb73145fd7c1f1ccc870a414e Mon Sep 17 00:00:00 2001 From: Jeffrey Warren Date: Tue, 21 Apr 2020 16:57:10 -0400 Subject: [PATCH 2/2] Update not_at_origin_validator.rb --- lib/not_at_origin_validator.rb | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/lib/not_at_origin_validator.rb b/lib/not_at_origin_validator.rb index 670526cb5..4697041d7 100644 --- a/lib/not_at_origin_validator.rb +++ b/lib/not_at_origin_validator.rb @@ -2,11 +2,14 @@ # custom validator used in map.rb class NotAtOriginValidator < ActiveModel::Validator - def validate(rec) - rec.errors[:base] << '0,0 is an unlikely locale.' if null_island?(rec) + def validate(record) + # adding exception to maps created before this validation was added: https://github.com/publiclab/mapknitter/issues/1264 + if null_island?(record) && record.created_at > DateTime.new(2015, 1, 13, 0, 0, 0) + record.errors[:base] << '0,0 is an unlikely locale.' + end end - def null_island?(rec) - rec.lat.zero? || rec.lon.zero? + def null_island?(record) + record.lat.zero? || record.lon.zero? end end