Class: GeocodingValidator
- Inherits:
-
ActiveModel::EachValidator
- Object
- ActiveModel::EachValidator
- GeocodingValidator
- Defined in:
- 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 21 |
# File 'app/validators/geocoding_validator.rb', line 6 def validate_each(record, attribute, value) if Decidim.geocoder.present? && record.component.present? organization = record.component.organization Geocoder.configure(Geocoder.config.merge(http_headers: { "Referer" => organization.host })) 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 |