class Geocode

Public Class Methods

create_by_query(query) click to toggle source
# File lib/acts_as_geocodable/geocode.rb, line 22
def self.create_by_query(query)
  create geocoder.locate(query).attributes.merge(query: query)
end
create_from_location(location) click to toggle source
# File lib/acts_as_geocodable/geocode.rb, line 30
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
find_or_create_by_location(location) click to toggle source
# File lib/acts_as_geocodable/geocode.rb, line 26
def self.find_or_create_by_location(location)
  find_by_query(location.to_s) || create_from_location(location)
end
find_or_create_by_query(query) click to toggle source
# File lib/acts_as_geocodable/geocode.rb, line 18
def self.find_or_create_by_query(query)
  find_by_query(query) || create_by_query(query)
end

Public Instance Methods

coordinates() click to toggle source
# File lib/acts_as_geocodable/geocode.rb, line 53
def coordinates
  "#{longitude},#{latitude}"
end
distance_to(destination, units = :miles, formula = :haversine) click to toggle source
# File lib/acts_as_geocodable/geocode.rb, line 8
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
geocoded() click to toggle source
# File lib/acts_as_geocodable/geocode.rb, line 45
def geocoded
  @geocoded ||= geocodings.collect { |geocoding| geocoding.geocodable }
end
geocoded?() click to toggle source
# File lib/acts_as_geocodable/geocode.rb, line 14
def geocoded?
  !latitude.blank? && !longitude.blank?
end
on(geocodable) click to toggle source
# File lib/acts_as_geocodable/geocode.rb, line 49
def on(geocodable)
  geocodings.create(geocodable: geocodable)
end
precision() click to toggle source
# File lib/acts_as_geocodable/geocode.rb, line 37
def precision
  Graticule::Precision.new(self[:precision])
end
precision=(name) click to toggle source
# File lib/acts_as_geocodable/geocode.rb, line 41
def precision=(name)
  self[:precision] = name.to_s
end
to_location() click to toggle source

Create a Graticule::Location

# File lib/acts_as_geocodable/geocode.rb, line 62
def to_location
  Graticule::Location.new(attributes.except("id", "query"))
end
to_s() click to toggle source
# File lib/acts_as_geocodable/geocode.rb, line 57
def to_s
  coordinates
end