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
unless c.place_id.present?
City.all.each do |c|
unless c.place_id.present?
location = Geocoder.search(c.address, language: 'en').first
c.place_id = location.data['place_id']
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
if city.present?
CityCache.create(city_id: city.id, search: str)
CityCache.cache(str, city.id)
return city
end
@ -119,7 +119,7 @@ class City < ActiveRecord::Base
# and not an address or country
# 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
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']
end
end
@ -135,7 +135,7 @@ class City < ActiveRecord::Base
city.save!
# save this to our cache
CityCache.create(city_id: city.id, search: str)
CityCache.cache(str, city.id)
# and return it
return city

4
app/models/city_cache.rb

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

Loading…
Cancel
Save