Sha256: c283d644ec905ab593ea68b30aeadbf380e68edef022044531f246036b44d1a1

Contents?: true

Size: 1.24 KB

Versions: 1

Compression:

Stored size: 1.24 KB

Contents

module MainStreet
  module Model
    def validates_address(fields:, geocode: false, country: nil)
      fields = Array(fields.map(&:to_s))
      geocode_options = {latitude: :latitude, longitude: :longitude}
      geocode_options = geocode_options.merge(geocode) if geocode.is_a?(Hash)

      class_eval do
        validate :verify_address, if: -> { fields.any? { |f| changes.key?(f.to_s) } }

        define_method :verify_address do
          address = fields.map { |v| send(v).presence }.compact.join(", ")

          if address.present?
            # must use a different variable than country
            record_country = instance_exec(&country) if country.respond_to?(:call)
            verifier = MainStreet::AddressVerifier.new(address, country: record_country)
            if verifier.success?
              if geocode
                self.send("#{geocode_options[:latitude]}=", verifier.latitude)
                self.send("#{geocode_options[:longitude]}=", verifier.longitude)
              end
            else
              errors.add(:base, verifier.failure_message)
            end

            # legacy - for standardize_address method
            @address_verification_result = verifier.result
          end
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
mainstreet-0.2.0 lib/mainstreet/model.rb