-
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.
Finish user edit, update, index and destroy actions
- Loading branch information
Showing
19 changed files
with
304 additions
and
11 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
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,9 +1,10 @@ | ||
module UsersHelper | ||
|
||
# Returns the Gravatar for the given user. | ||
def gravatar_for(user) | ||
def gravatar_for(user, options = { size: 80 }) | ||
gravatar_id = Digest::MD5::hexdigest(user.email.downcase) | ||
gravatar_url = "https://secure.gravatar.com/avatar/#{gravatar_id}" | ||
size = options[:size] | ||
gravatar_url = "https://secure.gravatar.com/avatar/#{gravatar_id}?s=#{size}" | ||
image_tag(gravatar_url, alt: user.name, class: "gravatar") | ||
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
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,8 @@ | ||
<li> | ||
<%= gravatar_for user, size: 50 %> | ||
<%= link_to user.name, user %> | ||
<% if current_user.admin? && !current_user?(user) %> | ||
| <%= link_to "delete", user, method: :delete, | ||
data: { confirm: "You sure?" } %> | ||
<% 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,29 @@ | ||
<% provide(:title, "Edit user") %> | ||
<h1>Update your profile</h1> | ||
|
||
<div class="row"> | ||
<div class="col-md-6 col-md-offset-3"> | ||
<%= form_for(@user) do |f| %> | ||
<%= render 'shared/error_messages' %> | ||
|
||
<%= f.label :name %> | ||
<%= f.text_field :name, class: 'form-control' %> | ||
|
||
<%= f.label :email %> | ||
<%= f.text_field :email, class: 'form-control' %> | ||
|
||
<%= f.label :password %> | ||
<%= f.password_field :password, class: 'form-control' %> | ||
|
||
<%= f.label :password_confirmation, "Confirmation" %> | ||
<%= f.password_field :password_confirmation, class: 'form-control' %> | ||
|
||
<%= f.submit "Save changes", class: "btn btn-primary" %> | ||
<% end %> | ||
|
||
<div class="gravatar_edit"> | ||
<%= gravatar_for @user %> | ||
<a href="http://gravatar.com/emails">change</a> | ||
</div> | ||
</div> | ||
</div> |
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,10 @@ | ||
<% provide(:title, 'All users') %> | ||
<h1>All users</h1> | ||
|
||
<%= will_paginate %> | ||
|
||
<ul class="users"> | ||
<%= render @users %> | ||
</ul> | ||
|
||
<%= will_paginate %> |
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,5 @@ | ||
class AddAdminToUsers < ActiveRecord::Migration | ||
def change | ||
add_column :users, :admin, :boolean, default: false | ||
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 |
---|---|---|
|
@@ -5,3 +5,18 @@ | |
# | ||
# cities = City.create([{ name: 'Chicago' }, { name: 'Copenhagen' }]) | ||
# Mayor.create(name: 'Emanuel', city: cities.first) | ||
User.create!(name: "Example User", | ||
email: "[email protected]", | ||
password: "foobar", | ||
password_confirmation: "foobar", | ||
admin: true) | ||
|
||
99.times do |n| | ||
name = Faker::Name.name | ||
email = "example-#{n+1}@railstutorial.org" | ||
password = "password" | ||
User.create!(name: name, | ||
email: email, | ||
password: password, | ||
password_confirmation: password) | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,56 @@ | ||
require 'test_helper' | ||
|
||
class UsersControllerTest < ActionController::TestCase | ||
|
||
def setup | ||
@user = users(:michael) | ||
@other_user = users(:archer) | ||
end | ||
|
||
test "should get new" do | ||
get :new | ||
assert_response :success | ||
assert_select "title", "Sign up | Ruby on Rails Tutorial Sample App" | ||
end | ||
|
||
end | ||
test "should redirect index when not logged in" do | ||
get :index | ||
assert_redirected_to login_url | ||
end | ||
|
||
test "should redirect edit when not logged in" do | ||
get :edit, id: @user | ||
assert_redirected_to login_url | ||
end | ||
|
||
test "should redirect update when not logged in" do | ||
patch :update, id: @user, user: { name: @user.name, email: @user.email } | ||
assert_redirected_to login_url | ||
end | ||
|
||
test "should redirect edit when logged in as wrong user" do | ||
log_in_as(@other_user) | ||
get :edit, id: @user | ||
assert_redirected_to root_url | ||
end | ||
|
||
test "should redirect update when logged in as wrong user" do | ||
log_in_as(@other_user) | ||
patch :update, id: @user, user: { name: @user.name, email: @user.email } | ||
assert_redirected_to root_url | ||
end | ||
|
||
test "should redirect destroy when not logged in" do | ||
assert_no_difference 'User.count' do | ||
delete :destroy, id: @user | ||
end | ||
assert_redirected_to login_url | ||
end | ||
|
||
test "should redirect destroy when logged in as a non-admin" do | ||
log_in_as(@other_user) | ||
assert_no_difference 'User.count' do | ||
delete :destroy, id: @user | ||
end | ||
assert_redirected_to root_url | ||
end | ||
end |
Oops, something went wrong.