Skip to content

Commit

Permalink
ユーザー一覧を非vue化する
Browse files Browse the repository at this point in the history
  • Loading branch information
yocchan-git committed Feb 22, 2024
1 parent a364995 commit 2b5da72
Show file tree
Hide file tree
Showing 24 changed files with 463 additions and 550 deletions.
6 changes: 3 additions & 3 deletions app/controllers/api/followings_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ def create
user = User.find(params[:id])
watch = params[:watch] == 'true'
if current_user.follow(user, watch:)
render json: { id: user.id }
render json: { html: render_to_string(partial: 'users/following', locals: { user: }) }
else
head :bad_request
end
Expand All @@ -17,7 +17,7 @@ def update
user = User.find(params[:id])
watch = params[:watch] == 'true'
if current_user.change_watching(user, watch)
head :no_content
render json: { html: render_to_string(partial: 'users/following', locals: { user: }) }
else
head :bad_request
end
Expand All @@ -26,7 +26,7 @@ def update
def destroy
user = User.find(params[:id])
if current_user.unfollow(user)
head :no_content
render json: { html: render_to_string(partial: 'users/following', locals: { user: }) }
else
head :bad_request
end
Expand Down
5 changes: 5 additions & 0 deletions app/controllers/api/users_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ def index
.page(params[:page])
.per(PAGER_NUMBER)
end

return unless params[:require_html]

@users = @users.page(params[:page]).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
4 changes: 4 additions & 0 deletions app/controllers/generations_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

class GenerationsController < ApplicationController
ALLOWED_TARGETS = %w[all trainee adviser graduate mentor retired].freeze
PAGER_NUMBER = 24

def show
@generation = params[:id].to_i
Expand All @@ -10,5 +11,8 @@ def show
def index
@target = ALLOWED_TARGETS.include?(params[:target]) ? params[:target] : ALLOWED_TARGETS.first
redirect_to root_path, alert: '管理者としてログインしてください' if @target == 'retired' && !current_user.admin?

result = Generation.generations(@target).reverse
@generations = Kaminari.paginate_array(result).page(params[:page]).per(PAGER_NUMBER)
end
end
35 changes: 25 additions & 10 deletions app/controllers/users_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,15 @@ class UsersController < ApplicationController
before_action :require_token, only: %i[new] if Rails.env.production?
before_action :set_user, only: %w[show]

PAGER_NUMBER = 20
PAGER_NUMBER = 24

def index
@target = params[:target]
@target = 'student_and_trainee' unless target_allowlist.include?(@target)
@entered_tag = params[:tag]
@watch = params[:watch]

target_users =
if @target == 'followings'
current_user.followees_list(watch: @watch)
elsif params[:tag]
User.tagged_with(params[:tag])
else
users = User.users_role(@target, allowed_targets: target_allowlist)
@target == 'inactive' ? users.order(:last_activity_at) : users
end
target_users = fetch_target_users

@users = target_users
.page(params[:page]).per(PAGER_NUMBER)
Expand All @@ -29,6 +22,10 @@ def index

@users = @users.unhibernated.unretired unless @target.in? %w[hibernated retired]

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 @@ -81,6 +78,24 @@ def create

private

def search_for_users(target, target_users, search_word)
users = target_users.search_by_keywords({ word: search_word })
# search_by_keywords内では { unretired } というスコープが設定されている
# 退会したユーザーに対しキーワード検索を行う場合は、一旦 unscope(where: :retired_on) で { unretired } スコープを削除し、その後で retired スコープを設定する必要がある
target == 'retired' ? users.unscope(where: :retired_on).retired : users
end

def fetch_target_users
if @target == 'followings'
current_user.followees_list(watch: @watch)
elsif @entered_tag
User.tagged_with(@entered_tag)
else
users = User.users_role(@target, allowed_targets: target_allowlist)
@target == 'inactive' ? users.order(:last_activity_at) : users
end
end

def target_allowlist
target_allowlist = %w[student_and_trainee followings mentor graduate adviser trainee year_end_party]
target_allowlist.push('job_seeking') if current_user.adviser?
Expand Down
13 changes: 13 additions & 0 deletions app/helpers/users_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,19 @@ def users_name
User.pluck(:login_name, :id).sort
end

def button_label(user)
if current_user.following?(user)
current_user.watching?(user) ? 'コメントあり' : 'コメントなし'
else
'フォローする'
end
end

def desc_paragraphs(user)
max_description = user.description.length <= 200 ? user.description : "#{user.description[0...200]}..."
max_description.split(/\n|\r\n/).map.with_index { |text, i| { id: i, text: } }
end

def all_countries_with_subdivisions
ISO3166::Country.all
.map { |country| [country.alpha2, country.subdivision_names_with_codes(I18n.locale.to_s)] }
Expand Down
212 changes: 0 additions & 212 deletions app/javascript/components/users.vue

This file was deleted.

13 changes: 0 additions & 13 deletions app/javascript/debounce.js

This file was deleted.

Loading

0 comments on commit 2b5da72

Please sign in to comment.