Skip to content

Commit

Permalink
検索機能のロジックを修正する
Browse files Browse the repository at this point in the history
  • Loading branch information
yocchan-git committed Mar 28, 2024
1 parent f132d09 commit f278ef3
Show file tree
Hide file tree
Showing 9 changed files with 46 additions and 121 deletions.
5 changes: 0 additions & 5 deletions app/controllers/api/users_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,6 @@ def index
.page(params[:page])
.per(PAGER_NUMBER)
end

return unless params[:require_html]

@users = @users.page(CURRENT_PAGE_COUNT).per(PAGER_NUMBER) if params[:search_word]
render json: { html: render_to_string(partial: 'users/user_list', locals: { users: @users, is_search: true }, formats: :html) }
end

def show; end
Expand Down
13 changes: 9 additions & 4 deletions app/controllers/users_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,8 @@ def index
.order(updated_at: :desc)

@users = @users.unhibernated.unretired unless @target.in? %w[hibernated retired]
@users = search_for_users(@target, @users, validate_search_word(params[:search_word])) if params[:search_word]

if params[:search_word]
@users = search_for_users(@target, @users, params[:search_word]).page(params[:page]).per(PAGER_NUMBER)
@search_word = params[:search_word]
end
@random_tags = User.tags.sample(20)
@top3_tags_counts = User.tags.limit(3).map(&:count).uniq
@tag = ActsAsTaggableOn::Tag.find_by(name: params[:tag])
Expand Down Expand Up @@ -197,4 +194,12 @@ def require_token

redirect_to root_path, notice: 'アドバイザー・メンター・研修生登録にはTOKENが必要です。'
end

def validate_search_word(search_word)
if search_word.match?(/^[\w-]+$/)
search_word.strip if search_word.strip.length >= 3
elsif search_word.strip.length >= 2
search_word.strip
end
end
end
1 change: 0 additions & 1 deletion app/javascript/packs/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ import '../hibernation_agreements.js'
import '../current-date-time-setter.js'
import '../modal-switcher.js'
import '../survey-question-listings.js'
import '../search-user.js'
import '../change-subdivisions.js'
import '../register-address.js'
import '../upload-image-to-article.js'
Expand Down
103 changes: 0 additions & 103 deletions app/javascript/search-user.js

This file was deleted.

2 changes: 1 addition & 1 deletion app/views/users/_no_match_user.html.slim
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
- tag_or_target_name = @entered_tag || t("target.#{@target}")
- title = is_search ? '一致する' : "#{tag_or_target_name}の"
- title = params[:search_word].present? ? '一致する' : "#{tag_or_target_name}の"
.row
.o-empty-message
.o-empty-message__icon
Expand Down
2 changes: 1 addition & 1 deletion app/views/users/_user_list.html.slim
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@
= render 'users/user', user:
= paginate users, params: { controller: 'users', action: 'index' }
- else
= render 'users/no_match_user', is_search:
= render 'users/no_match_user'
17 changes: 11 additions & 6 deletions app/views/users/index.html.slim
Original file line number Diff line number Diff line change
Expand Up @@ -49,17 +49,22 @@ main.page-main
.page-body__inner.has-side-nav
.container
.users
- if !@users.empty? && request.path == '/users' && @target != 'followings'
- if (@users.present? && request.path == '/users' && @target != 'followings') || params[:search_word]
.page-filter.form.pt-0
.container.is-md.has-no-x-padding
.form__items
.form-item.is-inline-md-up
label.a-form-label
| 絞り込み
input#js-user-search-input.a-text-input type="text" value="#{@search_word}" placeholder="ユーザー名、読み方、Discord ID、GitHub ID など"
= form_with url: request.path_info, method: 'get', local: true
.form-item.is-inline-md-up
label.a-form-label
| 絞り込み
= hidden_field_tag :target, params[:target]
= text_field_tag :search_word, params[:search_word], class: 'a-text-input',
placeholder: 'ユーザーID、ユーザー名、読み方、Discord ID など',
onchange: 'this.form.submit()',
id: 'js-user-search-input'
.page-content.is-users
#user_lists.users__items
= render 'users/user_list', users: @users, is_search: false
= render 'users/user_list', users: @users

= render '/users/random_tags'
- unless params[:tag]
Expand Down
1 change: 1 addition & 0 deletions test/system/discord_profiles_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ class DiscordProfilesTest < ApplicationSystemTestCase
visit_with_auth '/users', 'komagata'
assert_equal 24, all('.users-item').length
fill_in 'js-user-search-input', with: 'kimura1234'
find('#js-user-search-input').send_keys :return
assert_text 'Kimura Tadasi', count: 1
end

Expand Down
23 changes: 23 additions & 0 deletions test/system/users_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -381,48 +381,55 @@ class UsersTest < ApplicationSystemTestCase
visit_with_auth '/users', 'komagata'
assert_selector '.users-item', count: 24
fill_in 'js-user-search-input', with: 'kimura'
find('#js-user-search-input').send_keys :return
assert_text 'Kimura Tadasi', count: 1
end

test 'incremental search by name' do
visit_with_auth '/users', 'kimura'
assert_selector '.users-item', count: 24
fill_in 'js-user-search-input', with: 'Shinji'
find('#js-user-search-input').send_keys :return
assert_text 'Hatsuno Shinji', count: 1
end

test 'incremental search by name_kana' do
visit_with_auth '/users', 'mentormentaro'
assert_selector '.users-item', count: 24
fill_in 'js-user-search-input', with: 'キムラ ミタイ'
find('#js-user-search-input').send_keys :return
assert_text 'Kimura Mitai', count: 1
end

test 'incremental search by twitter_account' do
visit_with_auth '/users', 'komagata'
assert_selector '.users-item', count: 24
fill_in 'js-user-search-input', with: 'hatsuno'
find('#js-user-search-input').send_keys :return
assert_text 'Hatsuno Shinji', count: 1
end

test 'incremental search by blog_url' do
visit_with_auth '/users', 'komagata'
assert_selector '.users-item', count: 24
fill_in 'js-user-search-input', with: 'hatsuno.org'
find('#js-user-search-input').send_keys :return
assert_text 'Hatsuno Shinji', count: 1
end

test 'incremental search by github_account' do
visit_with_auth '/users', 'komagata'
assert_selector '.users-item', count: 24
fill_in 'js-user-search-input', with: 'kananashi'
find('#js-user-search-input').send_keys :return
assert_text 'ユーザーです 読み方のカナが無い', count: 1
end

test 'incremental search by facebook_url' do
visit_with_auth '/users', 'komagata'
assert_selector '.users-item', count: 24
fill_in 'js-user-search-input', with: 'kimurafacebook'
find('#js-user-search-input').send_keys :return
assert_text 'Kimura Mitai', count: 1
end

Expand All @@ -437,59 +444,71 @@ class UsersTest < ApplicationSystemTestCase
visit_with_auth '/users?target=mentor', 'komagata'
assert_selector '.users-item', count: 4
fill_in 'js-user-search-input', with: 'machida'
find('#js-user-search-input').send_keys :return
assert_text 'Machida Teppei', count: 1

fill_in 'js-user-search-input', with: 'kimura'
find('#js-user-search-input').send_keys :return
assert_text '一致するユーザーはいません'
end

test 'search only graduated students when target is graduate' do
visit_with_auth '/users?target=graduate', 'komagata'
assert_selector '.users-item', count: 3
fill_in 'js-user-search-input', with: '卒業 就職済美'
find('#js-user-search-input').send_keys :return
assert_text '卒業 就職済美', count: 1

fill_in 'js-user-search-input', with: 'kimura'
find('#js-user-search-input').send_keys :return
assert_text '一致するユーザーはいません'
end

test 'search only adviser when target is adviser' do
visit_with_auth '/users?target=adviser', 'komagata'
assert_selector '.users-item', count: 4
fill_in 'js-user-search-input', with: 'advijirou'
find('#js-user-search-input').send_keys :return
assert_text 'アドバイ 次郎', count: 1

fill_in 'js-user-search-input', with: 'kimura'
find('#js-user-search-input').send_keys :return
assert_text '一致するユーザーはいません'
end

test 'search only retired when target is retired' do
visit_with_auth '/users?target=retired', 'komagata'
assert_selector '.users-item', count: 4
fill_in 'js-user-search-input', with: 'yameo'
find('#js-user-search-input').send_keys :return
assert_text '辞目 辞目夫', count: 1

fill_in 'js-user-search-input', with: 'kimura'
find('#js-user-search-input').send_keys :return
assert_text '一致するユーザーはいません'
end

test 'search only trainee when target is trainee' do
visit_with_auth '/users?target=trainee', 'komagata'
assert_selector '.users-item', count: 2
fill_in 'js-user-search-input', with: 'Kensyu Seiko'
find('#js-user-search-input').send_keys :return
assert_text 'Kensyu Seiko', count: 1

fill_in 'js-user-search-input', with: 'kimura'
find('#js-user-search-input').send_keys :return
assert_text '一致するユーザーはいません'
end

test 'search users from all users when target is all' do
visit_with_auth '/users?target=all', 'komagata'
assert_selector '.users-item', count: 24
fill_in 'js-user-search-input', with: 'hajime'
find('#js-user-search-input').send_keys :return
assert_text 'Hajime Tayo', count: 1

fill_in 'js-user-search-input', with: 'machida'
find('#js-user-search-input').send_keys :return
assert_text 'Machida Teppei', count: 1
end

Expand Down Expand Up @@ -523,14 +542,18 @@ class UsersTest < ApplicationSystemTestCase
visit_with_auth '/users', 'komagata'
assert_selector '.users-item', count: 24
fill_in 'js-user-search-input', with: 'ki'
find('#js-user-search-input').send_keys :return
assert_selector '.users-item', count: 24
fill_in 'js-user-search-input', with: 'kim'
find('#js-user-search-input').send_keys :return
assert_text 'Kimura', count: 2

fill_in 'js-user-search-input', with: 'キ'
find('#js-user-search-input').send_keys :return
assert_selector '.user-list'
assert_no_selector '.searched-user-list'
fill_in 'js-user-search-input', with: 'キム'
find('#js-user-search-input').send_keys :return
assert_text 'Kimura', count: 2
end

Expand Down

0 comments on commit f278ef3

Please sign in to comment.