Class: GeocodingValidator

Inherits:
ActiveModel::EachValidator
  • Object
show all
Defined in:
decidim-core/app/validators/geocoding_validator.rb

Overview

This validator takes care of ensuring the validated content is an existing address and computes its coordinates.

Instance Method Summary collapse

Instance Method Details

#validate_each(record, attribute, value) ⇒ Object


6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'decidim-core/app/validators/geocoding_validator.rb', line 6

def validate_each(record, attribute, value)
  if Decidim::Map.available?(:geocoding) && record.component.present?
    geocoder = geocoder_for(record.component.organization)
    coordinates = geocoder.coordinates(value)

    if coordinates.present?
      record.latitude = coordinates.first
      record.longitude = coordinates.last
    else
      record.errors.add(attribute, :invalid)
    end
  else
    record.errors.add(attribute, :invalid)
  end
end