Skip to content

Commit ae7bde8

Browse files
committed
Finish user edit, update, index, and destroy action
1 parent 4782d24 commit ae7bde8

File tree

19 files changed

+312
-19
lines changed

19 files changed

+312
-19
lines changed

Gemfile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ ruby "3.4.3"
66
gem "rails", "8.0.2"
77
gem "jsbundling-rails", "1.3.1"
88
gem "bcrypt", "3.1.13"
9+
gem "faker", "3.5.2"
10+
gem "will_paginate", "4.0.1"
911
gem "cssbundling-rails", "1.4.3"
1012
gem "propshaft", "1.2.1"
1113
gem "importmap-rails", "2.1.0"

Gemfile.lock

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,8 @@ GEM
108108
ed25519 (1.4.0)
109109
erb (5.0.2)
110110
erubi (1.13.1)
111+
faker (3.5.2)
112+
i18n (>= 1.8.11, < 2)
111113
ffi (1.17.2-aarch64-linux-gnu)
112114
ffi (1.17.2-aarch64-linux-musl)
113115
ffi (1.17.2-arm-linux-gnu)
@@ -336,6 +338,7 @@ GEM
336338
base64
337339
websocket-extensions (>= 0.1.0)
338340
websocket-extensions (0.1.5)
341+
will_paginate (4.0.1)
339342
xpath (3.2.0)
340343
nokogiri (~> 1.8)
341344
zeitwerk (2.7.3)
@@ -355,6 +358,7 @@ DEPENDENCIES
355358
capybara (= 3.40.0)
356359
cssbundling-rails (= 1.4.3)
357360
debug (= 1.11.0)
361+
faker (= 3.5.2)
358362
guard (= 2.19.1)
359363
guard-minitest (= 2.4.6)
360364
importmap-rails (= 2.1.0)
@@ -376,6 +380,7 @@ DEPENDENCIES
376380
tzinfo-data (= 1.2025.2)
377381
web-console (= 4.2.1)
378382
webdrivers (= 5.3.1)
383+
will_paginate (= 4.0.1)
379384

380385
RUBY VERSION
381386
ruby 3.4.3p32

app/assets/stylesheets/application.bootstrap.scss

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,3 +204,14 @@ input {
204204
width: auto;
205205
margin-left: 0;
206206
}
207+
208+
/* Users index */
209+
.users {
210+
list-style: none;
211+
margin: 0;
212+
li {
213+
overflow: auto;
214+
padding: 10px 0;
215+
border-bottom: 1px solid $gray-500;
216+
}
217+
}

app/controllers/sessions_controller.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,11 @@ def new
55
def create
66
user = User.find_by(email: params[:session][:email].downcase)
77
if user && user.authenticate(params[:session][:password])
8+
forwarding_url = session[:forwarding_url]
89
reset_session
910
params[:session][:remember_me] == "1" ? remember(user) : forget(user)
1011
log_in user
11-
redirect_to user
12+
redirect_to forwarding_url || user
1213
else
1314
flash.now[:danger] = "Invalid email/password combination"
1415
render "new", status: :unprocessable_entity

app/controllers/users_controller.rb

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
class UsersController < ApplicationController
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+
26
def show
37
@user = User.find(params[:id])
48
end
@@ -19,10 +23,56 @@ def create
1923
end
2024
end
2125

26+
def edit
27+
# @user = User.find(params[:id])
28+
end
29+
30+
def update
31+
if @user.update(user_params)
32+
flash[:success] = "Profile updated"
33+
redirect_to @user
34+
else
35+
render "edit", status: :unprocessable_entity
36+
end
37+
end
38+
39+
def show
40+
@user = User.find(params[:id])
41+
end
42+
43+
def index
44+
@users = User.paginate(page: params[:page])
45+
end
46+
47+
def destroy
48+
User.find(params[:id]).destroy
49+
flash[:success] = "User deleted"
50+
redirect_to users_url, status: :see_other
51+
end
52+
2253
private
2354

2455
def user_params
2556
params.require(:user).permit(:name, :email, :password,
2657
:password_confirmation)
2758
end
59+
60+
def logged_in_user
61+
unless logged_in?
62+
store_location
63+
flash[:danger] = "Please log in."
64+
redirect_to login_url, status: :see_other
65+
end
66+
end
67+
68+
# Confirms the correct user.
69+
def correct_user
70+
@user = User.find(params[:id])
71+
redirect_to(root_url, status: :see_other) unless current_user?(@user)
72+
end
73+
74+
# Confirms an admin user.
75+
def admin_user
76+
redirect_to(root_url, status: :see_other) unless current_user.admin?
77+
end
2878
end

app/helpers/sessions_helper.rb

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,11 @@ def current_user
3131
end
3232
end
3333

34+
# Returns true if the given user is the current user.
35+
def current_user?(user)
36+
user && user == current_user
37+
end
38+
3439
# Returns true if the user is logged in, false otherwise.
3540
def logged_in?
3641
!current_user.nil?
@@ -49,4 +54,9 @@ def log_out
4954
reset_session
5055
@current_user = nil
5156
end
52-
end
57+
58+
# Stores the URL trying to be accessed.
59+
def store_location
60+
session[:forwarding_url] = request.original_url if request.get?
61+
end
62+
end

app/helpers/users_helper.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
module UsersHelper
22
# Returns the Gravatar for the given user.
33
# Returns the Gravatar for the given user.
4-
def gravatar_for(user, size: 80)
4+
def gravatar_for(user, options = { size: 80 })
5+
size = options[:size]
56
gravatar_id = Digest::MD5::hexdigest(user.email.downcase)
67
gravatar_url = "https://secure.gravatar.com/avatar/#{gravatar_id}?s=#{size}"
78
image_tag(gravatar_url, alt: user.name, class: "gravatar")

app/models/user.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ class User < ApplicationRecord
77
format: { with: VALID_EMAIL_REGEX },
88
uniqueness: true
99
has_secure_password
10-
validates :password, presence: true, length: { minimum: 6 }
10+
validates :password, presence: true, length: { minimum: 6 }, allow_nil: true
1111
# Returns the hash digest of the given string.
1212
def User.digest(string)
1313
cost = ActiveModel::SecurePassword.min_cost ? BCrypt::Engine::MIN_COST :

app/views/layouts/_header.html.erb

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNavDropdown" aria-controls="navbarNavDropdown" aria-expanded="false" aria-label="Toggle navigation">
66
<span class="navbar-toggler-icon"></span>
77
</button>
8+
<span class="visually-hidden">Toggle navigation</span>
89
<div class="collapse navbar-collapse" id="navbarNavDropdown">
9-
1010
<ul class="navbar-nav ms-auto">
1111
<li class="nav-item">
1212
<%= link_to "Home", root_path, class: "nav-link" %>
@@ -15,15 +15,14 @@
1515
<%= link_to "Help", help_path, class: "nav-link" %>
1616
</li>
1717
<% if logged_in? %>
18-
<li><%= link_to "Users", '#', class: "nav-link" %></li>
18+
<li><%= link_to "Users", users_path, class: "nav-link" %></li>
1919
<li class="dropdown nav-link">
20-
2120
<a href="#" id="account" class="dropdown-toggle" data-bs-toggle="dropdown">
2221
Account <b class="caret"></b>
2322
</a>
2423
<ul id="dropdown-menu" class="dropdown-menu">
2524
<li><%= link_to "Profile", current_user %></li>
26-
<li><%= link_to "Settings", '#' %></li>
25+
<li><%= link_to "Settings", edit_user_path(current_user) %></li>
2726
<li class="divider"></li>
2827
<li>
2928
<%= link_to "Log out", logout_path,

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, data: { "turbo-method": :delete,
6+
turbo_confirm: "You sure?" } %>
7+
<% end %>
8+
</li>

0 commit comments

Comments
 (0)