Skip to content

Commit 5fb1394

Browse files
committed
Finish user edit, update, index, and destroy actions
1 parent 2b9f01e commit 5fb1394

File tree

15 files changed

+100
-13
lines changed

15 files changed

+100
-13
lines changed

Gemfile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ gem 'rails', '4.2.6'
66
gem 'bcrypt', '3.1.7'
77
gem 'bootstrap-sass', '3.2.0.0'
88
gem 'puma', '3.1.0'
9+
gem 'faker', '1.4.2'
10+
gem 'will_paginate', '3.0.7'
11+
gem 'bootstrap-will_paginate', '0.0.10'
912
# Use sqlite3 as the database for Active Record
1013
gem 'sqlite3'
1114
# Use SCSS for stylesheets

Gemfile.lock

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ GEM
4242
debug_inspector (>= 0.0.1)
4343
bootstrap-sass (3.2.0.0)
4444
sass (~> 3.2)
45+
bootstrap-will_paginate (0.0.10)
46+
will_paginate
4547
builder (3.2.2)
4648
byebug (9.0.5)
4749
coffee-rails (4.1.1)
@@ -55,6 +57,8 @@ GEM
5557
debug_inspector (0.0.2)
5658
erubis (2.7.0)
5759
execjs (2.7.0)
60+
faker (1.4.2)
61+
i18n (~> 0.5)
5862
globalid (0.3.7)
5963
activesupport (>= 4.1.0)
6064
i18n (0.7.0)
@@ -145,15 +149,18 @@ GEM
145149
binding_of_caller (>= 0.7.2)
146150
railties (>= 4.0)
147151
sprockets-rails (>= 2.0, < 4.0)
152+
will_paginate (3.0.7)
148153

149154
PLATFORMS
150155
ruby
151156

152157
DEPENDENCIES
153158
bcrypt (= 3.1.7)
154159
bootstrap-sass (= 3.2.0.0)
160+
bootstrap-will_paginate (= 0.0.10)
155161
byebug
156162
coffee-rails (~> 4.1.0)
163+
faker (= 1.4.2)
157164
jbuilder (~> 2.0)
158165
jquery-rails
159166
puma (= 3.1.0)
@@ -165,6 +172,7 @@ DEPENDENCIES
165172
turbolinks
166173
uglifier (>= 1.3.0)
167174
web-console (~> 2.0)
175+
will_paginate (= 3.0.7)
168176

169177
BUNDLED WITH
170178
1.12.5

app/assets/stylesheets/custom.css.scss

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,15 @@
22
@import "bootstrap";
33

44
$gray-medium-light: #eaeaea;
5+
.users {
6+
list-style: none;
7+
margin: 0;
8+
li {
9+
overflow: auto;
10+
padding: 10px 0;
11+
border-bottom: 1px solid $gray-lighter;
12+
}
13+
}
514
.checkbox {
615
margin-top: -10px;
716
margin-bottom: 10px;

app/controllers/sessions_controller.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ def create
66
if user && user.authenticate(params[:session][:password])
77
log_in user
88
params[:session][:remember_me] == '1' ? remember(user) : forget(user)
9-
redirect_to user
9+
redirect_back_or user
1010
else
1111
flash.now[:danger] = 'Invalid email/password combination' # Not quite right!
1212
render 'new'

app/controllers/users_controller.rb

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,18 @@
11
class UsersController < ApplicationController
2-
before_action :logged_in_user, only: [:edit, :update]
2+
before_action :logged_in_user, only: [:index, :edit, :update, :destroy]
3+
before_action :correct_user, only: [:edit, :update]
4+
before_action :admin_user, only: :destroy
5+
def admin_user
6+
redirect_to(root_url) unless current_user.admin?
7+
end
8+
def index
9+
@users = User.paginate(page: params[:page])
10+
end
11+
def destroy
12+
User.find(params[:id]).destroy
13+
flash[:success] = "User deleted"
14+
redirect_to users_url
15+
end
316
def show
417
@user = User.find(params[:id])
518
# debugger
@@ -36,8 +49,13 @@ def user_params
3649
end
3750
def logged_in_user
3851
unless logged_in?
52+
store_location
3953
flash[:danger] = "Please log in."
4054
redirect_to login_url
4155
end
4256
end
57+
def correct_user
58+
@user = User.find(params[:id])
59+
redirect_to(root_url) unless current_user?(@user)
60+
end
4361
end

app/helpers/sessions_helper.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ def log_out
1212
session.delete(:user_id)
1313
@current_user = nil
1414
end
15+
def current_user?(user)
16+
user == current_user
17+
end
1518
def remember(user)
1619
user.remember
1720
cookies.permanent.signed[:user_id] = user.id
@@ -39,4 +42,13 @@ def log_out
3942
session.delete(:user_id)
4043
@current_user = nil
4144
end
45+
def redirect_back_or(default)
46+
redirect_to(session[:forwarding_url] || default)
47+
session.delete(:forwarding_url)
48+
end
49+
50+
# Stores the URL trying to be accessed.
51+
def store_location
52+
session[:forwarding_url] = request.url if request.get?
53+
end
4254
end

app/helpers/users_helper.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
module UsersHelper
2-
def gravatar_for(user)
2+
def gravatar_for(user, options = { size: 50 })
33
gravatar_id = Digest::MD5::hexdigest(user.email.downcase)
44
gravatar_url = "https://secure.gravatar.com/avatar/#{gravatar_id}"
55
image_tag(gravatar_url, alt: user.name, class: "gravatar")

app/views/layouts/_header.html.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<li><%= link_to "Home", root_path %></li>
77
<li><%= link_to "Help", help_path %></li>
88
<% if logged_in? %>
9-
<li><%= link_to "Users", '#' %></li>
9+
<li><%= link_to "Users", users_path %></li>
1010
<li class="dropdown">
1111
<a href="#" class="dropdown-toggle" data-toggle="dropdown">
1212
Account <b class="caret"></b>

app/views/users/_user.html.erb

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

app/views/users/index.html.erb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<% provide(:title, 'All users') %>
2+
<h1>All users</h1>
3+
<%= will_paginate %>
4+
<ul class="users">
5+
<%= render @users %>
6+
<!-- <% @users.each do |user| %>
7+
<li>
8+
<%= gravatar_for user, size: 50 %>
9+
<%= link_to user.name, user %>
10+
</li>
11+
<% end %> -->
12+
</ul>
13+
<%= will_paginate %>

config/routes.rb

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,5 @@
11
Rails.application.routes.draw do
2-
resources :users
3-
get 'users/new'
4-
5-
get 'users/new'
6-
7-
get 'users/new'
8-
2+
93
get 'users/new'
104

115
root 'static_pages#home'
@@ -16,7 +10,7 @@
1610
get 'login' => 'sessions#new'
1711
post 'login' => 'sessions#create'
1812
delete 'logout' => 'sessions#destroy'
19-
13+
resources :users
2014

2115
# The priority is based upon order of creation: first created -> highest priority.
2216
# See how all your routes lay out with "rake routes".
Lines changed: 5 additions & 0 deletions
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
4+
end
5+
end

db/schema.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
#
1212
# It's strongly recommended that you check this file into your version control system.
1313

14-
ActiveRecord::Schema.define(version: 20160920063233) do
14+
ActiveRecord::Schema.define(version: 20160921080444) do
1515

1616
create_table "users", force: :cascade do |t|
1717
t.string "name"
@@ -20,6 +20,7 @@
2020
t.datetime "updated_at", null: false
2121
t.string "password_digest"
2222
t.string "remember_digest"
23+
t.boolean "admin"
2324
end
2425

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

db/seeds.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,18 @@
55
#
66
# cities = City.create([{ name: 'Chicago' }, { name: 'Copenhagen' }])
77
# Mayor.create(name: 'Emanuel', city: cities.first)
8+
User.create!(name: "Example User",
9+
10+
password: "foobar",
11+
password_confirmation: "foobar",
12+
admin: true)
13+
14+
99.times do |n|
15+
name = Faker::Name.name
16+
email = "example-#{n+1}@railstutorial.org"
17+
password = "password"
18+
User.create!(name: name,
19+
email: email,
20+
password: password,
21+
password_confirmation: password)
22+
end

test/fixtures/users.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@ michael:
22
name: Michael Example
33
44
password_digest: <%= User.digest('password') %>
5+
admin: true
56
#empty

0 commit comments

Comments
 (0)