Skip to content

Commit

Permalink
Section 7.4.9: Add feature for deleting users, including protection a…
Browse files Browse the repository at this point in the history
…gainst self-deletion
  • Loading branch information
Ryan Bigg committed Dec 3, 2014
1 parent 01871d9 commit 924114f
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 1 deletion.
11 changes: 11 additions & 0 deletions ticketee/app/controllers/admin/users_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,17 @@ def update
end
end

def destroy
if @user == current_user
flash[:alert] = "You cannot delete yourself!"
else
@user.destroy
flash[:notice] = "User has been deleted."
end

redirect_to admin_users_path
end

private

def set_user
Expand Down
5 changes: 4 additions & 1 deletion ticketee/app/views/admin/users/show.html.erb
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
<h2><%= @user %></h2>

<%= link_to "Edit User", edit_admin_user_path(@user) %>
<%= link_to "Edit User", edit_admin_user_path(@user), class: "edit" %>
<%= link_to "Delete User", admin_user_path(@user), method: :delete,
data: { confirm: "Are you sure you want to delete this user?"},
class: "delete" %>
28 changes: 28 additions & 0 deletions ticketee/spec/features/admin/deleting_users_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
require "rails_helper"

feature "Deleting users" do
let!(:admin_user) { FactoryGirl.create(:admin_user) }
let!(:user) { FactoryGirl.create(:user) }

before do
login_as(admin_user)
visit "/"

click_link "Admin"
click_link "Users"
end

scenario "Deleting a user" do
click_link user.email
click_link "Delete User"

expect(page).to have_content("User has been deleted")
end

scenario "Users cannot delete themselves" do
click_link admin_user.email
click_link "Delete User"

expect(page).to have_content("You cannot delete yourself!")
end
end

0 comments on commit 924114f

Please sign in to comment.