Allow deletion of avatars

This commit is contained in:
Trevor Vallender 2024-06-04 14:20:57 +01:00
parent d1175870c2
commit 1ca30a9db2
7 changed files with 22 additions and 4 deletions

View File

@ -32,7 +32,9 @@ class UsersController < ApplicationController
end
def update
if @user.update(existing_user_params)
@user.avatar.purge if params["remove_avatar"] == "true"
if existing_user_params.present? && @user.update(existing_user_params)
redirect_to @user, notice: t(".success")
else
flash.now[:alert] = t(".error")

View File

@ -35,6 +35,9 @@
<hr>
<% if user.persisted? %>
<%= label_tag :remove_avatar %>
<%= check_box_tag :remove_avatar, value: true %>
<%= f.label :avatar %>
<%= f.file_field :avatar %>
<%= display_form_errors(user, :avatar) %>

View File

@ -52,6 +52,15 @@ class UsersControllerTest < ActionDispatch::IntegrationTest
assert_redirected_to user_path(users(:trevor))
end
test "can delete avatar" do
user = users(:trevor)
assert user.avatar.attached?
sign_in users(:trevor)
patch(user_url(user), params: { remove_avatar: "true" })
assert_not user.reload.avatar.attached?
end
private
def user_params

View File

@ -0,0 +1,4 @@
trevor_avatar:
name: avatar
record: trevor (User)
blob: trevor_avatar_blob

View File

@ -0,0 +1 @@
trevor_avatar_blob: <%= ActiveStorage::FixtureSet.blob(filename: "trevor.png", service_name: "test") %>

BIN
test/fixtures/files/trevor.png vendored Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 329 KiB

View File

@ -1,5 +1,3 @@
- avatars
- delete avatar
- default avatars
- shared/private notes
- Add characters to users/tables
@ -7,3 +5,4 @@
- notifications
- chat
- maps
- add expiration, invalidation to tokens