-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
119 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,9 @@ | ||
<li> | ||
<%= gravatar_for user, :size => 30 %> | ||
<%= link_to user.name, user %> | ||
<% if current_user.admin? %> | ||
| | ||
<%= link_to "delete", user, :method => :delete, :confirm => "You sure?", | ||
:title => "Delete #{user.name}" %> | ||
<% end %> | ||
</li> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
class AddAdminToUsers < ActiveRecord::Migration | ||
def self.up | ||
add_column :users, :admin, :boolean, :default => false | ||
end | ||
|
||
def self.down | ||
remove_column :users, :admin | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,10 +4,11 @@ namespace :db do | |
desc "Fill database with sample data" | ||
task :populate => :environment do | ||
Rake::Task['db:reset'].invoke | ||
User.create!(:name => "Example User", | ||
:email => "[email protected]", | ||
:password => "foobar", | ||
:password_confirmation => "foobar") | ||
admin = User.create!(:name => "Example User", | ||
:email => "[email protected]", | ||
:password => "foobar", | ||
:password_confirmation => "foobar") | ||
admin.toggle!(:admin) | ||
99.times do |n| | ||
name = Faker::Name.name | ||
email = "example-#{n+1}@railstutorial.org" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -50,6 +50,21 @@ | |
response.should have_selector('a', :href => "/users?page=2", | ||
:content => "Next") | ||
end | ||
|
||
it "should have delete links for admins" do | ||
@user.toggle!(:admin) | ||
other_user = User.all.second | ||
get :index | ||
response.should have_selector('a', :href => user_path(other_user), | ||
:content => "delete") | ||
end | ||
|
||
it "should not have delete links for non-admins" do | ||
other_user = User.all.second | ||
get :index | ||
response.should_not have_selector('a', :href => user_path(other_user), | ||
:content => "delete") | ||
end | ||
end | ||
end | ||
|
||
|
@@ -269,6 +284,53 @@ | |
response.should redirect_to(root_path) | ||
end | ||
end | ||
end | ||
|
||
describe "DELETE 'destroy'" do | ||
|
||
before(:each) do | ||
@user = Factory(:user) | ||
end | ||
|
||
describe "as a non-signed-in user" do | ||
it "should deny access" do | ||
delete :destroy, :id => @user | ||
response.should redirect_to(signin_path) | ||
end | ||
end | ||
|
||
describe "as non-admin user" do | ||
it "should protect the action" do | ||
test_sign_in(@user) | ||
delete :destroy, :id => @user | ||
response.should redirect_to(root_path) | ||
end | ||
end | ||
|
||
describe "as an admin user" do | ||
|
||
before(:each) do | ||
@admin = Factory(:user, :email => "[email protected]", :admin => true) | ||
test_sign_in(@admin) | ||
end | ||
|
||
it "should destroy the user" do | ||
lambda do | ||
delete :destroy, :id => @user | ||
end.should change(User, :count).by(-1) | ||
end | ||
|
||
it "should redirect to the users page" do | ||
delete :destroy, :id => @user | ||
flash[:success].should =~ /destroyed/i | ||
response.should redirect_to(users_path) | ||
end | ||
|
||
it "should not be able to destroy itself" do | ||
lambda do | ||
delete :destroy, :id => @admin | ||
end.should_not change(User, :count) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters