Skip to content

Commit

Permalink
Store selected color scheme
Browse files Browse the repository at this point in the history
  • Loading branch information
AntonKhorev committed Dec 1, 2024
1 parent a50b27a commit c1f868a
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
6 changes: 5 additions & 1 deletion app/controllers/preferences_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
22 changes: 22 additions & 0 deletions test/controllers/preferences_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit c1f868a

Please sign in to comment.