]> git.openstreetmap.org Git - rails.git/commitdiff
Merge remote-tracking branch 'upstream/pull/4926'
authorTom Hughes <tom@compton.nu>
Sat, 15 Feb 2025 17:07:59 +0000 (17:07 +0000)
committerTom Hughes <tom@compton.nu>
Sat, 15 Feb 2025 17:07:59 +0000 (17:07 +0000)
app/abilities/ability.rb
app/assets/javascripts/index.js
app/assets/javascripts/index/home.js [new file with mode: 0644]
app/controllers/accounts/homes_controller.rb [new file with mode: 0644]
app/views/accounts/homes/show.html.erb [new file with mode: 0644]
app/views/layouts/_header.html.erb
app/views/layouts/map.html.erb
config/locales/en.yml
config/routes.rb
test/system/account_home_test.rb [new file with mode: 0644]

index adedce543e0f889ec519e07a378b26e40bd80a15..dd377a727c817d275fa97d5339aef32904c178ab 100644 (file)
@@ -29,7 +29,7 @@ class Ability
 
     if user&.active?
       can :welcome, :site
-      can :read, [:deletion, :account_terms, :account_pd_declaration]
+      can :read, [:deletion, :account_terms, :account_pd_declaration, :account_home]
 
       if Settings.status != "database_offline"
         can [:read, :create, :destroy], :changeset_subscription
index a5916d3ed13682be79bc664840aa9529caf9f9f9..810327e3fe15bd48a13dd4f5212bcb3bf932fbb7 100644 (file)
@@ -21,6 +21,7 @@
 //= require index/directions
 //= require index/changeset
 //= require index/query
+//= require index/home
 //= require router
 
 $(document).ready(function () {
@@ -221,16 +222,6 @@ $(document).ready(function () {
     L.marker([params.mlat, params.mlon]).addTo(map);
   }
 
-  $("#homeanchor").on("click", function (e) {
-    e.preventDefault();
-
-    var data = $(this).data(),
-        center = L.latLng(data.lat, data.lon);
-
-    map.setView(center, data.zoom);
-    L.marker(center, { icon: OSM.getUserIcon() }).addTo(map);
-  });
-
   function remoteEditHandler(bbox, object) {
     var remoteEditHost = "http://127.0.0.1:8111",
         osmHost = location.protocol + "//" + location.host,
@@ -365,7 +356,8 @@ $(document).ready(function () {
     "/relation/:id(/history)": OSM.Browse(map, "relation"),
     "/relation/:id/history/:version": OSM.OldBrowse(),
     "/changeset/:id": OSM.Changeset(map),
-    "/query": OSM.Query(map)
+    "/query": OSM.Query(map),
+    "/account/home": OSM.Home(map)
   });
 
   if (OSM.preferred_editor === "remote" && document.location.pathname === "/edit") {
diff --git a/app/assets/javascripts/index/home.js b/app/assets/javascripts/index/home.js
new file mode 100644 (file)
index 0000000..7e297b7
--- /dev/null
@@ -0,0 +1,38 @@
+OSM.Home = function (map) {
+  let marker;
+
+  function clearMarker() {
+    if (marker) map.removeLayer(marker);
+    marker = null;
+  }
+
+  const page = {};
+
+  page.pushstate = page.popstate = page.load = function () {
+    map.setSidebarOverlaid(true);
+    clearMarker();
+
+    if (OSM.home) {
+      OSM.router.withoutMoveListener(function () {
+        map.setView(OSM.home, 15, { reset: true });
+      });
+      marker = L.marker(OSM.home, {
+        icon: OSM.getUserIcon(),
+        title: I18n.t("javascripts.home.marker_title")
+      }).addTo(map);
+    } else {
+      $("#browse_status").html(
+        $("<div class='m-2 alert alert-warning'>").text(
+          I18n.t("javascripts.home.not_set")
+        )
+      );
+    }
+  };
+
+  page.unload = function () {
+    clearMarker();
+    $("#browse_status").empty();
+  };
+
+  return page;
+};
diff --git a/app/controllers/accounts/homes_controller.rb b/app/controllers/accounts/homes_controller.rb
new file mode 100644 (file)
index 0000000..e31cce7
--- /dev/null
@@ -0,0 +1,13 @@
+module Accounts
+  class HomesController < ApplicationController
+    layout :map_layout
+
+    before_action :authorize_web
+    before_action :set_locale
+    before_action :require_oauth
+
+    authorize_resource :class => :account_home
+
+    def show; end
+  end
+end
diff --git a/app/views/accounts/homes/show.html.erb b/app/views/accounts/homes/show.html.erb
new file mode 100644 (file)
index 0000000..ea6ee70
--- /dev/null
@@ -0,0 +1 @@
+<% content_for(:content_class) { "overlay-sidebar" } %>
index aa524938181c32cdb210fdb1c1cb9168b056b58d..1f5856908abed577e1a18c580b94a7c679dfeb51 100644 (file)
@@ -88,7 +88,9 @@
           <%= link_to t("users.show.my settings"), edit_account_path, :class => "dropdown-item" %>
           <%= link_to t("users.show.my_preferences"), preferences_path, :class => "dropdown-item" %>
           <div class="dropdown-divider"></div>
-          <%= yield :greeting %>
+          <% if current_user.home_location? %>
+            <%= link_to t("layouts.home"), account_home_path, :class => "dropdown-item" %>
+          <% end %>
           <%= link_to t("layouts.logout"), logout_path(:referer => request.fullpath), :method => "post", :class => "geolink dropdown-item" %>
         </div>
       </div>
index e17ea4ed826bf2f9593973f766dd76f8ba54d4c0..72f6076b45850999df682013efd1b0e9e60b2aa5 100644 (file)
@@ -4,18 +4,6 @@
 
 <% content_for(:body_class) { "map-layout" } %>
 
-<% if current_user&.home_location? %>
-  <% content_for :greeting do %>
-    <%= link_to t("layouts.home"),
-                "#",
-                :id => "homeanchor",
-                :class => "set_position dropdown-item",
-                :data => { :lat => current_user.home_lat,
-                           :lon => current_user.home_lon,
-                           :zoom => 15 } %>
-  <% end %>
-<% end %>
-
 <% content_for :header do %>
   <%= render :partial => "layouts/search", :locals => { :autofocus => false } %>
 <% end %>
index b52bd137f6f4960d64a87add7dbc4d1d4ab3a164..1f9792d39608c15770e62285b2d5185de26b4eaf 100644 (file)
@@ -3317,6 +3317,9 @@ en:
       show_address: Show address
       query_features: Query features
       centre_map: Centre map here
+    home:
+      marker_title: My home location
+      not_set: Home location is not set for your account
   redactions:
     edit:
       heading: "Edit Redaction"
index 7e9cd8dfde78f76e26a78150576561518772c791..b3fc5af15900002fe06d4fe8e5cef36315528d5e 100644 (file)
@@ -300,6 +300,7 @@ OpenStreetMap::Application.routes.draw do
       resource :terms, :only => [:show, :update]
       resource :pd_declaration, :only => [:show, :create]
       resource :deletion, :only => :show
+      resource :home, :only => :show
     end
   end
 
diff --git a/test/system/account_home_test.rb b/test/system/account_home_test.rb
new file mode 100644 (file)
index 0000000..813c45e
--- /dev/null
@@ -0,0 +1,57 @@
+require "application_system_test_case"
+
+class AccountHomeTest < ApplicationSystemTestCase
+  test "Go to Home Location works on map layout pages" do
+    user = create(:user, :display_name => "test user", :home_lat => 60, :home_lon => 30)
+    sign_in_as(user)
+
+    visit root_path
+    assert_no_selector "img.leaflet-marker-icon"
+
+    click_on "test user"
+    click_on "Go to Home Location"
+    all "img.leaflet-marker-icon", :count => 1 do |marker|
+      assert_equal "My home location", marker["title"]
+    end
+
+    click_on "OpenStreetMap logo"
+    assert_no_selector "img.leaflet-marker-icon"
+  end
+
+  test "Go to Home Location works on non-map layout pages" do
+    user = create(:user, :display_name => "test user", :home_lat => 60, :home_lon => 30)
+    sign_in_as(user)
+
+    visit about_path
+    assert_no_selector "img.leaflet-marker-icon"
+
+    click_on "test user"
+    click_on "Go to Home Location"
+    all "img.leaflet-marker-icon", :count => 1 do |marker|
+      assert_equal "My home location", marker["title"]
+    end
+
+    click_on "OpenStreetMap logo"
+    assert_no_selector "img.leaflet-marker-icon"
+  end
+
+  test "Go to Home Location is not available for users without home location" do
+    user = create(:user, :display_name => "test user")
+    sign_in_as(user)
+
+    visit root_path
+    assert_no_selector "img.leaflet-marker-icon"
+
+    click_on "test user"
+    assert_no_link "Go to Home Location"
+  end
+
+  test "account home page shows a warning when visited by users without home location" do
+    user = create(:user, :display_name => "test user")
+    sign_in_as(user)
+
+    visit account_home_path
+    assert_no_selector "img.leaflet-marker-icon"
+    assert_text "Home location is not set"
+  end
+end