]> git.openstreetmap.org Git - rails.git/commitdiff
Store selected site color scheme
authorAnton Khorev <tony29@yandex.ru>
Sat, 16 Nov 2024 15:13:03 +0000 (18:13 +0300)
committerAnton Khorev <tony29@yandex.ru>
Sun, 1 Dec 2024 01:43:31 +0000 (04:43 +0300)
app/controllers/preferences_controller.rb
test/controllers/preferences_controller_test.rb

index dcf0d8b64e2682364b7c5b489ad1c9c72662bd19..f002d5d395d045307735fbf874dee70eefa71c1a 100644 (file)
@@ -21,7 +21,15 @@ class PreferencesController < ApplicationController
                                     else
                                       params[:user][:preferred_editor]
                                     end
-    if current_user.save
+
+    success = current_user.save
+
+    if params[:site_color_scheme]
+      site_color_scheme_preference = current_user.preferences.find_or_create_by(:k => "site.color_scheme")
+      success &= site_color_scheme_preference.update(:v => params[:site_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" }
       redirect_to preferences_path
index 81760fe5de54816a88b448dcdd6a47e3ea008a8e..d442e2cd2b98f24fadc0c7f87e2e5e208025f52d 100644 (file)
@@ -22,6 +22,7 @@ class PreferencesControllerTest < ActionDispatch::IntegrationTest
 
   def test_update_preferred_editor
     user = create(:user, :languages => [])
+    user.preferences.create(:k => "site.color_scheme", :v => "light")
     session_for(user)
 
     # Changing to a invalid editor should fail
@@ -32,6 +33,7 @@ class PreferencesControllerTest < ActionDispatch::IntegrationTest
     assert_select ".alert-success", false
     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
 
     # Changing to a valid editor should work
     user.preferred_editor = "id"
@@ -41,6 +43,7 @@ class PreferencesControllerTest < ActionDispatch::IntegrationTest
     assert_template :show
     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
 
     # Changing to the default editor should work
     user.preferred_editor = "default"
@@ -50,5 +53,28 @@ class PreferencesControllerTest < ActionDispatch::IntegrationTest
     assert_template :show
     assert_select ".alert-success", /^Preferences updated/
     assert_select "dd", "Default (currently iD)"
+    assert_equal "light", user.preferences.find_by(:k => "site.color_scheme")&.v
+  end
+
+  def test_update_preferred_site_color_scheme
+    user = create(:user, :languages => [])
+    session_for(user)
+    assert_nil user.preferences.find_by(:k => "site.color_scheme")
+
+    # Changing when previously not defined
+    put preferences_path, :params => { :user => user.attributes, :site_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 => "site.color_scheme")&.v
+
+    # Changing when previously defined
+    put preferences_path, :params => { :user => user.attributes, :site_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 => "site.color_scheme")&.v
   end
 end