module ActsAsGeocodable::Model::ClassMethods
Public Instance Methods
farthest()
click to toggle source
Find the farthest location to the given origin
Model.origin("Grand Rapids, MI").farthest
# File lib/acts_as_geocodable.rb, line 126 def farthest far.first end
location_to_geocode(location)
click to toggle source
Convert the given location to a Geocode
# File lib/acts_as_geocodable.rb, line 131 def location_to_geocode(location) case location when Geocode then location when ActsAsGeocodable::Model then location.geocode when String, Fixnum then Geocode.find_or_create_by_query(location.to_s) end end
nearest()
click to toggle source
Find the nearest location to the given origin
Model.origin("Grand Rapids, MI").nearest
# File lib/acts_as_geocodable.rb, line 118 def nearest near.first end
validates_as_geocodable(options = {}) { |geocode| ... }
click to toggle source
Validate that the model can be geocoded
Options:
-
:message
: Added to errors base (Default: Address could not be geocoded.) -
:allow_nil
: If all the address attributes are blank, then don't try to validate the geocode (Default: false) -
:precision
: Require a minimum geocoding precision
validates_as_geocodable
also takes a block that you can use to performa additional checks on the geocode. If this block returns false, then validation will fail.
validates_as_geocodable do |geocode| geocode.country == "US" end
# File lib/acts_as_geocodable.rb, line 154 def validates_as_geocodable(options = {}) options = options.reverse_merge message: "Address could not be geocoded.", allow_nil: false validate do |model| is_blank = model.to_location.attributes.except(:precision).all?(&:blank?) unless options[:allow_nil] && is_blank geocode = model.send(:attach_geocode) if !geocode || (options[:precision] && geocode.precision < options[:precision]) || (block_given? && yield(geocode) == false) model.errors.add(:base, options[:message]) end end end end
Private Instance Methods
sql_for_distance(origin, units = acts_as_geocodable_options[:units])
click to toggle source
# File lib/acts_as_geocodable.rb, line 171 def sql_for_distance(origin, units = acts_as_geocodable_options[:units]) Graticule::Distance::Spherical.to_sql( latitude: origin.latitude, longitude: origin.longitude, latitude_column: "geocodes.latitude", longitude_column: "geocodes.longitude", units: units ) end