Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

管理ページに、ユーザーのroleとjobの絞り込み機能追加 #7418

Merged
merged 13 commits into from
May 28, 2024
Merged
2 changes: 2 additions & 0 deletions app/controllers/admin/users_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ def index
else
user_scope.where(retired_on: nil)
end
@job = params[:job]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jobはviewで使っていますか?
もし使っていなければインスタンス変数である必要はないかもです。

Copy link
Contributor Author

@reckyy reckyy Apr 28, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

こちら、渡しそびれていました。
気づいていただきありがとうございます!

下記でsortするときに渡すクエリパラメータのために、用意していたインスタンス変数になります。

  • ユーザー名
  • 企業
  • 最終活動日時
  • 登録日時
スクリーンショット 2024-04-28 21 58 13

渡しそびれていたインスタンス変数を追記

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@reckyy なるほどです~

user_scope = user_scope.users_job(@job) if @job.present?
@users = user_scope.with_attached_avatar
.preload(:company, :course)
.order_by_counts(params[:order_by] || 'id', @direction)
Expand Down
10 changes: 10 additions & 0 deletions app/helpers/users_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,14 @@ def all_countries_with_subdivisions
.to_h
.to_json
end

def roles_for_select
roles = %w[all student_and_trainee inactive hibernated retired graduate adviser mentor trainee year_end_party campaign]
roles.map { |role| [t("target.#{role}"), role] }
end

def jobs_for_select
user_jobs = User.jobs.keys.map { |job| [t("activerecord.enums.user.job.#{job}"), job] }
user_jobs.prepend(%w[全員 all])
end
end
1 change: 1 addition & 0 deletions app/javascript/stylesheets/application.sass
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@
@import application/blocks/page/page-tabs
@import application/blocks/page/page
@import application/blocks/page/page-filter
@import application/blocks/page/page-main-filter

@import application/blocks/page-content/page-content
@import application/blocks/page-content/page-content-header-actions
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
.page-main-filter__inner
display: block
padding-block: .75rem

.page-main-filter__row
display: flex
gap: 1rem
align-items: center
flex-wrap: wrap
.page-main-filter__row
gap: .5rem
2 changes: 2 additions & 0 deletions app/javascript/stylesheets/atoms/_a-form-label.sass
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
.a-form-label
+text-block(.875rem 1.5 0 .5rem, block 600)
&:last-child
margin-bottom: 0
&.is-required
&::after
+fa(fas '\f069')
Expand Down
8 changes: 8 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,14 @@ def users_role(target, allowed_targets: [], default_target: :none)
send(scope_name)
end

# User::users_roleと同じく安全性確保のため、以下の条件を指定している。
# allowed_job.include?(job): 存在する職業を過不足なく指定した配列の中に、jobが存在するかどうかチェック。
def users_job(job)
allowed_jobs = User.jobs.keys.freeze
scope_name = allowed_jobs.include?(job) ? "job_#{job}" : 'all'
send(scope_name)
end

def tags
unretired.unhibernated.all_tag_counts(order: 'count desc, name asc')
end
Expand Down
24 changes: 24 additions & 0 deletions app/views/admin/users/_filter.html.slim
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
.page-main-filter
.container
= form_with url: admin_users_path(target: @target, job: @job), local: true, method: 'get', class: 'page-main-filter__inner' do
.page-main-filter__row
.page-main-filter__column
.page-main-filter__row
.page-main-filter__column
label.a-form-label.is-sm
| ロール
.page-main-filter__column
.a-button.is-sm.is-secondary.is-select.is-block
= select_tag 'target', options_for_select(roles_for_select, params[:target])
.page-main-filter__column
.page-main-filter__row
.page-main-filter__column
label.a-form-label.is-sm
| 職業
.page-main-filter__column
.a-button.is-sm.is-secondary.is-select.is-block
= select_tag 'job', options_for_select(jobs_for_select, params[:job])
.page-main-filter__column
button.a-button.is-sm.is-primary.is-block
span
| この条件で絞り込む
6 changes: 0 additions & 6 deletions app/views/admin/users/_nav.html.slim

This file was deleted.

2 changes: 1 addition & 1 deletion app/views/admin/users/_sort_column.html.slim
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
- direction = params[:order_by] == order_by && direction == 'asc' ? 'desc' : 'asc'

= link_to admin_users_path(order_by:, direction:, target:), class: 'a-button is-xs is-secondary is-icon' do
= link_to admin_users_path(order_by:, direction:, target:, job:), class: 'a-button is-xs is-secondary is-icon' do
- if direction == 'asc'
i.fa-solid.fa-caret-up
- else
Expand Down
8 changes: 4 additions & 4 deletions app/views/admin/users/_table.html.slim
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,21 @@
th.admin-table__label ステータス
th.admin-table__label
| ユーザー名
= render 'sort_column', order_by: 'login_name', direction: direction, target: @target
= render 'sort_column', order_by: 'login_name', direction: direction, target: @target, job: @job
th.admin-table__label
| メールアドレス
th.admin-table__label
| 企業
= render 'sort_column', order_by: 'company_id', direction: direction, target: @target
= render 'sort_column', order_by: 'company_id', direction: direction, target: @target, job: @job
th.admin-table__label 職業
th.admin-table__label Discord
th.admin-table__label
|
= User.human_attribute_name :last_activity_at
= render 'sort_column', order_by: 'last_activity_at', direction: direction, target: @target
= render 'sort_column', order_by: 'last_activity_at', direction: direction, target: @target, job: @job
th.admin-table__label
| 登録日時
= render 'sort_column', order_by: 'created_at', direction: direction, target: @target
= render 'sort_column', order_by: 'created_at', direction: direction, target: @target, job: @job
th.admin-table__label 相談部屋
th.admin-table__label カード登録
th.admin-table__label サブスク
Expand Down
18 changes: 13 additions & 5 deletions app/views/admin/users/index.html.slim
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,16 @@ header.page-header
= title

= render 'admin/admin_page_tabs'
= render 'nav'

.page-body
.container
= render 'table', users: @users, direction: @direction
main.page-main
header.page-main-header
.container
.page-main-header__inner
.page-main-header__start
h1.page-main-header__title
| ユーザー一覧
hr.a-border
= render 'filter'
hr.a-border
.page-body
.container
= render 'table', users: @users, direction: @direction
29 changes: 29 additions & 0 deletions test/helpers/users_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,33 @@ class UsersHelperTest < ActionView::TestCase
assert_includes countries['JP'], %w[北海道 01]
assert_includes countries['US'], %w[アラスカ州 AK]
end

test '#roles_for_select' do
user_roles = [
%w[全員 all],
%w[現役生 student_and_trainee],
%w[非アクティブ inactive],
%w[休会 hibernated],
%w[退会 retired],
%w[卒業 graduate],
%w[アドバイザー adviser],
%w[メンター mentor],
%w[研修生 trainee],
%w[忘年会 year_end_party],
%w[お試し延長 campaign]
]
assert roles_for_select, user_roles
end

test '#jobs_for_select' do
user_jobs = [
%w[全員 all],
%w[学生 student],
%w[会社員 office_worker],
%w[フリーター part_time_worker],
%w[休職中 vacation],
%w[働いていない unemployed]
]
assert jobs_for_select, user_jobs
end
end
12 changes: 12 additions & 0 deletions test/models/user_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -724,4 +724,16 @@ class UserTest < ActiveSupport::TestCase
assert_equal '2020-07-01 09:00:00 +0900', users(:kyuukai).scheduled_retire_at.to_s
assert_nil users(:hatsuno).scheduled_retire_at
end

test '.users_job' do
assert_equal User.job_student, User.users_job('student')
assert_equal User.job_office_worker, User.users_job('office_worker')
assert_equal User.job_part_time_worker, User.users_job('part_time_worker')
assert_equal User.job_vacation, User.users_job('vacation')
assert_equal User.job_unemployed, User.users_job('unemployed')
end

test '.users_job returns all users when invalid job is passed' do
assert_equal User.all, User.users_job('destroy_all')
end
end
7 changes: 7 additions & 0 deletions test/system/admin/users_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,13 @@ class Admin::UsersTest < ApplicationSystemTestCase
assert_text 'kensyu(Kensyu Seiko)'
end

test 'show listing graduated and office_worker' do
visit_with_auth '/admin/users?target=graduate&job=office_worker', 'komagata'
assert_equal '管理ページ | FBC', title
assert_text 'sotugyou-with-job(卒業 就職済美)'
assert_no_text 'sotugyou(卒業 太郎)'
end

test 'exclude hibernated and retired users from year-end-party email list' do
visit_with_auth '/admin/users?target=year_end_party', 'komagata'
assert_equal '管理ページ | FBC', title
Expand Down