From 8e66e1e83bd62b8f755f7265893ad0f0231e2b90 Mon Sep 17 00:00:00 2001 From: Anton Khorev Date: Tue, 3 Jun 2025 04:15:29 +0300 Subject: [PATCH] Move profile resource to profile/description --- .../profiles/descriptions_controller.rb | 51 +++++++++ app/controllers/profiles_controller.rb | 49 --------- app/views/dashboards/show.html.erb | 2 +- .../profiles/{ => descriptions}/show.html.erb | 0 app/views/users/show.html.erb | 2 +- config/locales/en.yml | 55 +++++----- config/routes.rb | 8 +- .../profiles/descriptions_controller_test.rb | 100 ++++++++++++++++++ test/controllers/profiles_controller_test.rb | 95 ----------------- test/system/user_company_test.rb | 2 +- test/system/user_location_change_test.rb | 2 +- 11 files changed, 189 insertions(+), 177 deletions(-) create mode 100644 app/controllers/profiles/descriptions_controller.rb delete mode 100644 app/controllers/profiles_controller.rb rename app/views/profiles/{ => descriptions}/show.html.erb (100%) create mode 100644 test/controllers/profiles/descriptions_controller_test.rb delete mode 100644 test/controllers/profiles_controller_test.rb diff --git a/app/controllers/profiles/descriptions_controller.rb b/app/controllers/profiles/descriptions_controller.rb new file mode 100644 index 000000000..262048be3 --- /dev/null +++ b/app/controllers/profiles/descriptions_controller.rb @@ -0,0 +1,51 @@ +module Profiles + class DescriptionsController < ApplicationController + layout "site" + + before_action :authorize_web + before_action :set_locale + + authorize_resource :class => :profile + + before_action :check_database_readable + before_action :check_database_writable, :only => [:update] + + def show; end + + def update + social_links_params = params.permit(:user => [:social_links_attributes => [:id, :url, :_destroy]]) + current_user.assign_attributes(social_links_params[:user]) + + if params[:user][:description] != current_user.description + current_user.description = params[:user][:description] + current_user.description_format = "markdown" + end + + case params[:avatar_action] + when "new" + current_user.avatar.attach(params[:user][:avatar]) + current_user.image_use_gravatar = false + when "delete" + current_user.avatar.purge_later + current_user.image_use_gravatar = false + when "gravatar" + current_user.avatar.purge_later + current_user.image_use_gravatar = true + end + + current_user.company = params[:user][:company] + + current_user.home_lat = params[:user][:home_lat] + current_user.home_lon = params[:user][:home_lon] + current_user.home_location_name = params[:user][:home_location_name] + + if current_user.save + flash[:notice] = t ".success" + redirect_to user_path(current_user) + else + flash.now[:error] = t ".failure" + render :show + end + end + end +end diff --git a/app/controllers/profiles_controller.rb b/app/controllers/profiles_controller.rb deleted file mode 100644 index 24b49a7e5..000000000 --- a/app/controllers/profiles_controller.rb +++ /dev/null @@ -1,49 +0,0 @@ -class ProfilesController < 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 update - social_links_params = params.permit(:user => [:social_links_attributes => [:id, :url, :_destroy]]) - current_user.assign_attributes(social_links_params[:user]) - - if params[:user][:description] != current_user.description - current_user.description = params[:user][:description] - current_user.description_format = "markdown" - end - - case params[:avatar_action] - when "new" - current_user.avatar.attach(params[:user][:avatar]) - current_user.image_use_gravatar = false - when "delete" - current_user.avatar.purge_later - current_user.image_use_gravatar = false - when "gravatar" - current_user.avatar.purge_later - current_user.image_use_gravatar = true - end - - current_user.company = params[:user][:company] - - current_user.home_lat = params[:user][:home_lat] - current_user.home_lon = params[:user][:home_lon] - current_user.home_location_name = params[:user][:home_location_name] - - if current_user.save - flash[:notice] = t ".success" - redirect_to user_path(current_user) - else - flash.now[:error] = t ".failure" - render :show - end - end -end diff --git a/app/views/dashboards/show.html.erb b/app/views/dashboards/show.html.erb index 52d4f3edc..cb52195f2 100644 --- a/app/views/dashboards/show.html.erb +++ b/app/views/dashboards/show.html.erb @@ -8,7 +8,7 @@
<% if !current_user.home_location? %>
-

<%= t(".no_home_location_html", :edit_profile_link => link_to(t(".edit_your_profile"), profile_path)) %>

+

<%= t(".no_home_location_html", :edit_profile_link => link_to(t(".edit_your_profile"), profile_description_path)) %>

<% else %> <% content_for :head do %> diff --git a/app/views/profiles/show.html.erb b/app/views/profiles/descriptions/show.html.erb similarity index 100% rename from app/views/profiles/show.html.erb rename to app/views/profiles/descriptions/show.html.erb diff --git a/app/views/users/show.html.erb b/app/views/users/show.html.erb index 9c4af3e4d..aed26de2e 100644 --- a/app/views/users/show.html.erb +++ b/app/views/users/show.html.erb @@ -286,6 +286,6 @@ <% if current_user and @user.id == current_user.id %>
- <%= link_to t(".edit_profile"), profile_path, :class => "btn btn-outline-primary" %> + <%= link_to t(".edit_profile"), profile_description_path, :class => "btn btn-outline-primary" %>
<% end %> diff --git a/config/locales/en.yml b/config/locales/en.yml index a435d7fa4..33310b193 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -1994,33 +1994,34 @@ en: title: My Advanced Preferences save: Update Preferences profiles: - show: - title: Edit Profile - save: Update Profile - cancel: Cancel - image: Image - gravatar: - gravatar: "Use Gravatar" - link: "https://wiki.openstreetmap.org/wiki/Gravatar" - what_is_gravatar: "What is Gravatar?" - new image: "Add an image" - keep image: "Keep the current image" - delete image: "Remove the current image" - replace image: "Replace the current image" - image size hint: "(square images at least 100x100 work best)" - home location: "Home Location" - no home location: "You have not entered your home location." - update home location on click: "Update home location when I click on the map?" - show: "Show" - delete: "Delete" - undelete: "Undo delete" - social_links: - title: Social Profile Links - remove: Remove - add: Add Social Link - update: - success: Profile updated. - failure: Couldn't update profile. + descriptions: + show: + title: Edit Profile + save: Update Profile + cancel: Cancel + image: Image + gravatar: + gravatar: "Use Gravatar" + link: "https://wiki.openstreetmap.org/wiki/Gravatar" + what_is_gravatar: "What is Gravatar?" + new image: "Add an image" + keep image: "Keep the current image" + delete image: "Remove the current image" + replace image: "Replace the current image" + image size hint: "(square images at least 100x100 work best)" + home location: "Home Location" + no home location: "You have not entered your home location." + update home location on click: "Update home location when I click on the map?" + show: "Show" + delete: "Delete" + undelete: "Undo delete" + social_links: + title: Social Profile Links + remove: Remove + add: Add Social Link + update: + success: Profile updated. + failure: Couldn't update profile. sessions: new: tab_title: "Log In" diff --git a/config/routes.rb b/config/routes.rb index 6a2393c69..413cdd180 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -317,8 +317,12 @@ OpenStreetMap::Application.routes.draw do get "/account/edit", :to => redirect(:path => "/account"), :as => nil resource :dashboard, :only => [:show] - resource :profile, :only => [:show, :update] - get "/profile/edit", :to => redirect(:path => "/profile"), :as => nil + + namespace :profile, :module => :profiles do + resource :description, :only => [:show, :update] + end + get "/profile", :to => redirect(:path => "/profile/description"), :as => nil + get "/profile/edit", :to => redirect(:path => "/profile/description"), :as => nil scope :preferences, :module => :preferences do resource :basic_preferences, :path => "basic", :only => [:show, :update] diff --git a/test/controllers/profiles/descriptions_controller_test.rb b/test/controllers/profiles/descriptions_controller_test.rb new file mode 100644 index 000000000..11a7004a1 --- /dev/null +++ b/test/controllers/profiles/descriptions_controller_test.rb @@ -0,0 +1,100 @@ +require "test_helper" + +module Profiles + class DescriptionsControllerTest < ActionDispatch::IntegrationTest + ## + # test all routes which lead to this controller + def test_routes + assert_routing( + { :path => "/profile/description", :method => :get }, + { :controller => "profiles/descriptions", :action => "show" } + ) + assert_routing( + { :path => "/profile/description", :method => :put }, + { :controller => "profiles/descriptions", :action => "update" } + ) + + get "/profile" + assert_redirected_to "/profile/description" + + get "/profile/edit" + assert_redirected_to "/profile/description" + end + + def test_update + user = create(:user) + session_for(user) + + # Updating the description should work + put profile_description_path, :params => { :user => { :description => "new description" } } + assert_redirected_to user_path(user) + follow_redirect! + assert_response :success + assert_template :show + assert_select ".alert-success", /^Profile updated./ + assert_select "div", "new description" + + # Changing to an uploaded image should work + image = Rack::Test::UploadedFile.new("test/gpx/fixtures/a.gif", "image/gif") + put profile_description_path, :params => { :avatar_action => "new", :user => { :avatar => image, :description => user.description } } + assert_redirected_to user_path(user) + follow_redirect! + assert_response :success + assert_template :show + assert_select ".alert-success", /^Profile updated./ + get profile_description_path + assert_select "form > fieldset > div > div.col-sm-10 > div.form-check > input[name=avatar_action][checked][value=?]", "keep" + + # Changing to a gravatar image should work + put profile_description_path, :params => { :avatar_action => "gravatar", :user => { :description => user.description } } + assert_redirected_to user_path(user) + follow_redirect! + assert_response :success + assert_template :show + assert_select ".alert-success", /^Profile updated./ + get profile_description_path + assert_select "form > fieldset > div > div.col-sm-10 > div > div.form-check > input[name=avatar_action][checked][value=?]", "gravatar" + + # Removing the image should work + put profile_description_path, :params => { :avatar_action => "delete", :user => { :description => user.description } } + assert_redirected_to user_path(user) + follow_redirect! + assert_response :success + assert_template :show + assert_select ".alert-success", /^Profile updated./ + get profile_description_path + assert_select "form > fieldset > div > div.col-sm-10 > div > input[name=avatar_action][checked]", false + assert_select "form > fieldset > div > div.col-sm-10 > div > div.form-check > input[name=avatar_action][checked]", false + + # Updating social links should work + put profile_description_path, :params => { :user => { :description => user.description, :social_links_attributes => [{ :url => "https://test.com/test" }] } } + assert_redirected_to user_path(user) + follow_redirect! + assert_response :success + assert_template :show + assert_select ".alert-success", /^Profile updated./ + assert_select "a", "test.com/test" + + # Updating the company name should work + put profile_description_path, :params => { :user => { :company => "new company", :description => user.description } } + assert_redirected_to user_path(user) + follow_redirect! + assert_response :success + assert_template :show + assert_select ".alert-success", /^Profile updated./ + user.reload + assert_equal "new company", user.company + end + + def test_update_empty_social_link + user = create(:user) + session_for(user) + + put profile_description_path, :params => { :user => { :description => user.description, :social_links_attributes => [{ :url => "" }] } } + + assert_response :success + assert_template :show + assert_dom ".alert-danger", :text => "Couldn't update profile." + end + end +end diff --git a/test/controllers/profiles_controller_test.rb b/test/controllers/profiles_controller_test.rb deleted file mode 100644 index 11526046d..000000000 --- a/test/controllers/profiles_controller_test.rb +++ /dev/null @@ -1,95 +0,0 @@ -require "test_helper" - -class ProfilesControllerTest < ActionDispatch::IntegrationTest - ## - # test all routes which lead to this controller - def test_routes - assert_routing( - { :path => "/profile", :method => :get }, - { :controller => "profiles", :action => "show" } - ) - assert_routing( - { :path => "/profile", :method => :put }, - { :controller => "profiles", :action => "update" } - ) - - get "/profile/edit" - assert_redirected_to "/profile" - end - - def test_update - user = create(:user) - session_for(user) - - # Updating the description should work - put profile_path, :params => { :user => { :description => "new description" } } - assert_redirected_to user_path(user) - follow_redirect! - assert_response :success - assert_template :show - assert_select ".alert-success", /^Profile updated./ - assert_select "div", "new description" - - # Changing to an uploaded image should work - image = Rack::Test::UploadedFile.new("test/gpx/fixtures/a.gif", "image/gif") - put profile_path, :params => { :avatar_action => "new", :user => { :avatar => image, :description => user.description } } - assert_redirected_to user_path(user) - follow_redirect! - assert_response :success - assert_template :show - assert_select ".alert-success", /^Profile updated./ - get profile_path - assert_select "form > fieldset > div > div.col-sm-10 > div.form-check > input[name=avatar_action][checked][value=?]", "keep" - - # Changing to a gravatar image should work - put profile_path, :params => { :avatar_action => "gravatar", :user => { :description => user.description } } - assert_redirected_to user_path(user) - follow_redirect! - assert_response :success - assert_template :show - assert_select ".alert-success", /^Profile updated./ - get profile_path - assert_select "form > fieldset > div > div.col-sm-10 > div > div.form-check > input[name=avatar_action][checked][value=?]", "gravatar" - - # Removing the image should work - put profile_path, :params => { :avatar_action => "delete", :user => { :description => user.description } } - assert_redirected_to user_path(user) - follow_redirect! - assert_response :success - assert_template :show - assert_select ".alert-success", /^Profile updated./ - get profile_path - assert_select "form > fieldset > div > div.col-sm-10 > div > input[name=avatar_action][checked]", false - assert_select "form > fieldset > div > div.col-sm-10 > div > div.form-check > input[name=avatar_action][checked]", false - - # Updating social links should work - put profile_path, :params => { :user => { :description => user.description, :social_links_attributes => [{ :url => "https://test.com/test" }] } } - assert_redirected_to user_path(user) - follow_redirect! - assert_response :success - assert_template :show - assert_select ".alert-success", /^Profile updated./ - assert_select "a", "test.com/test" - - # Updating the company name should work - put profile_path, :params => { :user => { :company => "new company", :description => user.description } } - assert_redirected_to user_path(user) - follow_redirect! - assert_response :success - assert_template :show - assert_select ".alert-success", /^Profile updated./ - user.reload - assert_equal "new company", user.company - end - - def test_update_empty_social_link - user = create(:user) - session_for(user) - - put profile_path, :params => { :user => { :description => user.description, :social_links_attributes => [{ :url => "" }] } } - - assert_response :success - assert_template :show - assert_dom ".alert-danger", :text => "Couldn't update profile." - end -end diff --git a/test/system/user_company_test.rb b/test/system/user_company_test.rb index c1e094cc8..3d0ae5367 100644 --- a/test/system/user_company_test.rb +++ b/test/system/user_company_test.rb @@ -11,7 +11,7 @@ class UserCompanyTest < ApplicationSystemTestCase assert_no_text :all, "Company" end - visit profile_path + visit profile_description_path within_content_body do fill_in "Company", :with => "Test Co." diff --git a/test/system/user_location_change_test.rb b/test/system/user_location_change_test.rb index ac032a3d8..c302c5cad 100644 --- a/test/system/user_location_change_test.rb +++ b/test/system/user_location_change_test.rb @@ -15,7 +15,7 @@ class UserLocationChangeTest < ApplicationSystemTestCase assert_no_text :all, "Home location" end - visit profile_path + visit profile_description_path within_content_body do fill_in "Home Location Name", :with => "Test Place" -- 2.39.5