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..278797c49b 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_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