diff --git a/Gemfile b/Gemfile index 6afd66925..db2a7c5fd 100644 --- a/Gemfile +++ b/Gemfile @@ -29,6 +29,7 @@ gem 'flipper-active_record' gem 'flipper-ui' gem 'fog-openstack' gem 'font-awesome-rails' +gem 'geocoder' gem 'gon' gem 'graphiql-rails' gem 'graphql' diff --git a/Gemfile.lock b/Gemfile.lock index 0d0c4a12e..c3d75309d 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -232,6 +232,7 @@ GEM font-awesome-rails (4.7.0.4) railties (>= 3.2, < 6.0) formatador (0.2.5) + geocoder (1.6.0) globalid (0.4.2) activesupport (>= 4.2.0) gon (6.2.1) @@ -743,6 +744,7 @@ DEPENDENCIES flipper-ui fog-openstack font-awesome-rails + geocoder gon graphiql-rails graphql diff --git a/app/models/dossier.rb b/app/models/dossier.rb index 96d2f08c2..a7c237bbc 100644 --- a/app/models/dossier.rb +++ b/app/models/dossier.rb @@ -343,7 +343,7 @@ class Dossier < ApplicationRecord def geo_position if etablissement.present? - point = ApiAdresse::PointAdapter.new(etablissement.geo_adresse).geocode + point = Geocoder.search(etablissement.geo_adresse).first end lon = "2.428462" @@ -351,8 +351,7 @@ class Dossier < ApplicationRecord zoom = "13" if point.present? - lon = point.x.to_s - lat = point.y.to_s + lat, lon = point.coordinates.map(&:to_s) end { lon: lon, lat: lat, zoom: zoom } diff --git a/config/initializers/geocoder.rb b/config/initializers/geocoder.rb new file mode 100644 index 000000000..972505d47 --- /dev/null +++ b/config/initializers/geocoder.rb @@ -0,0 +1 @@ +Geocoder.configure(lookup: :ban_data_gouv_fr) diff --git a/spec/models/dossier_spec.rb b/spec/models/dossier_spec.rb index 5167b274e..d1ae1d8ca 100644 --- a/spec/models/dossier_spec.rb +++ b/spec/models/dossier_spec.rb @@ -1084,4 +1084,39 @@ describe Dossier do expect { expired_brouillon.reload }.to raise_error(ActiveRecord::RecordNotFound) end end + + describe '#geo_position' do + let(:lat) { "46.538192" } + let(:lon) { "2.428462" } + let(:zoom) { "13" } + + let(:etablissement_geo_adresse_lat) { "40.7143528" } + let(:etablissement_geo_adresse_lon) { "-74.0059731" } + + let(:result) { { lat: lat, lon: lon, zoom: zoom } } + let(:dossier) { create(:dossier) } + + it 'should geolocate' do + expect(dossier.geo_position).to eq(result) + end + + context 'with etablissement' do + before do + Geocoder::Lookup::Test.add_stub( + dossier.etablissement.geo_adresse, [ + { + 'coordinates' => [etablissement_geo_adresse_lat.to_f, etablissement_geo_adresse_lon.to_f] + } + ] + ) + end + + let(:dossier) { create(:dossier, :with_entreprise) } + let(:result) { { lat: etablissement_geo_adresse_lat, lon: etablissement_geo_adresse_lon, zoom: zoom } } + + it 'should geolocate' do + expect(dossier.geo_position).to eq(result) + end + end + end end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 538b95806..f5e6de454 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -152,6 +152,8 @@ RSpec.configure do |config| ActionMailer::Base.deliveries.clear ActiveStorage::Current.host = 'http://test.host' + + Geocoder.configure(lookup: :test) } RSpec::Matchers.define :have_same_attributes_as do |expected, options|