Class: Geocode

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
lib/acts_as_geocodable/geocode.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.create_by_query(query) ⇒ Object



25
26
27
# File 'lib/acts_as_geocodable/geocode.rb', line 25

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

.create_from_location(location) ⇒ Object



33
34
35
36
37
38
# File 'lib/acts_as_geocodable/geocode.rb', line 33

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

.find_or_create_by_location(location) ⇒ Object



29
30
31
# File 'lib/acts_as_geocodable/geocode.rb', line 29

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) ⇒ Object



21
22
23
# File 'lib/acts_as_geocodable/geocode.rb', line 21

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

Instance Method Details

#coordinatesObject



56
57
58
# File 'lib/acts_as_geocodable/geocode.rb', line 56

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

#distance_to(destination, units = :miles, formula = :haversine) ⇒ Object



11
12
13
14
15
# File 'lib/acts_as_geocodable/geocode.rb', line 11

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

#geocodedObject



48
49
50
# File 'lib/acts_as_geocodable/geocode.rb', line 48

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

#geocoded?Boolean

Returns:

  • (Boolean)


17
18
19
# File 'lib/acts_as_geocodable/geocode.rb', line 17

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

#on(geocodable) ⇒ Object



52
53
54
# File 'lib/acts_as_geocodable/geocode.rb', line 52

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

#precisionObject



40
41
42
# File 'lib/acts_as_geocodable/geocode.rb', line 40

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

#precision=(name) ⇒ Object



44
45
46
# File 'lib/acts_as_geocodable/geocode.rb', line 44

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

#to_locationObject

Create a Graticule::Location



65
66
67
# File 'lib/acts_as_geocodable/geocode.rb', line 65

def to_location
  Graticule::Location.new(attributes.except('id', 'query'))
end

#to_sObject



60
61
62
# File 'lib/acts_as_geocodable/geocode.rb', line 60

def to_s
  coordinates
end