diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index 1298ece..204348e 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -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") diff --git a/app/views/users/_form.html.erb b/app/views/users/_form.html.erb index fb0e3cf..4a64b41 100644 --- a/app/views/users/_form.html.erb +++ b/app/views/users/_form.html.erb @@ -35,6 +35,9 @@
<% 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) %> diff --git a/test/controllers/users_controller_test.rb b/test/controllers/users_controller_test.rb index ebc3eed..83cba84 100644 --- a/test/controllers/users_controller_test.rb +++ b/test/controllers/users_controller_test.rb @@ -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 diff --git a/test/fixtures/active_storage/attachments.yml b/test/fixtures/active_storage/attachments.yml new file mode 100644 index 0000000..ad7e8f7 --- /dev/null +++ b/test/fixtures/active_storage/attachments.yml @@ -0,0 +1,4 @@ +trevor_avatar: + name: avatar + record: trevor (User) + blob: trevor_avatar_blob diff --git a/test/fixtures/active_storage/blobs.yml b/test/fixtures/active_storage/blobs.yml new file mode 100644 index 0000000..c8cf9cb --- /dev/null +++ b/test/fixtures/active_storage/blobs.yml @@ -0,0 +1 @@ +trevor_avatar_blob: <%= ActiveStorage::FixtureSet.blob(filename: "trevor.png", service_name: "test") %> diff --git a/test/fixtures/files/trevor.png b/test/fixtures/files/trevor.png new file mode 100644 index 0000000..3a9e536 Binary files /dev/null and b/test/fixtures/files/trevor.png differ diff --git a/todo.md b/todo.md index 2a30609..ea8babd 100644 --- a/todo.md +++ b/todo.md @@ -1,9 +1,8 @@ -- avatars - - delete avatar - - default avatars +- default avatars - shared/private notes - Add characters to users/tables - Character sheets/prototypes - notifications - chat - maps +- add expiration, invalidation to tokens