From 9db0e9407a05b5abbc69c2f70f1dc30457654b86 Mon Sep 17 00:00:00 2001 From: Anton Khorev Date: Sat, 16 Nov 2024 18:13:03 +0300 Subject: [PATCH] Store selected color scheme --- app/controllers/preferences_controller.rb | 6 ++++- .../preferences_controller_test.rb | 22 +++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/app/controllers/preferences_controller.rb b/app/controllers/preferences_controller.rb index dcf0d8b64e..37c8d1bf6a 100644 --- a/app/controllers/preferences_controller.rb +++ b/app/controllers/preferences_controller.rb @@ -21,7 +21,11 @@ def update else params[:user][:preferred_editor] end - if current_user.save + + pref = current_user.preferences.find_or_create_by(:k => "site.color_scheme") + pref.v = params[:color_scheme] + + if current_user.save && pref.save # 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 diff --git a/test/controllers/preferences_controller_test.rb b/test/controllers/preferences_controller_test.rb index 81760fe5de..8633fb5cac 100644 --- a/test/controllers/preferences_controller_test.rb +++ b/test/controllers/preferences_controller_test.rb @@ -51,4 +51,26 @@ def test_update_preferred_editor assert_select ".alert-success", /^Preferences updated/ assert_select "dd", "Default (currently iD)" 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, :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, :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