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 end
def update 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") redirect_to @user, notice: t(".success")
else else
flash.now[:alert] = t(".error") flash.now[:alert] = t(".error")

View File

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

View File

@ -52,6 +52,15 @@ class UsersControllerTest < ActionDispatch::IntegrationTest
assert_redirected_to user_path(users(:trevor)) assert_redirected_to user_path(users(:trevor))
end 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 private
def user_params 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,9 +1,8 @@
- avatars - default avatars
- delete avatar
- default avatars
- shared/private notes - shared/private notes
- Add characters to users/tables - Add characters to users/tables
- Character sheets/prototypes - Character sheets/prototypes
- notifications - notifications
- chat - chat
- maps - maps
- add expiration, invalidation to tokens