mirror of
https://github.com/Freika/dawarich.git
synced 2026-01-11 09:41:40 -05:00
35 lines
948 B
Ruby
35 lines
948 B
Ruby
# 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
|
|
@place.name = properties['name'] if properties['name'].present?
|
|
@place.city = properties['city'] if properties['city'].present?
|
|
@place.country = properties['country'] if properties['country'].present?
|
|
@place.geodata = geodata.data if DawarichSettings.store_geodata?
|
|
@place.save!
|
|
|
|
if properties['name'].present?
|
|
@place
|
|
.visits
|
|
.where(name: Place::DEFAULT_NAME)
|
|
.update_all(name: properties['name'])
|
|
end
|
|
|
|
@place
|
|
end
|
|
end
|
|
end
|
|
end
|