Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
S
sample_app
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Mai Hoang Thai Ha
sample_app
Commits
62a1e377
Commit
62a1e377
authored
Jun 15, 2021
by
Mai Hoang Thai Ha
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Finish user edit, update, index and destroy actions
parent
a11f9f2d
Pipeline
#1270
failed with stages
in 0 seconds
Changes
9
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
9 changed files
with
96 additions
and
9 deletions
+96
-9
app/controllers/users_controller.rb
+12
-2
app/views/users/_user.html.erb
+9
-0
app/views/users/index.html.erb
+2
-5
db/migrate/20210615072636_add_admin_to_users.rb
+5
-0
db/schema.rb
+2
-1
db/seeds.rb
+2
-1
test/controllers/users_controller_test.rb
+15
-0
test/fixtures/users.yml
+18
-0
test/integration/users_index_test.rb
+31
-0
No files found.
app/controllers/users_controller.rb
View file @
62a1e377
class
UsersController
<
ApplicationController
before_action
:logged_in_user
,
only:
[
:index
,
:edit
,
:update
]
before_action
:logged_in_user
,
only:
[
:index
,
:edit
,
:update
,
:destroy
]
before_action
:correct_user
,
only:
[
:edit
,
:update
]
before_action
:admin_user
,
only: :destroy
def
index
@users
=
User
.
paginate
(
page:
params
[
:page
])
# @users = User.all
end
def
show
...
...
@@ -41,6 +41,12 @@ class UsersController < ApplicationController
end
end
def
destroy
User
.
find
(
params
[
:id
]).
destroy
flash
[
:success
]
=
"User deleted"
redirect_to
users_url
end
private
def
user_params
...
...
@@ -63,4 +69,8 @@ class UsersController < ApplicationController
@user
=
User
.
find
(
params
[
:id
])
redirect_to
(
root_url
)
unless
current_user?
(
@user
)
end
def
admin_user
redirect_to
(
root_url
)
unless
current_user
.
admin?
end
end
app/views/users/_user.html.erb
0 → 100644
View file @
62a1e377
<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>
\ No newline at end of file
app/views/users/index.html.erb
View file @
62a1e377
...
...
@@ -4,11 +4,8 @@
<%=
will_paginate
%>
<ul
class=
"users"
>
<%
@users
.
each
do
|
user
|
%>
<li>
<%=
gravatar_for
user
,
size:
50
%>
<%=
link_to
user
.
name
,
user
%>
</li>
<%
@users
.
each
do
|
user
|
%>
<%=
render
@users
%>
<%
end
%>
</ul>
...
...
db/migrate/20210615072636_add_admin_to_users.rb
0 → 100644
View file @
62a1e377
class
AddAdminToUsers
<
ActiveRecord
::
Migration
[
6.1
]
def
change
add_column
:users
,
:admin
,
:boolean
,
default:
false
end
end
db/schema.rb
View file @
62a1e377
...
...
@@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord
::
Schema
.
define
(
version:
2021_06_1
1_030107
)
do
ActiveRecord
::
Schema
.
define
(
version:
2021_06_1
5_072636
)
do
create_table
"users"
,
force: :cascade
do
|
t
|
t
.
string
"name"
...
...
@@ -19,6 +19,7 @@ ActiveRecord::Schema.define(version: 2021_06_11_030107) do
t
.
datetime
"updated_at"
,
precision:
6
,
null:
false
t
.
string
"password_digest"
t
.
string
"remember_digest"
t
.
boolean
"admin"
,
default:
false
t
.
index
[
"email"
],
name:
"index_users_on_email"
,
unique:
true
end
...
...
db/seeds.rb
View file @
62a1e377
User
.
create!
(
name:
"Example User"
,
email:
"example@railstutorial.org"
,
password:
"foobar"
,
password_confirmation:
"foobar"
)
password_confirmation:
"foobar"
,
admin:
true
)
99
.
times
do
|
num
|
name
=
Faker
::
Name
.
name
...
...
test/controllers/users_controller_test.rb
View file @
62a1e377
...
...
@@ -30,6 +30,21 @@ class UsersControllerTest < ActionDispatch::IntegrationTest
assert_redirected_to
login_url
end
test
"should redirect destroy when not logged in"
do
assert_no_difference
'User.count'
do
delete
user_path
(
@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
user_path
(
@user
)
end
assert_redirected_to
root_url
end
test
"should redirect edit when logged in as wrong user"
do
log_in_as
(
@other_user
)
get
edit_user_path
(
@user
)
...
...
test/fixtures/users.yml
View file @
62a1e377
...
...
@@ -7,3 +7,20 @@ archer:
name
:
Sterling Archer
email
:
duchess@example.gov
password_digest
:
<%= User.digest('password') %>
lana
:
name
:
Lana Kane
email
:
hands@example.gov
password_digest
:
<%= User.digest('password') %>
malory
:
name
:
Malory Archer
email
:
boss@example.gov
password_digest
:
<%= User.digest('password') %>
<% 30.times do |n| %>
user_<%= n %>
:
name
:
<%= "User
#{n}" %>
email
:
<%= "user-#{n}@example.com" %>
password_digest
:
<%= User.digest('password') %>
<% end %>
\ No newline at end of file
test/integration/users_index_test.rb
0 → 100644
View file @
62a1e377
require
"test_helper"
class
UsersIndexTest
<
ActionDispatch
::
IntegrationTest
def
setup
@admin
=
users
(
:michael
)
@non_admin
=
users
(
:archer
)
end
test
"index as admin including pagination and delete links"
do
log_in_as
(
@admin
)
get
users_path
assert_template
'users/index'
assert_select
'div.pagination'
first_page_of_users
=
User
.
paginate
(
page:
1
)
first_page_of_users
.
each
do
|
user
|
assert_select
'a[href=?]'
,
user_path
(
user
),
text:
user
.
name
unless
user
=
@admin
assert_select
'a[href=?]'
,
user_path
(
user
),
text:
'delete'
end
end
# assert_difference 'User.count', -1 do
# delete user_path(@non_admin)
# end
end
test
"index as non-admin"
do
log_in_as
(
@non_admin
)
get
users_path
assert_select
'a'
,
text:
'delete'
,
count:
0
end
end
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment