module Geokit module Cached module Geocodable @@cache_locations = true @@default_country = 'Poland' def self.included(base) base.extend(ClassMethods) base.class_eval do attr_accessor :provider end end def selected_country country || @@default_country end def complete_address "%s, %s" % [address, selected_country] end def cached_location_for_address CachedLocation.find_by_address(complete_address) end def cached_location_for_address? !cached_location_for_address.nil? end def cached_location cached_location_for_address || CachedLocation.new(:address => complete_address) end def cache_locations? @@cache_locations end def cache_location! cached_location.cache!(:lat => lat, :lng => lng, :provider => provider) if cache_locations? end def geocode_address_cached if cached_location_for_address? self.lat, self.lng, self.provider = cached_location.lat, cached_location.lng, cached_location.provider else @geo = Geokit::Geocoders::MultiGeocoder.geocode(complete_address) if @geo.success self.lat, self.lng, self.provider = @geo.lat, @geo.lng, @geo.provider cache_location! if cache_locations? end end end module ClassMethods def cache_locations(value) @@cache_locations = value end def default_country(value) @@default_country = value end end end end end