Skip to content

Commit

Permalink
Make preferences page editable without clicking 'Edit Preferences'
Browse files Browse the repository at this point in the history
  • Loading branch information
AntonKhorev committed Dec 29, 2024
1 parent 99af52b commit 967e6d4
Show file tree
Hide file tree
Showing 8 changed files with 35 additions and 83 deletions.
4 changes: 1 addition & 3 deletions app/controllers/preferences_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ class PreferencesController < ApplicationController

def show; end

def edit; end

def update
current_user.languages = params[:user][:languages].split(",")

Expand Down Expand Up @@ -40,7 +38,7 @@ def update
redirect_to preferences_path
else
flash.now[:error] = t ".failure"
render :edit
render :show
end
end
end
28 changes: 0 additions & 28 deletions app/views/preferences/edit.html.erb

This file was deleted.

48 changes: 19 additions & 29 deletions app/views/preferences/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,26 @@
<h1><%= t ".title" %></h1>
<% end %>

<dl class="row">
<dt class="col-sm-4"><%= t ".preferred_editor" %></dt>
<%= bootstrap_form_for current_user, :url => { :action => :update } do |f| %>
<%= f.select :preferred_editor, [[t("editor.default", :name => t("editor.#{Settings.default_editor}.name")), "default"]] + Editors::AVAILABLE_EDITORS.collect { |e| [t("editor.#{e}.description"), e] } %>

<% if current_user.preferred_editor? %>
<dd class="col-sm-8"><%= t("editor.#{current_user.preferred_editor}.description") %></dd>
<% else %>
<dd class="col-sm-8"><%= t("editor.default", :name => t("editor.#{Settings.default_editor}.name")) %></dd>
<% end %>
<%= f.text_field :languages %>

<dt class="col-sm-4"><%= t ".preferred_languages" %></dt>
<div class="mb-3">
<%= label_tag "site_color_scheme", t(".preferred_site_color_scheme"), :class => "form-label" %>
<%= select_tag "site_color_scheme",
options_for_select(%w[auto light dark].map { |scheme| [t(".site_color_schemes.#{scheme}"), scheme] },
preferred_color_scheme(:site)),
:class => "form-select" %>
</div>

<dd class="col-sm-8">
<ul class="list-unstyled">
<% current_user.preferred_languages.each do |locale| %>
<li><%= locale %></li>
<% end %>
</ul>
</dd>
<div class="mb-3">
<%= label_tag "map_color_scheme", t(".preferred_map_color_scheme"), :class => "form-label" %>
<%= select_tag "map_color_scheme",
options_for_select(%w[auto light dark].map { |scheme| [t(".map_color_schemes.#{scheme}"), scheme] },
preferred_color_scheme(:map)),
:class => "form-select" %>
</div>

<dt class="col-sm-4"><%= t ".preferred_site_color_scheme" %></dt>
<dd class="col-sm-8">
<%= t ".site_color_schemes.#{preferred_color_scheme(:site)}" %>
</dd>

<dt class="col-sm-4"><%= t ".preferred_map_color_scheme" %></dt>
<dd class="col-sm-8">
<%= t ".map_color_schemes.#{preferred_color_scheme(:map)}" %>
</dd>
</dl>

<div>
<%= link_to t(".edit_preferences"), edit_preferences_path, :class => "btn btn-outline-primary" %>
</div>
<%= f.primary t(".save") %>
<% end %>
4 changes: 0 additions & 4 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1841,11 +1841,7 @@ en:
auto: Auto
light: Light
dark: Dark
edit_preferences: Edit Preferences
edit:
title: Edit Preferences
save: Update Preferences
cancel: Cancel
update:
failure: Couldn't update preferences.
update_success_flash:
Expand Down
3 changes: 2 additions & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,8 @@
resource :deletion, :only => [:show]
end
resource :dashboard, :only => [:show]
resource :preferences, :only => [:show, :edit, :update]
resource :preferences, :only => [:show, :update]
get "/preferences/edit", :to => redirect(:path => "/preferences")
resource :profile, :only => [:edit, :update]

# friendships
Expand Down
25 changes: 10 additions & 15 deletions test/controllers/preferences_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,6 @@ def test_routes
{ :controller => "preferences", :action => "show" }
)

assert_routing(
{ :path => "/preferences/edit", :method => :get },
{ :controller => "preferences", :action => "edit" }
)

assert_routing(
{ :path => "/preferences", :method => :put },
{ :controller => "preferences", :action => "update" }
Expand All @@ -27,35 +22,35 @@ def test_update_preferred_editor
session_for(user)

# Changing to a invalid editor should fail
user.preferred_editor = "unknown"
put preferences_path, :params => { :user => user.attributes }
put preferences_path, :params => { :user => { :preferred_editor => "unknown", :languages => [] } }
assert_response :success
assert_template :edit
assert_template :show
assert_select ".alert-success", false
assert_select ".alert-danger", true
assert_select "form > div > select#user_preferred_editor > option[selected]", false
user.reload
assert_nil user.preferred_editor
assert_equal "light", user.preferences.find_by(:k => "site.color_scheme")&.v
assert_equal "light", user.preferences.find_by(:k => "map.color_scheme")&.v

# Changing to a valid editor should work
user.preferred_editor = "id"
put preferences_path, :params => { :user => user.attributes }
put preferences_path, :params => { :user => { :preferred_editor => "id", :languages => [] } }
assert_redirected_to preferences_path
follow_redirect!
assert_template :show
assert_select ".alert-success", /^Preferences updated/
assert_select "dd", "iD (in-browser editor)"
user.reload
assert_equal "id", user.preferred_editor
assert_equal "light", user.preferences.find_by(:k => "site.color_scheme")&.v
assert_equal "light", user.preferences.find_by(:k => "map.color_scheme")&.v

# Changing to the default editor should work
user.preferred_editor = "default"
put preferences_path, :params => { :user => user.attributes }
put preferences_path, :params => { :user => { :preferred_editor => "default", :languages => [] } }
assert_redirected_to preferences_path
follow_redirect!
assert_template :show
assert_select ".alert-success", /^Preferences updated/
assert_select "dd", "Default (currently iD)"
user.reload
assert_nil user.preferred_editor
assert_equal "light", user.preferences.find_by(:k => "site.color_scheme")&.v
assert_equal "light", user.preferences.find_by(:k => "map.color_scheme")&.v
end
Expand Down
4 changes: 2 additions & 2 deletions test/system/preferences_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ class PreferencesTest < ApplicationSystemTestCase
def test_flash_message_shows_in_original_language
sign_in_as(create(:user))

visit edit_preferences_path
visit preferences_path
click_on "Update Preferences"

assert_content "Preferences updated"
Expand All @@ -13,7 +13,7 @@ def test_flash_message_shows_in_original_language
def test_flash_message_shows_in_new_language
sign_in_as(create(:user))

visit edit_preferences_path
visit preferences_path
fill_in "Preferred Languages", :with => "fr"
click_on "Update Preferences"

Expand Down
2 changes: 1 addition & 1 deletion test/system/view_communities_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def test_lc_links
def test_translated_links
sign_in_as(create(:user))

visit edit_preferences_path
visit preferences_path
fill_in "Preferred Languages", :with => "fr"
click_on "Update Preferences"

Expand Down

0 comments on commit 967e6d4

Please sign in to comment.