|
@ -64,18 +64,16 @@ class City < ActiveRecord::Base |
|
|
# see if the city is already present in our database |
|
|
# see if the city is already present in our database |
|
|
city = City.find_by_place_id(location.data['place_id']) |
|
|
city = City.find_by_place_id(location.data['place_id']) |
|
|
|
|
|
|
|
|
# return the city if we found it in the db already |
|
|
# if we didn't find a match by place id, collect the city, territory, and country from the result |
|
|
if city.present? |
|
|
unless city.present? |
|
|
CityCache.cache(str, city.id) |
|
|
# google nsames things differently than we do, we'll look for these itesm |
|
|
return city |
|
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
|
|
# otherwise build a new city |
|
|
|
|
|
component_alises = { |
|
|
component_alises = { |
|
|
'locality' => :city, |
|
|
'locality' => :city, |
|
|
'administrative_area_level_1' => :territory, |
|
|
'administrative_area_level_1' => :territory, |
|
|
'country' => :country |
|
|
'country' => :country |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
# and populate this map to eventually create the city if we need to |
|
|
city_data = { |
|
|
city_data = { |
|
|
locale: :en, |
|
|
locale: :en, |
|
|
latitude: location.data['geometry']['location']['lat'], |
|
|
latitude: location.data['geometry']['location']['lat'], |
|
@ -130,9 +128,26 @@ class City < ActiveRecord::Base |
|
|
# we need to have the city and country at least |
|
|
# we need to have the city and country at least |
|
|
return false unless city_data[:city].present? && city_data[:country].present? |
|
|
return false unless city_data[:city].present? && city_data[:country].present? |
|
|
|
|
|
|
|
|
# save the new city |
|
|
# one last attempt to make sure we don't already have a record of this city |
|
|
|
|
|
city = City.where(city: city_data[:city], territory: city_data[:territory], country: city_data[:country]).first |
|
|
|
|
|
|
|
|
|
|
|
# only if we still can't find the city, then save it as a new one |
|
|
|
|
|
unless city.present? |
|
|
city = City.new(city_data) |
|
|
city = City.new(city_data) |
|
|
|
|
|
# if we found exactly what we were looking for, keep these location details |
|
|
|
|
|
# otherwise we may have searched for 'The Bronx' and set the sity the 'New York' but these details will be about The Bronx |
|
|
|
|
|
# so if we try to show New York on a map it will always point to The Bronx, not very fair to those from Staten Island |
|
|
|
|
|
unless city_data[:city] == searched_component |
|
|
|
|
|
new_location = Geocoder.search(str, language: 'en').first |
|
|
|
|
|
city.latitude = new_location.data['geometry']['location']['lat'] |
|
|
|
|
|
city.longitude = new_location.data['geometry']['location']['lng'] |
|
|
|
|
|
city.place_id = new_location.data['place_id'] |
|
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
|
|
# and create the new city |
|
|
city.save! |
|
|
city.save! |
|
|
|
|
|
end |
|
|
|
|
|
end |
|
|
|
|
|
|
|
|
# save this to our cache |
|
|
# save this to our cache |
|
|
CityCache.cache(str, city.id) |
|
|
CityCache.cache(str, city.id) |
|
|