diff --git a/app/controllers/api/v1/places_controller.rb b/app/controllers/api/v1/places_controller.rb index 805b40e1..f4a0407a 100644 --- a/app/controllers/api/v1/places_controller.rb +++ b/app/controllers/api/v1/places_controller.rb @@ -70,9 +70,9 @@ module Api end def tag_ids - ids = params.dig(:place, :tag_ids) - Array(ids).compact - end + ids = params.dig(:place, :tag_ids) + Array(ids).compact + end def add_tags return if tag_ids.empty? diff --git a/app/services/reverse_geocoding/places/fetch_data.rb b/app/services/reverse_geocoding/places/fetch_data.rb index 240ac6de..75162aa5 100644 --- a/app/services/reverse_geocoding/places/fetch_data.rb +++ b/app/services/reverse_geocoding/places/fetch_data.rb @@ -15,7 +15,7 @@ class ReverseGeocoding::Places::FetchData return end - places = reverse_geocoded_places + places = geocoder_places first_place = places.shift update_place(first_place) @@ -82,6 +82,7 @@ class ReverseGeocoding::Places::FetchData def find_existing_places(osm_ids) Place.where("geodata->'properties'->>'osm_id' IN (?)", osm_ids) + .where(user_id: nil) .index_by { |p| p.geodata.dig('properties', 'osm_id').to_s } .compact end @@ -145,7 +146,7 @@ class ReverseGeocoding::Places::FetchData "POINT(#{coordinates[0]} #{coordinates[1]})" end - def reverse_geocoded_places + def geocoder_places data = Geocoder.search( [place.lat, place.lon], limit: 10, diff --git a/spec/factories/places.rb b/spec/factories/places.rb index 303f1f48..b4b8c3b9 100644 --- a/spec/factories/places.rb +++ b/spec/factories/places.rb @@ -6,7 +6,7 @@ FactoryBot.define do latitude { 54.2905245 } longitude { 13.0948638 } # lonlat is auto-generated by before_validation callback in Place model - association :user + # association :user trait :with_geodata do geodata do diff --git a/spec/services/reverse_geocoding/places/fetch_data_spec.rb b/spec/services/reverse_geocoding/places/fetch_data_spec.rb index 399b31c0..976b68c9 100644 --- a/spec/services/reverse_geocoding/places/fetch_data_spec.rb +++ b/spec/services/reverse_geocoding/places/fetch_data_spec.rb @@ -98,7 +98,7 @@ RSpec.describe ReverseGeocoding::Places::FetchData do it 'updates the original place and creates others' do service.call - created_place = Place.where.not(id: place.id).first + created_place = Place.where(user_id: nil).where.not(id: place.id).first expect(created_place.name).to include('Second Place') expect(created_place.city).to eq('Hamburg') end @@ -584,15 +584,15 @@ RSpec.describe ReverseGeocoding::Places::FetchData do place # Force place creation expect { service.call }.to change { Place.count }.by(1) - created_place = Place.where.not(id: place.id).first + created_place = Place.where(user_id: nil).where.not(id: place.id).first expect(created_place.latitude).to eq(54.0) expect(created_place.longitude).to eq(13.0) end end context 'when lonlat is already present on existing place' do - let!(:existing_place) { create(:place, :with_geodata, lonlat: 'POINT(10.0 50.0)') } - let(:existing_data) do + let!(:existing_place) { create(:place, :with_geodata, lonlat: 'POINT(10.0 50.0)', latitude: 50.0, longitude: 10.0) } + let(:mock_data) do double( data: { 'geometry' => { 'coordinates' => [15.0, 55.0] }, @@ -605,7 +605,7 @@ RSpec.describe ReverseGeocoding::Places::FetchData do end before do - allow(Geocoder).to receive(:search).and_return([mock_geocoded_place, existing_data]) + allow(Geocoder).to receive(:search).and_return([mock_geocoded_place, mock_data]) end it 'does not override existing coordinates when updating geodata' do