Skip to content

Commit 13e33ed

Browse files
Finish user edit, update, index, and destroy actions
1 parent 6977e72 commit 13e33ed

File tree

22 files changed

+526
-108
lines changed

22 files changed

+526
-108
lines changed

.idea/.rakeTasks

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/sample_app.iml

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/workspace.xml

Lines changed: 189 additions & 92 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Gemfile

Lines changed: 3 additions & 0 deletions
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
# Bundle edge Rails instead:
811
# gem 'rails', :git => 'git://github.com/rails/rails.git'

Gemfile.lock

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ GEM
3333
arel (3.0.2)
3434
bcrypt-ruby (3.0.1-x86-mingw32)
3535
bootstrap-sass (2.0.0)
36+
bootstrap-will_paginate (0.0.6)
37+
will_paginate
3638
builder (3.0.4)
3739
capybara (1.1.2)
3840
mime-types (>= 1.16)
@@ -69,6 +71,8 @@ GEM
6971
factory_girl_rails (1.4.0)
7072
factory_girl (~> 2.3.0)
7173
railties (>= 3.0.0)
74+
faker (1.0.1)
75+
i18n (~> 0.4)
7276
ffi (1.1.5-x86-mingw32)
7377
gherkin (2.11.5-x86-mingw32)
7478
json (>= 1.4.6)
@@ -157,6 +161,7 @@ GEM
157161
uglifier (1.2.3)
158162
execjs (>= 0.3.0)
159163
multi_json (>= 1.0.2)
164+
will_paginate (3.0.3)
160165
xpath (0.1.4)
161166
nokogiri (~> 1.3)
162167

@@ -167,11 +172,13 @@ DEPENDENCIES
167172
annotate (~> 2.4.1.beta)
168173
bcrypt-ruby (= 3.0.1)
169174
bootstrap-sass (= 2.0.0)
175+
bootstrap-will_paginate (= 0.0.6)
170176
capybara (= 1.1.2)
171177
coffee-rails (= 3.2.2)
172178
cucumber-rails (= 1.2.1)
173179
database_cleaner (= 0.7.0)
174180
factory_girl_rails (= 1.4.0)
181+
faker (= 1.0.1)
175182
jquery-rails
176183
pg (= 0.12.2)
177184
rails (= 3.2.3)
@@ -180,3 +187,4 @@ DEPENDENCIES
180187
simplecov
181188
sqlite3 (= 1.3.5)
182189
uglifier (= 1.2.3)
190+
will_paginate (= 3.0.3)

app/assets/stylesheets/custom.css.scss

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,4 +167,19 @@ input, textarea, select, .uneditable-input {
167167
.field_with_errors {
168168
@extend .control-group;
169169
@extend .error;
170+
}
171+
172+
/* users index */
173+
174+
.users {
175+
list-style: none;
176+
margin: 0;
177+
li {
178+
overflow: auto;
179+
padding: 10px 0;
180+
border-top: 1px solid $grayLighter;
181+
&:last-child{
182+
border-bottom: 1px solid $grayLighter;
183+
}
184+
}
170185
}

app/controllers/sessions_controller.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ def create
88
user = User.find_by_email(params[:session][:email])
99
if user && user.authenticate(params[:session][:password])
1010
sign_in user
11-
redirect_to user
11+
redirect_back_or user
1212
else
1313
flash.now[:error] = 'Invalid email/password combination'
1414
render 'new'

app/controllers/users_controller.rb

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
11
class UsersController < ApplicationController
2+
before_filter :signed_in_user, only: [:index, :edit, :update, :destroy]
3+
before_filter :correct_user, only: [:edit, :update]
4+
before_filter :admin_user, only: :destroy
5+
6+
def index
7+
@users = User.paginate(page: params[:page])
8+
end
29

310
def show
411
@user = User.find(params[:id])
@@ -19,4 +26,42 @@ def create
1926
render 'new'
2027
end
2128
end
29+
30+
def edit
31+
32+
end
33+
34+
def update
35+
if @user.update_attributes(params[:user])
36+
flash[:success] = "Profile updated"
37+
sign_in @user
38+
redirect_to @user
39+
else
40+
render 'edit'
41+
end
42+
end
43+
44+
def destroy
45+
User.find(params[:id]).destroy
46+
flash[:success] = "User destroyed."
47+
redirect_to users_path
48+
end
49+
50+
private
51+
52+
def signed_in_user
53+
unless signed_in?
54+
store_location
55+
redirect_to signin_path, notice: "Please sign in."
56+
end
57+
end
58+
59+
def correct_user
60+
@user = User.find(params[:id])
61+
redirect_to(root_path) unless current_user?(@user)
62+
end
63+
64+
def admin_user
65+
redirect_to(root_path) unless current_user.admin?
66+
end
2267
end

app/helpers/sessions_helper.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,21 @@ def current_user
1717
@current_user ||= User.find_by_remember_token(cookies[:remember_token])
1818
end
1919

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

app/views/layouts/_header.html.erb

Lines changed: 2 additions & 2 deletions
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" %>

0 commit comments

Comments
 (0)