2025-07-12 05:21:38 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
|
|
module Places
|
|
|
|
|
class NameFetcher
|
|
|
|
|
def initialize(place)
|
|
|
|
|
@place = place
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def call
|
|
|
|
|
geodata = Geocoder.search([@place.lat, @place.lon], units: :km, limit: 1, distance_sort: true).first
|
|
|
|
|
|
|
|
|
|
return if geodata.blank?
|
|
|
|
|
|
|
|
|
|
properties = geodata.data&.dig('properties')
|
|
|
|
|
return if properties.blank?
|
|
|
|
|
|
|
|
|
|
ActiveRecord::Base.transaction do
|
2025-07-12 11:57:22 -04:00
|
|
|
@place.name = properties['name'] if properties['name'].present?
|
|
|
|
|
@place.city = properties['city'] if properties['city'].present?
|
|
|
|
|
@place.country = properties['country'] if properties['country'].present?
|
2025-07-12 05:21:38 -04:00
|
|
|
@place.geodata = geodata.data if DawarichSettings.store_geodata?
|
|
|
|
|
@place.save!
|
|
|
|
|
|
2025-07-12 11:57:22 -04:00
|
|
|
if properties['name'].present?
|
|
|
|
|
@place
|
|
|
|
|
.visits
|
|
|
|
|
.where(name: Place::DEFAULT_NAME)
|
|
|
|
|
.update_all(name: properties['name'])
|
|
|
|
|
end
|
2025-07-12 05:21:38 -04:00
|
|
|
|
|
|
|
|
@place
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|