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



22
23
24
# 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) ⇒ Object



30
31
32
33
34
35
# 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) ⇒ Object



26
27
28
# 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) ⇒ Object



18
19
20
# 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

Instance Method Details

#coordinatesObject



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

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

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



8
9
10
11
12
# 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

#geocodedObject



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

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

#geocoded?Boolean

Returns:

  • (Boolean)


14
15
16
# File 'lib/acts_as_geocodable/geocode.rb', line 14

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

#on(geocodable) ⇒ Object



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

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

#precisionObject



37
38
39
# File 'lib/acts_as_geocodable/geocode.rb', line 37

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

#precision=(name) ⇒ Object



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

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

#to_locationObject

Create a Graticule::Location



62
63
64
# File 'lib/acts_as_geocodable/geocode.rb', line 62

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

#to_sObject



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

def to_s
  coordinates
end