From e97f8240d5b87a5de0995f689ba45d4c4756dc30 Mon Sep 17 00:00:00 2001 From: Anton Khorev Date: Tue, 3 Jun 2025 18:20:28 +0300 Subject: [PATCH] Move profile image editing to image page --- .../profiles/descriptions_controller.rb | 12 ------ app/controllers/profiles/images_controller.rb | 12 ++++++ app/views/profiles/descriptions/show.html.erb | 31 ---------------- app/views/profiles/images/show.html.erb | 32 ++++++++++++++++ config/locales/en.yml | 19 +++++----- .../profiles/descriptions_controller_test.rb | 32 ---------------- .../profiles/images_controller_test.rb | 37 +++++++++++++++++++ 7 files changed, 90 insertions(+), 85 deletions(-) diff --git a/app/controllers/profiles/descriptions_controller.rb b/app/controllers/profiles/descriptions_controller.rb index 1419c6df3..15a3e8129 100644 --- a/app/controllers/profiles/descriptions_controller.rb +++ b/app/controllers/profiles/descriptions_controller.rb @@ -11,18 +11,6 @@ module Profiles 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.save end end diff --git a/app/controllers/profiles/images_controller.rb b/app/controllers/profiles/images_controller.rb index 8c43daad5..c3cba5aba 100644 --- a/app/controllers/profiles/images_controller.rb +++ b/app/controllers/profiles/images_controller.rb @@ -3,6 +3,18 @@ module Profiles private def update_profile + 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.save end end diff --git a/app/views/profiles/descriptions/show.html.erb b/app/views/profiles/descriptions/show.html.erb index 956dc5d33..e1fa7be75 100644 --- a/app/views/profiles/descriptions/show.html.erb +++ b/app/views/profiles/descriptions/show.html.erb @@ -36,37 +36,6 @@ -
- <%= f.label t(".image") %> -
-
- <%= user_image current_user %> -
-
- <% if current_user.avatar.attached? %> - <%= f.radio_button "avatar_action", "keep", :name => "avatar_action", :label => t(".keep image"), :checked => !current_user.image_use_gravatar %> - <% end %> - <% if current_user.avatar.attached? || current_user.image_use_gravatar? %> - <%= f.radio_button "avatar_action", "delete", :name => "avatar_action", :label => t(".delete image"), :checked => false %> - <% end %> - <% if current_user.avatar.attached? %> - <%= f.form_group :help => t(".image size hint"), :class => "mb-0" do %> - <%= f.radio_button "avatar_action", "new", :name => "avatar_action", :label => t(".replace image"), :checked => false %> - <%= f.file_field :avatar, :hide_label => true, :wrapper => { :class => "mb-0" } %> - <% end %> - <% else %> - <%= f.form_group :help => t(".image size hint"), :class => "mb-0" do %> - <%= f.radio_button "avatar_action", "new", :name => "avatar_action", :label => t(".new image"), :checked => false %> - <%= f.file_field :avatar, :hide_label => true, :wrapper => { :class => "mb-0" } %> - <% end %> - <% end %> - <%= f.form_group :help => link_to(t(".gravatar.what_is_gravatar"), t(".gravatar.link")) do %> - <%= f.radio_button "avatar_action", "gravatar", :name => "avatar_action", :label => t(".gravatar.gravatar"), :checked => current_user.image_use_gravatar %> - <% end %> -
-
-
- <%= f.primary t(".save") %> <%= link_to t(".cancel"), current_user, :class => "btn btn-link" %> <% end %> diff --git a/app/views/profiles/images/show.html.erb b/app/views/profiles/images/show.html.erb index a63e38f56..12cc1d9e2 100644 --- a/app/views/profiles/images/show.html.erb +++ b/app/views/profiles/images/show.html.erb @@ -1,3 +1,7 @@ +<% content_for :head do %> + <%= javascript_include_tag "user" %> +<% end %> + <% content_for :heading_class, "pb-0" %> <% content_for :heading do %> @@ -6,6 +10,34 @@ <% end %> <%= bootstrap_form_for current_user, :url => { :action => :update } do |f| %> +
+
+ <%= user_image current_user %> +
+
+ <% if current_user.avatar.attached? %> + <%= f.radio_button "avatar_action", "keep", :name => "avatar_action", :label => t(".keep image"), :checked => !current_user.image_use_gravatar %> + <% end %> + <% if current_user.avatar.attached? || current_user.image_use_gravatar? %> + <%= f.radio_button "avatar_action", "delete", :name => "avatar_action", :label => t(".delete image"), :checked => false %> + <% end %> + <% if current_user.avatar.attached? %> + <%= f.form_group :help => t(".image size hint"), :class => "mb-0" do %> + <%= f.radio_button "avatar_action", "new", :name => "avatar_action", :label => t(".replace image"), :checked => false %> + <%= f.file_field :avatar, :hide_label => true, :wrapper => { :class => "mb-0" } %> + <% end %> + <% else %> + <%= f.form_group :help => t(".image size hint"), :class => "mb-0" do %> + <%= f.radio_button "avatar_action", "new", :name => "avatar_action", :label => t(".new image"), :checked => false %> + <%= f.file_field :avatar, :hide_label => true, :wrapper => { :class => "mb-0" } %> + <% end %> + <% end %> + <%= f.form_group :help => link_to(t(".gravatar.what_is_gravatar"), t(".gravatar.link")) do %> + <%= f.radio_button "avatar_action", "gravatar", :name => "avatar_action", :label => t(".gravatar.gravatar"), :checked => current_user.image_use_gravatar %> + <% end %> +
+
+ <%= f.primary t(".save") %> <%= link_to t(".cancel"), current_user, :class => "btn btn-link" %> <% end %> diff --git a/config/locales/en.yml b/config/locales/en.yml index a2a875287..4d1850085 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -2005,16 +2005,6 @@ en: 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)" social_links: title: Social Profile Links remove: Remove @@ -2027,6 +2017,15 @@ en: title: Edit Profile save: Update Profile cancel: Cancel + 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)" update: success: Profile updated. failure: Couldn't update profile. diff --git a/test/controllers/profiles/descriptions_controller_test.rb b/test/controllers/profiles/descriptions_controller_test.rb index 7cda7396a..8dbbf483c 100644 --- a/test/controllers/profiles/descriptions_controller_test.rb +++ b/test/controllers/profiles/descriptions_controller_test.rb @@ -50,38 +50,6 @@ module Profiles 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) diff --git a/test/controllers/profiles/images_controller_test.rb b/test/controllers/profiles/images_controller_test.rb index 84f140dec..9a506ef1d 100644 --- a/test/controllers/profiles/images_controller_test.rb +++ b/test/controllers/profiles/images_controller_test.rb @@ -30,5 +30,42 @@ module Profiles assert_redirected_to login_path(:referer => profile_image_path) end + + def test_update + user = create(:user) + session_for(user) + + # Changing to an uploaded image should work + image = Rack::Test::UploadedFile.new("test/gpx/fixtures/a.gif", "image/gif") + put profile_image_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_dom ".alert-success", :text => "Profile updated." + get profile_image_path + assert_dom "form > div > div.col-sm-10 > div.form-check > input[name=avatar_action][checked][value=?]", "keep" + + # Changing to a gravatar image should work + put profile_image_path, :params => { :avatar_action => "gravatar", :user => { :description => user.description } } + assert_redirected_to user_path(user) + follow_redirect! + assert_response :success + assert_template :show + assert_dom ".alert-success", :text => "Profile updated." + get profile_image_path + assert_dom "form > div > div.col-sm-10 > div > div.form-check > input[name=avatar_action][checked][value=?]", "gravatar" + + # Removing the image should work + put profile_image_path, :params => { :avatar_action => "delete", :user => { :description => user.description } } + assert_redirected_to user_path(user) + follow_redirect! + assert_response :success + assert_template :show + assert_dom ".alert-success", :text => "Profile updated." + get profile_image_path + assert_dom "form > div > div.col-sm-10 > div > input[name=avatar_action][checked]", false + assert_dom "form > div > div.col-sm-10 > div > div.form-check > input[name=avatar_action][checked]", false + end end end -- 2.39.5