Support the ability for admins to delete users

This commit is contained in:
Michael Ball 2021-03-08 20:19:57 -08:00
parent 331e0a1610
commit 332bf8f1df
3 changed files with 28 additions and 5 deletions

View file

@ -66,10 +66,14 @@ module Admin
def edit; end def edit; end
def delete def destroy
@user.destroy! if @user.destroy
flash[:notice] = "User #{@user.id} (#{@user.emai}) successfully deleted." redirect_to admin_users_path,
redirect_to admin_users_path notice: "User #{@user.id} (#{@user.email}) deleted."
else
redirect_to admin_users_path,
error: "User #{@user.id} (#{@user.emai}) could not be deleted. #{@user.full_messages.join(',')}"
end
end end
private private

View file

@ -12,8 +12,10 @@
.tab-content .tab-content
#user-info-content.tab-pane{class: "#{'active' unless params[:tab] == 'submissions-content'}"} #user-info-content.tab-pane{class: "#{'active' unless params[:tab] == 'submissions-content'}"}
- if can? :edit, @user - if can? :edit, @user
.pull-right .pull-right.btn-group
= link_to 'Edit', edit_admin_user_path(@user), class: 'btn btn-primary' = link_to 'Edit', edit_admin_user_path(@user), class: 'btn btn-primary'
= link_to 'Delete', admin_user_path(@user),method: :delete, class: 'btn btn-danger',
data: {confirm: "Are you sure?"}
%table.table %table.table
- @show_attributes.each do |attr| - @show_attributes.each do |attr|
%tr %tr

View file

@ -103,4 +103,21 @@ describe Admin::UsersController do
end end
end end
end end
describe 'DELETE #destroy' do
before do
delete :destroy, params: { id: user.id }
end
it 'redirects to admin users index path' do
expect(response).to redirect_to admin_users_path
end
it 'shows success message in flash notice' do
expect(flash[:notice]).to match("User #{user.id} (#{user.email}) deleted.")
end
it 'deletes the user' do
expect { user.reload }.to raise_error(ActiveRecord::RecordNotFound)
end
end
end end