From: Andy Allan Date: Wed, 30 Jun 2021 17:02:42 +0000 (+0100) Subject: Split user preferences into a separate page X-Git-Tag: live~1995^2~4 X-Git-Url: https://git.openstreetmap.org./rails.git/commitdiff_plain/2403630da87df8a3b57913e8949ae0ff8f611d22 Split user preferences into a separate page Refs #3167 --- diff --git a/app/abilities/ability.rb b/app/abilities/ability.rb index c03ab288c..b77f68be7 100644 --- a/app/abilities/ability.rb +++ b/app/abilities/ability.rb @@ -46,6 +46,7 @@ class Ability can [:make_friend, :remove_friend], Friendship can [:new, :create, :reply, :show, :inbox, :outbox, :mark, :destroy], Message can [:close, :reopen], Note + can [:show, :edit, :update], :preference can [:new, :create], Report can [:mine, :new, :create, :edit, :update, :destroy], Trace can [:account, :go_public], User diff --git a/app/controllers/preferences_controller.rb b/app/controllers/preferences_controller.rb new file mode 100644 index 000000000..6839bf37a --- /dev/null +++ b/app/controllers/preferences_controller.rb @@ -0,0 +1,32 @@ +class PreferencesController < ApplicationController + layout "site" + + before_action :authorize_web + before_action :set_locale + + authorize_resource :class => false + + before_action :check_database_readable + before_action :check_database_writable, :only => [:update] + + def show; end + + def edit; end + + def update + current_user.languages = params[:user][:languages].split(",") + + current_user.preferred_editor = if params[:user][:preferred_editor] == "default" + nil + else + params[:user][:preferred_editor] + end + if current_user.save + flash[:notice] = t ".success" + redirect_to preferences_path + else + flash[:error] = t ".failure" + render :edit + end + end +end diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index 3101e5af2..1f3eb2f7a 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -368,8 +368,6 @@ class UsersController < ApplicationController user.description_format = "markdown" end - user.languages = params[:user][:languages].split(",") - case params[:avatar_action] when "new" user.avatar.attach(params[:user][:avatar]) @@ -385,12 +383,6 @@ class UsersController < ApplicationController user.home_lat = params[:user][:home_lat] user.home_lon = params[:user][:home_lon] - user.preferred_editor = if params[:user][:preferred_editor] == "default" - nil - else - params[:user][:preferred_editor] - end - if params[:user][:auth_provider].nil? || params[:user][:auth_provider].blank? user.auth_provider = nil user.auth_uid = nil @@ -399,8 +391,6 @@ class UsersController < ApplicationController if user.save session[:fingerprint] = user.fingerprint - set_locale(:reset => true) - if user.new_email.blank? || user.new_email == user.email flash[:notice] = t "users.account.flash update success" else diff --git a/app/views/layouts/_header.html.erb b/app/views/layouts/_header.html.erb index fe5bf23c5..6b6271c2b 100644 --- a/app/views/layouts/_header.html.erb +++ b/app/views/layouts/_header.html.erb @@ -96,6 +96,7 @@ <% end %> <%= link_to t("users.show.my profile"), user_path(current_user), :class => "dropdown-item" %> <%= link_to t("users.show.my settings"), { :controller => "users", :action => "account", :display_name => current_user.display_name }, { :class => "dropdown-item" } %> + <%= link_to t("users.show.my_preferences"), preferences_path, :class => "dropdown-item" %> <%= yield :greeting %> <%= link_to t("layouts.logout"), logout_path(:referer => request.fullpath), :method => "post", :class => "geolink dropdown-item" %> diff --git a/app/views/preferences/edit.html.erb b/app/views/preferences/edit.html.erb new file mode 100644 index 000000000..07d89fbb5 --- /dev/null +++ b/app/views/preferences/edit.html.erb @@ -0,0 +1,12 @@ +<% content_for :heading do %> +

<%= t ".title" %>

+<% end %> + +<%= 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] } %> + + <%= f.text_field :languages %> + + <%= f.primary t(".save") %> + <%= link_to t(".cancel"), preferences_path, :class => "btn btn-link" %> +<% end %> diff --git a/app/views/preferences/show.html.erb b/app/views/preferences/show.html.erb new file mode 100644 index 000000000..d17c83b7a --- /dev/null +++ b/app/views/preferences/show.html.erb @@ -0,0 +1,28 @@ +<% content_for :heading do %> +

<%= t ".title" %>

+<% end %> + +
+
<%= t ".preferred_editor" %>
+ + <% if current_user.preferred_editor? %> +
<%= t("editor.#{current_user.preferred_editor}.description") %>
+ <% else %> +
<%= t("editor.default", :name => t("editor.#{Settings.default_editor}.name")) %>
+ <% end %> + +
<%= t ".preferred_languages" %>
+ +
+
    + <% current_user.preferred_languages.each do |locale| %> +
  • <%= locale %>
  • + <% end %> +
+ +
+
+ +
+ <%= link_to t(".edit_preferences"), edit_preferences_path, :class => "btn btn-outline-primary" %> +
diff --git a/app/views/users/account.html.erb b/app/views/users/account.html.erb index dc31de97a..f1c46c710 100644 --- a/app/views/users/account.html.erb +++ b/app/views/users/account.html.erb @@ -58,9 +58,7 @@ - <%= 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] } %> <%= f.richtext_field :description, :cols => 80, :rows => 20 %> - <%= f.text_field :languages %>
<%= f.label t(".image") %> diff --git a/config/locales/en.yml b/config/locales/en.yml index 0119aeae1..74d68263c 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -1644,6 +1644,19 @@ en: reset: "Reset Password" flash changed: "Your password has been changed." flash token bad: "Did not find that token, check the URL maybe?" + preferences: + show: + title: My Preferences + preferred_editor: Preferred Editor + preferred_languages: Preferred Languages + edit_preferences: Edit Preferences + edit: + title: Edit Preferences + save: Update Preferences + cancel: Cancel + update: + success: Preferences updated. + failure: Couldn't update preferences. sessions: new: title: "Login" @@ -2445,6 +2458,7 @@ en: my profile: My Profile my settings: My Settings my comments: My Comments + my_preferences: My Preferences blocks on me: Blocks on Me blocks by me: Blocks by Me send message: Send Message diff --git a/config/routes.rb b/config/routes.rb index 048db8b33..b68882dbf 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -240,6 +240,8 @@ OpenStreetMap::Application.routes.draw do match "/user/:display_name/account" => "users#account", :via => [:get, :post], :as => "user_account" post "/user/:display_name/set_status" => "users#set_status", :as => :set_status_user + resource :preferences, :only => [:show, :edit, :update] + # friendships match "/user/:display_name/make_friend" => "friendships#make_friend", :via => [:get, :post], :as => "make_friend" match "/user/:display_name/remove_friend" => "friendships#remove_friend", :via => [:get, :post], :as => "remove_friend" diff --git a/test/controllers/preferences_controller_test.rb b/test/controllers/preferences_controller_test.rb new file mode 100644 index 000000000..8c529e62a --- /dev/null +++ b/test/controllers/preferences_controller_test.rb @@ -0,0 +1,56 @@ +require "test_helper" + +class PreferencesControllerTest < ActionDispatch::IntegrationTest + ## + # test all routes which lead to this controller + def test_routes + assert_routing( + { :path => "/preferences", :method => :get }, + { :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" } + ) + end + + def test_update_preferred_editor + user = create(:user, :languages => []) + session_for(user) + + # Changing to a invalid editor should fail + user.preferred_editor = "unknown" + put preferences_path, :params => { :user => user.attributes } + assert_response :success + assert_template :edit + assert_select ".notice", false + assert_select ".error", true + assert_select "form > div.form-group > select#user_preferred_editor > option[selected]", false + + # Changing to a valid editor should work + user.preferred_editor = "id" + put preferences_path, :params => { :user => user.attributes } + assert_response :redirect + assert_redirected_to preferences_path + follow_redirect! + assert_template :show + assert_select ".notice", /^Preferences updated/ + assert_select "dd", "iD (in-browser editor)" + + # Changing to the default editor should work + user.preferred_editor = "default" + put preferences_path, :params => { :user => user.attributes } + assert_response :redirect + assert_redirected_to preferences_path + follow_redirect! + assert_template :show + assert_select ".notice", /^Preferences updated/ + assert_select "dd", "Default (currently iD)" + end +end diff --git a/test/controllers/users_controller_test.rb b/test/controllers/users_controller_test.rb index 62eb79f0c..6eebd7f1a 100644 --- a/test/controllers/users_controller_test.rb +++ b/test/controllers/users_controller_test.rb @@ -475,36 +475,6 @@ class UsersControllerTest < ActionDispatch::IntegrationTest assert_select ".notice", /^User information updated successfully/ assert_select "form#accountForm > div.form-group > div#user_description_container > div#user_description_content > textarea#user_description", user.description - # Changing to a invalid editor should fail - user.preferred_editor = "unknown" - post user_account_path(user), :params => { :user => user.attributes } - assert_response :success - assert_template :account - assert_select ".notice", false - assert_select "form#accountForm > div.form-group > select#user_preferred_editor > option[selected]", false - - # Changing to a valid editor should work - user.preferred_editor = "id" - post user_account_path(user), :params => { :user => user.attributes } - assert_response :redirect - assert_redirected_to user_account_url(user) - get user_account_path(user) - assert_response :success - assert_template :account - assert_select ".notice", /^User information updated successfully/ - assert_select "form#accountForm > div.form-group > select#user_preferred_editor > option[selected][value=?]", "id" - - # Changing to the default editor should work - user.preferred_editor = "default" - post user_account_path(user), :params => { :user => user.attributes } - assert_response :redirect - assert_redirected_to user_account_url(user) - get user_account_path(user) - assert_response :success - assert_template :account - assert_select ".notice", /^User information updated successfully/ - assert_select "form#accountForm > div.form-group > select#user_preferred_editor > option[selected]", false - # Changing to an uploaded image should work image = Rack::Test::UploadedFile.new("test/gpx/fixtures/a.gif", "image/gif") post user_account_path(user), :params => { :avatar_action => "new", :user => user.attributes.merge(:avatar => image) }