From eefa614dd73212a8338bdd1e085a541040dca94f 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..722e4599a6 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 + + site_color_scheme_preference = current_user.preferences.find_or_create_by(:k => "site.color_scheme") + site_color_scheme_preference.v = params[:site_color_scheme] + + if current_user.save && site_color_scheme_preference.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..dec483d454 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, :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