From 9f341430493cdaafb6ea9ce6ac29fc6af291d57c Mon Sep 17 00:00:00 2001 From: Anton Khorev Date: Sun, 1 Dec 2024 04:18:56 +0300 Subject: [PATCH] Store selected map color scheme --- app/controllers/preferences_controller.rb | 5 ++++ .../preferences_controller_test.rb | 26 +++++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/app/controllers/preferences_controller.rb b/app/controllers/preferences_controller.rb index f002d5d395..1d96766ef2 100644 --- a/app/controllers/preferences_controller.rb +++ b/app/controllers/preferences_controller.rb @@ -29,6 +29,11 @@ def update success &= site_color_scheme_preference.update(:v => params[:site_color_scheme]) end + if params[:map_color_scheme] + map_color_scheme_preference = current_user.preferences.find_or_create_by(:k => "map.color_scheme") + success &= map_color_scheme_preference.update(:v => params[:map_color_scheme]) + end + if success # Use a partial so that it is rendered during the next page load in the correct language. flash[:notice] = { :partial => "preferences/update_success_flash" } diff --git a/test/controllers/preferences_controller_test.rb b/test/controllers/preferences_controller_test.rb index d442e2cd2b..1cd07b7d9f 100644 --- a/test/controllers/preferences_controller_test.rb +++ b/test/controllers/preferences_controller_test.rb @@ -23,6 +23,7 @@ def test_routes def test_update_preferred_editor user = create(:user, :languages => []) user.preferences.create(:k => "site.color_scheme", :v => "light") + user.preferences.create(:k => "map.color_scheme", :v => "light") session_for(user) # Changing to a invalid editor should fail @@ -34,6 +35,7 @@ def test_update_preferred_editor assert_select ".alert-danger", true assert_select "form > div > select#user_preferred_editor > option[selected]", false assert_equal "light", user.preferences.find_by(:k => "site.color_scheme")&.v + assert_equal "light", user.preferences.find_by(:k => "map.color_scheme")&.v # Changing to a valid editor should work user.preferred_editor = "id" @@ -44,6 +46,7 @@ def test_update_preferred_editor assert_select ".alert-success", /^Preferences updated/ assert_select "dd", "iD (in-browser editor)" assert_equal "light", user.preferences.find_by(:k => "site.color_scheme")&.v + assert_equal "light", user.preferences.find_by(:k => "map.color_scheme")&.v # Changing to the default editor should work user.preferred_editor = "default" @@ -54,6 +57,7 @@ def test_update_preferred_editor assert_select ".alert-success", /^Preferences updated/ assert_select "dd", "Default (currently iD)" assert_equal "light", user.preferences.find_by(:k => "site.color_scheme")&.v + assert_equal "light", user.preferences.find_by(:k => "map.color_scheme")&.v end def test_update_preferred_site_color_scheme @@ -77,4 +81,26 @@ def test_update_preferred_site_color_scheme assert_select ".alert-success", /^Preferences updated/ assert_equal "auto", user.preferences.find_by(:k => "site.color_scheme")&.v end + + def test_update_preferred_map_color_scheme + user = create(:user, :languages => []) + session_for(user) + assert_nil user.preferences.find_by(:k => "map.color_scheme") + + # Changing when previously not defined + put preferences_path, :params => { :user => user.attributes, :map_color_scheme => "light" } + assert_redirected_to preferences_path + follow_redirect! + assert_template :show + assert_select ".alert-success", /^Preferences updated/ + assert_equal "light", user.preferences.find_by(:k => "map.color_scheme")&.v + + # Changing when previously defined + put preferences_path, :params => { :user => user.attributes, :map_color_scheme => "auto" } + assert_redirected_to preferences_path + follow_redirect! + assert_template :show + assert_select ".alert-success", /^Preferences updated/ + assert_equal "auto", user.preferences.find_by(:k => "map.color_scheme")&.v + end end