Class: GeoLocation

Inherits:
ActiveRecord::Base show all
Defined in:
app/models/geo_location.rb

Instance Method Summary collapse

Methods inherited from ActiveRecord::Base

#readonly?

Instance Method Details

#geocodeObject

This method performs the geociding query and stores the query datetime.



30
31
32
33
# File 'app/models/geo_location.rb', line 30

def geocode
  self.queried_at = DateTime.now
  super
end

#geocode_and_saveObject

Perform geocode query and save the record. This is needed after finding a record where no geocode query has been done, yet.



38
39
40
41
# File 'app/models/geo_location.rb', line 38

def geocode_and_save
  geocode
  save
end

#geocoded?Boolean

This method returns ‘true` if the geocoding query has been performed, already.

After a query, the attributes may still be empty, since this might be an invalid address. Therefore, it’s good to know, whether the query has been done already.

Returns:

  • (Boolean)


49
50
51
# File 'app/models/geo_location.rb', line 49

def geocoded?
  (queried_at.present? || country_code.present?)
end

#in_europe?Boolean

This method returns true if the location is in Europe.

Returns:

  • (Boolean)


73
74
75
76
# File 'app/models/geo_location.rb', line 73

def in_europe?
  country_code.in?(%w(AD AL AT BA BE BG BY CH CY CZ DE DK EE ES FI FO FR GG GI GR GB HR HU IE IM IS IT JE LI LT LU LV MC MD\
 MK MT NL NO PL PT RO RU SE SI SJ SK SM TR UA UK VA YU))
end

#plzObject

The following method is a country-specific accessor to the the German Postleitzahl (PLZ). If returns the German postal code if the address is in Germany. Otherwise, it returns nil.

There are cases when the maps api can’t isolate the postal code. In this case, the ‘postal_code` returns `nil`. Try to get it from the regex then.



86
87
88
89
90
# File 'app/models/geo_location.rb', line 86

def plz
  if country_code == "DE"
    postal_code || address.match(/(\d{5})/).try(:[], 1)
  end
end