diff --git a/Gemfile b/Gemfile index 6544368ae1cbc82e64c964c03c59957972989b29..612a5bc28ebb7f29a3513dd5722e0d3d73a96c4a 100644 --- a/Gemfile +++ b/Gemfile @@ -26,6 +26,7 @@ gem "faceted_search"#, path: "../../noesya/faceted_search" gem "font-awesome-sass" gem "front_matter_parser" gem "gdpr" +gem "geocoder", "~> 1.8" gem "gitlab" gem "hal_openscience", "~> 0.1" # gem "hal_openscience", path: "../hal_openscience" diff --git a/Gemfile.lock b/Gemfile.lock index 44a13bb69dabe3c3846a04ba34b22a3f11203de7..c6747c13781548393616547c5d55a2288f0e2c64 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -221,6 +221,7 @@ GEM js_cookie_rails rails sassc-rails + geocoder (1.8.1) gitlab (4.19.0) httparty (~> 0.20) terminal-table (>= 1.5.1) @@ -559,6 +560,7 @@ DEPENDENCIES font-awesome-sass front_matter_parser gdpr + geocoder (~> 1.8) gitlab hal_openscience (~> 0.1) has_scope (~> 0.8.0) diff --git a/app/models/concerns/with_geolocation.rb b/app/models/concerns/with_geolocation.rb new file mode 100644 index 0000000000000000000000000000000000000000..d233db29ec68b0e415874f6a697f2f012e08dd59 --- /dev/null +++ b/app/models/concerns/with_geolocation.rb @@ -0,0 +1,23 @@ +module WithGeolocation + extend ActiveSupport::Concern + + included do + geocoded_by :full_street_address + + after_validation :geocode, if: -> (geocodable) { geocodable.full_street_address_present? && geocodable.full_street_address_changed? } + end + + def full_street_address + "#{address}, #{zipcode} #{city} #{country}" + end + + protected + + def full_street_address_present? + address.present? || zipcode.present? || city.present? + end + + def full_street_address_changed? + address_changed? || zipcode_changed? || city_changed? + end +end \ No newline at end of file diff --git a/app/models/university/organization.rb b/app/models/university/organization.rb index 6625514d853ed32c412ea3a7a7fb8ce93e42f3ad..1ab9473a5f6d24520608497027ecc5a1c60c6562 100644 --- a/app/models/university/organization.rb +++ b/app/models/university/organization.rb @@ -39,6 +39,7 @@ class University::Organization < ApplicationRecord include Sanitizable include WithBlobs include WithBlocks + include WithGeolocation include WithGit include WithPermalink include WithSlug diff --git a/app/views/admin/university/organizations/static.html.erb b/app/views/admin/university/organizations/static.html.erb index 50fe16931b7282fc64bd02e40628535ac5ee3b8f..d5dfa7df56673c1eeab9a2531df5b28da1df3518 100644 --- a/app/views/admin/university/organizations/static.html.erb +++ b/app/views/admin/university/organizations/static.html.erb @@ -14,11 +14,13 @@ kind: :zipcode, :city, :country, + :latitude, + :longitude, :phone, :email, :twitter, :linkedin -].each do |key| +].each do |key| value = @about.public_send key %> <%= key %>: > diff --git a/config/initializers/geocoder.rb b/config/initializers/geocoder.rb new file mode 100644 index 0000000000000000000000000000000000000000..d8b9f10554128d1336db700b8e4939d4f370c605 --- /dev/null +++ b/config/initializers/geocoder.rb @@ -0,0 +1 @@ +Geocoder.configure(http_headers: { "User-Agent" => ENV["MAIL_FROM_DEFAULT_ADDRESS"] }) \ No newline at end of file diff --git a/db/migrate/20230306153945_add_geolocation_to_university_organizations.rb b/db/migrate/20230306153945_add_geolocation_to_university_organizations.rb new file mode 100644 index 0000000000000000000000000000000000000000..0bf237e647e95283e0f237b0598cc8fd7e19a2a8 --- /dev/null +++ b/db/migrate/20230306153945_add_geolocation_to_university_organizations.rb @@ -0,0 +1,6 @@ +class AddGeolocationToUniversityOrganizations < ActiveRecord::Migration[7.0] + def change + add_column :university_organizations, :latitude, :float + add_column :university_organizations, :longitude, :float + end +end diff --git a/db/schema.rb b/db/schema.rb index 88f02d69126df9bb0d02d10cf9c8b35742f8336a..af39f9a133006f6313cadeba392fd073e159dcc5 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -171,18 +171,6 @@ ActiveRecord::Schema[7.0].define(version: 2023_03_06_134627) do t.index ["communication_website_post_id", "communication_website_category_id"], name: "post_category" end - create_table "communication_website_connections", id: :uuid, default: -> { "gen_random_uuid()" }, force: :cascade do |t| - t.uuid "university_id", null: false - t.uuid "website_id", null: false - t.string "object_type", null: false - t.uuid "object_id", null: false - t.datetime "created_at", null: false - t.datetime "updated_at", null: false - t.index ["object_type", "object_id"], name: "index_communication_website_connections_on_object" - t.index ["university_id"], name: "index_communication_website_connections_on_university_id" - t.index ["website_id"], name: "index_communication_website_connections_on_website_id" - end - create_table "communication_website_git_files", id: :uuid, default: -> { "public.gen_random_uuid()" }, force: :cascade do |t| t.string "previous_path" t.string "about_type", null: false @@ -827,6 +815,8 @@ ActiveRecord::Schema[7.0].define(version: 2023_03_06_134627) do t.string "twitter" t.string "linkedin" t.string "mastodon" + t.float "latitude" + t.float "longitude" t.index ["university_id"], name: "index_university_organizations_on_university_id" end @@ -971,8 +961,6 @@ ActiveRecord::Schema[7.0].define(version: 2023_03_06_134627) do add_foreign_key "communication_website_categories", "education_programs", column: "program_id" add_foreign_key "communication_website_categories", "languages" add_foreign_key "communication_website_categories", "universities" - add_foreign_key "communication_website_connections", "communication_websites", column: "website_id" - add_foreign_key "communication_website_connections", "universities" add_foreign_key "communication_website_git_files", "communication_websites", column: "website_id" add_foreign_key "communication_website_imported_authors", "communication_website_imported_websites", column: "website_id" add_foreign_key "communication_website_imported_authors", "universities"