Sha256: 5e3c1c6857f24e55f5756546a535f5c157c8f5807d43cf146f593baad1bedb28

Contents?: true

Size: 766 Bytes

Versions: 30

Compression:

Stored size: 766 Bytes

Contents

# frozen_string_literal: true

# This validator takes care of ensuring the validated content is
# an existing address and computes its coordinates.
class GeocodingValidator < ActiveModel::EachValidator
  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
end

Version data entries

30 entries across 30 versions & 1 rubygems

Version Path
decidim-core-0.22.0 app/validators/geocoding_validator.rb
decidim-core-0.21.0 app/validators/geocoding_validator.rb
decidim-core-0.20.1 app/validators/geocoding_validator.rb
decidim-core-0.20.0 app/validators/geocoding_validator.rb
decidim-core-0.19.1 app/validators/geocoding_validator.rb
decidim-core-0.18.1 app/validators/geocoding_validator.rb
decidim-core-0.19.0 app/validators/geocoding_validator.rb
decidim-core-0.17.2 app/validators/geocoding_validator.rb
decidim-core-0.18.0 app/validators/geocoding_validator.rb
decidim-core-0.17.1 app/validators/geocoding_validator.rb
decidim-core-0.16.1 app/validators/geocoding_validator.rb
decidim-core-0.17.0 app/validators/geocoding_validator.rb
decidim-core-0.16.0 app/validators/geocoding_validator.rb
decidim-core-0.15.2 app/validators/geocoding_validator.rb
decidim-core-0.15.1 app/validators/geocoding_validator.rb
decidim-core-0.15.0 app/validators/geocoding_validator.rb
decidim-core-0.14.4 app/validators/geocoding_validator.rb
decidim-core-0.14.3 app/validators/geocoding_validator.rb
decidim-core-0.14.2 app/validators/geocoding_validator.rb
decidim-core-0.14.1 app/validators/geocoding_validator.rb