Module: ActsAsGeocodable::Model::ClassMethods
- Defined in:
- lib/acts_as_geocodable.rb
Instance Method Summary collapse
-
#farthest ⇒ Object
Find the farthest location to the given origin.
-
#location_to_geocode(location) ⇒ Object
Convert the given location to a Geocode.
-
#nearest ⇒ Object
Find the nearest location to the given origin.
-
#validates_as_geocodable(options = {}) ⇒ Object
Validate that the model can be geocoded.
Instance Method Details
#farthest ⇒ Object
Find the farthest location to the given origin
Model.origin("Grand Rapids, MI").farthest
126 127 128 |
# File 'lib/acts_as_geocodable.rb', line 126 def farthest far.first end |
#location_to_geocode(location) ⇒ Object
Convert the given location to a Geocode
131 132 133 134 135 136 137 |
# 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 ⇒ Object
Find the nearest location to the given origin
Model.origin("Grand Rapids, MI").nearest
118 119 120 |
# File 'lib/acts_as_geocodable.rb', line 118 def nearest near.first end |
#validates_as_geocodable(options = {}) ⇒ Object
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
154 155 156 157 158 159 160 161 162 163 164 165 166 167 |
# File 'lib/acts_as_geocodable.rb', line 154 def validates_as_geocodable( = {}) = .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 [:allow_nil] && is_blank geocode = model.send(:attach_geocode) if !geocode || ([:precision] && geocode.precision < [:precision]) || (block_given? && yield(geocode) == false) model.errors.add(:base, [:message]) end end end end |