Browse Source

Fixed city caching

development
Godwin 8 years ago
parent
commit
185c521f15
  1. 2
      Rakefile
  2. 6
      app/models/city.rb
  3. 4
      app/models/city_cache.rb

2
Rakefile

@ -37,8 +37,8 @@ task update_cities: :environment do
end end
end end
unless c.place_id.present?
City.all.each do |c| City.all.each do |c|
unless c.place_id.present?
location = Geocoder.search(c.address, language: 'en').first location = Geocoder.search(c.address, language: 'en').first
c.place_id = location.data['place_id'] c.place_id = location.data['place_id']
c.save! c.save!

6
app/models/city.rb

@ -66,7 +66,7 @@ class City < ActiveRecord::Base
# return the city if we found it in the db already # return the city if we found it in the db already
if city.present? if city.present?
CityCache.create(city_id: city.id, search: str) CityCache.cache(str, city.id)
return city return city
end end
@ -119,7 +119,7 @@ class City < ActiveRecord::Base
# and not an address or country # and not an address or country
# some places are not labeled 'locality', search for 'Halifax NS' for example and you will # some places are not labeled 'locality', search for 'Halifax NS' for example and you will
# get 'administrative_area_level_2' since Halifax is a municipality # get 'administrative_area_level_2' since Halifax is a municipality
if component['types'] == location.data['types'] && not_a_city.include?(component['types'].first) if component['types'] == location.data['types'] && !not_a_city.include?(component['types'].first)
searched_component = component['short_name'] searched_component = component['short_name']
end end
end end
@ -135,7 +135,7 @@ class City < ActiveRecord::Base
city.save! city.save!
# save this to our cache # save this to our cache
CityCache.create(city_id: city.id, search: str) CityCache.cache(str, city.id)
# and return it # and return it
return city return city

4
app/models/city_cache.rb

@ -6,4 +6,8 @@ class CityCache < ActiveRecord::Base
def self.search(str) def self.search(str)
CityCache.find_by_search(str.downcase) CityCache.find_by_search(str.downcase)
end end
def self.cache(str, city_id)
CityCache.create(city_id: city_id, search: str.downcase)
end
end end

Loading…
Cancel
Save