diff --git a/app/dashboards/service_dashboard.rb b/app/dashboards/service_dashboard.rb index 5e3ea65fe..50f03179f 100644 --- a/app/dashboards/service_dashboard.rb +++ b/app/dashboards/service_dashboard.rb @@ -19,7 +19,9 @@ class ServiceDashboard < Administrate::BaseDashboard telephone: Field::String, horaires: Field::String, adresse: Field::String, - siret: Field::String + siret: Field::String, + etablissement_adresse: Field::String, + etablissement_latlng: GeopointField }.freeze # COLLECTION_ATTRIBUTES @@ -46,7 +48,9 @@ class ServiceDashboard < Administrate::BaseDashboard :telephone, :horaires, :adresse, - :siret + :siret, + :etablissement_adresse, + :etablissement_latlng ].freeze # FORM_ATTRIBUTES diff --git a/app/fields/geopoint_field.rb b/app/fields/geopoint_field.rb new file mode 100644 index 000000000..08a98bfae --- /dev/null +++ b/app/fields/geopoint_field.rb @@ -0,0 +1,11 @@ +require "administrate/field/base" + +class GeopointField < Administrate::Field::Base + def lat + data.first + end + + def lng + data.last + end +end diff --git a/app/models/service.rb b/app/models/service.rb index c8a803944..8ec49544f 100644 --- a/app/models/service.rb +++ b/app/models/service.rb @@ -63,6 +63,10 @@ class Service < ApplicationRecord etablissement_infos.fetch("adresse", nil) end + def etablissement_latlng + [etablissement_lat, etablissement_lng] + end + private def enqueue_api_entreprise diff --git a/app/views/fields/geopoint_field/_show.html.haml b/app/views/fields/geopoint_field/_show.html.haml new file mode 100644 index 000000000..cd714588d --- /dev/null +++ b/app/views/fields/geopoint_field/_show.html.haml @@ -0,0 +1,2 @@ +- if field.data.present? + = link_to field.data.join(", "), "https://www.geoportail.gouv.fr/carte?c=#{field.lng},#{field.lat}&z=17&permalink=yes", target: '_blank', rel: 'noopener' diff --git a/spec/models/service_spec.rb b/spec/models/service_spec.rb index 3277de1f2..2aa403bdc 100644 --- a/spec/models/service_spec.rb +++ b/spec/models/service_spec.rb @@ -136,4 +136,16 @@ describe Service, type: :model do end end end + + describe 'etablissement_latlng' do + it 'without coordinates' do + service = build(:service, etablissement_lat: nil, etablissement_lng: nil) + expect(service.etablissement_latlng).to eq([nil, nil]) + end + + it 'with coordinates' do + service = build(:service) + expect(service.etablissement_latlng).to eq([48.87, 2.34]) + end + end end