Sha256: 813a0e857a3c6dfa5f9764f65418bf3a45b53bc6e1a3336ee177a8314638c528

Contents?: true

Size: 1.46 KB

Versions: 2

Compression:

Stored size: 1.46 KB

Contents

class Geocode < ActiveRecord::Base
  has_many :geocodings, dependent: :destroy

  validates_uniqueness_of :query

  cattr_accessor :geocoder

  def distance_to(destination, units = :miles, formula = :haversine)
    if destination && destination.latitude && destination.longitude
      Graticule::Distance.const_get(formula.to_s.camelize).distance(self, destination, units)
    end
  end

  def geocoded?
    !latitude.blank? && !longitude.blank?
  end

  def self.find_or_create_by_query(query)
    find_by_query(query) || create_by_query(query)
  end

  def self.create_by_query(query)
    create geocoder.locate(query).attributes.merge(query: query)
  end

  def self.find_or_create_by_location(location)
    find_by_query(location.to_s) || create_from_location(location)
  end

  def self.create_from_location(location)
    create geocoder.locate(location).attributes.merge(query: location.to_s)
  rescue Graticule::Error => error
    logger.warn error.message
    nil
  end

  def precision
    Graticule::Precision.new(self[:precision])
  end

  def precision=(name)
    self[:precision] = name.to_s
  end

  def geocoded
    @geocoded ||= geocodings.collect { |geocoding| geocoding.geocodable }
  end

  def on(geocodable)
    geocodings.create(geocodable: geocodable)
  end

  def coordinates
    "#{longitude},#{latitude}"
  end

  def to_s
    coordinates
  end

  # Create a Graticule::Location
  def to_location
    Graticule::Location.new(attributes.except("id", "query"))
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
acts_as_geocodable-2.1.1 lib/acts_as_geocodable/geocode.rb
acts_as_geocodable-2.1.0 lib/acts_as_geocodable/geocode.rb