Skip to content

Commit 28d413b

Browse files
committed
Finish user edit, update, index, and destroy actions
1 parent b687ddd commit 28d413b

19 files changed

+365
-13
lines changed

Gemfile

+3
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ source 'https://rubygems.org'
33
gem 'rails', '3.2.3'
44
gem 'bootstrap-sass', '2.0.0'
55
gem 'bcrypt-ruby', '3.0.1'
6+
gem 'faker', '1.0.1'
7+
gem 'will_paginate', '3.0.3'
8+
gem 'bootstrap-will_paginate', '0.0.6'
69

710
group :development, :test do
811
gem 'rspec-rails', '2.9.0'

Gemfile.lock

+8
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ GEM
3232
arel (3.0.3)
3333
bcrypt-ruby (3.0.1)
3434
bootstrap-sass (2.0.0)
35+
bootstrap-will_paginate (0.0.6)
36+
will_paginate
3537
builder (3.0.4)
3638
capybara (1.1.2)
3739
mime-types (>= 1.16)
@@ -63,6 +65,8 @@ GEM
6365
factory_girl_rails (1.4.0)
6466
factory_girl (~> 2.3.0)
6567
railties (>= 3.0.0)
68+
faker (1.0.1)
69+
i18n (~> 0.4)
6670
ffi (1.9.3)
6771
formatador (0.2.5)
6872
guard (1.7.0)
@@ -169,6 +173,7 @@ GEM
169173
warden (1.2.3)
170174
rack (>= 1.0)
171175
websocket (1.0.7)
176+
will_paginate (3.0.3)
172177
xpath (0.1.4)
173178
nokogiri (~> 1.3)
174179

@@ -179,10 +184,12 @@ DEPENDENCIES
179184
annotate (~> 2.4.1.beta)
180185
bcrypt-ruby (= 3.0.1)
181186
bootstrap-sass (= 2.0.0)
187+
bootstrap-will_paginate (= 0.0.6)
182188
capybara (= 1.1.2)
183189
coffee-rails (~> 3.2.2)
184190
devise
185191
factory_girl_rails (= 1.4.0)
192+
faker (= 1.0.1)
186193
guard-rspec (= 0.5.5)
187194
jquery-rails (~> 2.0.0)
188195
libnotify (= 0.5.9)
@@ -193,3 +200,4 @@ DEPENDENCIES
193200
sass-rails (~> 3.2.4)
194201
sqlite3 (= 1.3.5)
195202
uglifier (= 1.2.3)
203+
will_paginate (= 3.0.3)

app/assets/stylesheets/custom.css.scss

+15
Original file line numberDiff line numberDiff line change
@@ -175,4 +175,19 @@ input, textarea, select, .uneditable-input {
175175
.field_with_errors {
176176
@extend .control-group;
177177
@extend .error;
178+
}
179+
180+
/* users index */
181+
182+
.users {
183+
list-style: none;
184+
margin: 0;
185+
li {
186+
overflow: auto;
187+
padding: 10px 0;
188+
border-top: 1px solid $grayLighter;
189+
&:last-child {
190+
border-bottom: 1px solid $grayLighter;
191+
}
192+
}
178193
}

app/controllers/sessions_controller.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ def create
77
user = User.find_by_email(params[:session][:email])
88
if user && user.authenticate(params[:session][:password])
99
sign_in user
10-
redirect_to user
10+
redirect_back_or user
1111
else
1212
flash.now[:error] = 'Invalid email/password combination' #Not quite right!
1313
render 'new'

app/controllers/users_controller.rb

+51
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
class UsersController < ApplicationController
2+
before_filter :signed_in_user, only: [:index, :edit, :update]
3+
before_filter :correct_user, only: [:edit, :update]
4+
before_filter :admin_user, only: :destroy
25

36
def show
47
@user = User.find(params[:id])
@@ -18,4 +21,52 @@ def create
1821
render 'new'
1922
end
2023
end
24+
25+
def edit
26+
@user = User.find(params[:id])
27+
if @user.update_attributes(params[:user])
28+
# Handle a successful update.
29+
else
30+
render 'edit'
31+
end
32+
end
33+
34+
def update
35+
@user = User.find(params[:id])
36+
if @user.update_attributes(params[:user])
37+
flash[:success] = "Profile updated"
38+
sign_in @user
39+
redirect_to @user
40+
else
41+
render 'edit'
42+
end
43+
end
44+
45+
def destroy
46+
User.find(params[:id]).destroy
47+
flash[:success] = "User destroyed."
48+
redirect_to users_path
49+
end
50+
51+
def index
52+
@users = User.paginate(page: params[:page])
53+
end
54+
55+
private
56+
57+
def signed_in_user
58+
unless signed_in?
59+
store_location
60+
redirect_to signin_path, notice: "Please sign in."
61+
end
62+
end
63+
64+
def correct_user
65+
@user = User.find(params[:id])
66+
redirect_to(root_path) unless current_user?(@user)
67+
end
68+
69+
def admin_user
70+
redirect_to(root_path) unless current_user.admin?
71+
end
2172
end

app/helpers/sessions_helper.rb

+13
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,21 @@ def current_user
1616
@current_user ||= User.find_by_remember_token(cookies[:remember_token])
1717
end
1818

19+
def current_user?(user)
20+
@user == current_user
21+
end
22+
1923
def sign_out
2024
self.current_user = nil
2125
cookies.delete(:remember_token)
2226
end
27+
28+
def redirect_back_or(default)
29+
redirect_to(session[:return_to] || default)
30+
session.delete(:return_to)
31+
end
32+
33+
def store_location
34+
session[:return_to] = request.fullpath
35+
end
2336
end

app/helpers/users_helper.rb

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
module UsersHelper
22

33
# Returns the Gravatar (http://gravatar.com/) for the given user.
4-
def gravatar_for(user)
4+
def gravatar_for(user, options = { size: 50 })
55
gravatar_id = Digest::MD5::hexdigest(user.email.downcase)
6-
gravatar_url = "https://secure.gravatar.com/avatars/#{gravatar_id}.png"
6+
size = options[:size]
7+
gravatar_url = "https://secure.gravatar.com/avatars/#{gravatar_id}.png?s=#{size}"
78
image_tag(gravatar_url, alt: user.name, class: "gravatar")
89
end
910
end

app/views/layouts/_header.html.erb

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@
77
<li><%= link_to "Home", root_path %></li>
88
<li><%= link_to "Help", help_path %></li>
99
<% if signed_in? %>
10-
<li><%= link_to "Users", '#' %></li>
10+
<li><%= link_to "Users", users_path %></li>
1111
<li id="fat-menu" class="dropdown">
1212
<a href="#" class="dropdown-toggle" data-toggle="dropdown">
1313
Account <b class="caret"></b>
1414
</a>
1515
<ul class="dropdown-menu">
1616
<li><%= link_to "Profile", current_user %></li>
17-
<li><%= link_to "Settings", '#' %></li>
17+
<li><%= link_to "Settings", edit_user_path(current_user) %></li>
1818
<li class="divider"></li>
1919
<li>
2020
<%= link_to "Sign out", signout_path, method: "delete" %>

app/views/users/_user.html.erb

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<li>
2+
<%= gravatar_for user, size: 52 %>
3+
<%= link_to user.name, user %>
4+
<% if current_user.admin? && !current_user?(user) %>
5+
<%= link_to "delete", user, method: :delete, confirm: "You sure?" %>
6+
<% end %>
7+
</li>

app/views/users/edit.html.erb

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<% provide(:title, "Edit user") %>
2+
<h1>Update your profile</h1>
3+
4+
<div class="row">
5+
<div class="span6 offset3">
6+
<%= form_for(@user) do |f| %>
7+
<%= render 'shared/error_messages' %>
8+
9+
<%= f.label :name %>
10+
<%= f.text_field :name %>
11+
12+
<%= f.label :email %>
13+
<%= f.text_field :email %>
14+
15+
<%= f.label :password %>
16+
<%= f.password_field :password %>
17+
18+
<%= f.label :password_confirmation, "Confirm Password" %>
19+
<%= f.password_field :password_confirmation %>
20+
21+
<%= f.submit "Save changes", class: "btn btn-large btn-primary" %>
22+
<% end %>
23+
24+
<%= gravatar_for @user %>
25+
<a href="http://gravatar.com/emails">change</a>
26+
</div>
27+
</div>

app/views/users/index.html.erb

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<% provide(:title, 'All users') %>
2+
<h1>All users</h1>
3+
4+
<%= will_paginate %>
5+
6+
<ul class="users">
7+
<%= render @users %>
8+
</ul>
9+
10+
<%= will_paginate %>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class AddAdminToUsers < ActiveRecord::Migration
2+
def change
3+
add_column :users, :admin, :boolean, default: false
4+
end
5+
end

db/schema.rb

+4-3
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,16 @@
1111
#
1212
# It's strongly recommended to check this file into your version control system.
1313

14-
ActiveRecord::Schema.define(:version => 20140710000649) do
14+
ActiveRecord::Schema.define(:version => 20140723211927) do
1515

1616
create_table "users", :force => true do |t|
1717
t.string "name"
1818
t.string "email"
19-
t.datetime "created_at", :null => false
20-
t.datetime "updated_at", :null => false
19+
t.datetime "created_at", :null => false
20+
t.datetime "updated_at", :null => false
2121
t.string "password_digest"
2222
t.string "remember_token"
23+
t.boolean "admin", :default => false
2324
end
2425

2526
add_index "users", ["email"], :name => "index_users_on_email", :unique => true

lib/tasks/sample_data.rake

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
namespace :db do
2+
desc "Fill database with sample data"
3+
task populate: :environment do
4+
admin = User.create(name: "Example User",
5+
6+
password: "foobar",
7+
password_confirmation: "foobar")
8+
admin.toggle!(:admin)
9+
10+
99.time do |n|
11+
name = Faker::Name.name
12+
email = "example-#{n+1}@railstutorial.org"
13+
password = "password"
14+
User.create!(name: name,
15+
email: email,
16+
password: password,
17+
password_confirmation: password)
18+
end
19+
end
20+
end

spec/factories.rb

+7-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
FactoryGirl.define do
22
factory :user do
3-
name "Michael Hartl"
4-
email "michael@example.com"
3+
sequence(:name) { |n| "Person #{n}" }
4+
sequence(:email) { |n| "person_#{n}@example.com" }
55
password "foobar"
66
password_confirmation "foobar"
7+
8+
factory :admin do
9+
admin true
10+
end
711
end
8-
end
12+
end

spec/models/user_spec.rb

+9
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,17 @@
2424
it { should respond_to(:password) }
2525
it { should respond_to(:password_confirmation) }
2626
it { should respond_to(:remember_token) }
27+
it { should respond_to(:admin) }
2728
it { should respond_to(:authenticate) }
29+
2830
it { should be_valid }
31+
it { should_not be_admin }
32+
33+
describe "with admin attribute set to 'true'" do
34+
before { @user.toggle!(:admin) }
35+
36+
it { should be_admin }
37+
end
2938

3039
describe "when name is not present" do
3140
before { @user.name = " " }

0 commit comments

Comments
 (0)