Compare commits

...

1 Commits

Author SHA1 Message Date
Trevor Vallender 8836532664 Allow deletion of avatars 2024-06-04 16:04:26 +01:00
9 changed files with 48 additions and 4 deletions

View File

@ -32,7 +32,7 @@ class UsersController < ApplicationController
end
def update
if @user.update(existing_user_params)
if existing_user_params.present? && @user.update(existing_user_params)
redirect_to @user, notice: t(".success")
else
flash.now[:alert] = t(".error")
@ -59,6 +59,7 @@ class UsersController < ApplicationController
:last_name,
:profile,
:avatar,
:delete_avatar,
)
end

View File

@ -0,0 +1,25 @@
# frozen_string_literal: true
module DeletableAttachments
extend ActiveSupport::Concern
included do
before_save :delete_attachments
def delete_attachments
attachment_reflections.each do |reflection, _|
if send("delete_#{reflection}")
send(reflection).purge
end
end
end
end
class_methods do
def deletable_attachments(*attachments)
attachments.each do |attachment|
attribute "delete_#{attachment}", :boolean, default: false
end
end
end
end

View File

@ -1,6 +1,9 @@
# frozen_string_literal: true
class User < ApplicationRecord
include DeletableAttachments
deletable_attachments :avatar
has_and_belongs_to_many :site_roles
has_many :owned_tables, foreign_key: :owner_id, class_name: "Table"
has_many :players, dependent: :destroy

View File

@ -35,6 +35,9 @@
<hr>
<% if user.persisted? %>
<%= f.label :delete_avatar %>
<%= f.check_box :delete_avatar %>
<%= 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: { user: { delete_avatar: "1" } })
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,6 +1,4 @@
- avatars
- delete avatar
- default avatars
- default avatars
- shared/private notes
- Add characters to users/tables
- Character sheets/prototypes