Skip to content

Commit

Permalink
Merge pull request #6447 from fjordllc/feature/move-discord-columns
Browse files Browse the repository at this point in the history
usersテーブルにあるDiscord関連のカラムを移動する
  • Loading branch information
komagata authored Jul 29, 2023
2 parents 8abb5cb + f17bca9 commit c6187a3
Show file tree
Hide file tree
Showing 38 changed files with 567 additions and 242 deletions.
6 changes: 3 additions & 3 deletions app/controllers/admin/users_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ def user_params
params.require(:user).permit(
:adviser, :login_name, :name,
:name_kana, :email, :course_id, :subscription_id,
:description, :discord_account, :github_account,
:twitter_account, :facebook_url, :blog_url, :times_url,
:description, :github_account,
:twitter_account, :facebook_url, :blog_url,
:password, :password_confirmation, :job,
:organization, :os, :study_place,
:experience, :company_id,
Expand All @@ -60,7 +60,7 @@ def user_params
:officekey_permission, :tag_list, :training_ends_on,
:profile_image, :profile_name, :profile_job, :mentor,
:profile_text, { authored_books_attributes: %i[id title url cover _destroy] },
:country_code, :subdivision_code
:country_code, :subdivision_code, discord_profile_attributes: %i[account_name times_url]
)
end

Expand Down
3 changes: 2 additions & 1 deletion app/controllers/api/talks/unreplied_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ class API::Talks::UnrepliedController < API::BaseController
PAGER_NUMBER = 20

def index
@talks = Talk.eager_load(user: { avatar_attachment: :blob })
@talks = Talk.joins(:user)
.includes(user: [{ avatar_attachment: :blob }, :discord_profile])
.unreplied
.order(updated_at: :desc, id: :asc)
@talks =
Expand Down
3 changes: 2 additions & 1 deletion app/controllers/api/talks_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ class API::TalksController < API::BaseController
def index
@target = params[:target]
@target = 'all' unless TARGETS.include?(@target)
@talks = Talk.eager_load(user: { avatar_attachment: :blob })
@talks = Talk.joins(:user)
.includes(user: [{ avatar_attachment: :blob }, :discord_profile])
.order(updated_at: :desc, id: :asc)
@talks =
if params[:search_word]
Expand Down
6 changes: 3 additions & 3 deletions app/controllers/current_user_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,16 @@ def user_params
user_attribute = [
:adviser, :login_name, :name,
:name_kana, :email, :course_id,
:description, :job_seeking, :discord_account,
:description, :job_seeking,
:github_account, :twitter_account, :facebook_url,
:blog_url, :times_url, :password, :password_confirmation,
:blog_url, :password, :password_confirmation,
:job, :organization, :os,
:experience, :company_id,
:nda, :avatar, :trainee,
:mail_notification, :job_seeker, :tag_list,
:after_graduation_hope, :training_ends_on, :profile_image,
:profile_name, :profile_job, :profile_text, { authored_books_attributes: %i[id title url cover _destroy] },
:feed_url, :country_code, :subdivision_code
:feed_url, :country_code, :subdivision_code, { discord_profile_attributes: %i[id account_name times_url] }
]
user_attribute.push(:retired_on, :graduated_on, :free, :github_collaborator) if current_user.admin?
params.require(:user).permit(user_attribute)
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/home_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def set_required_fields
avatar_attached: current_user.avatar.attached?,
tag_list_count: current_user.tag_list.size,
after_graduation_hope: current_user.after_graduation_hope,
discord_account: current_user.discord_account,
discord_account_name: current_user.discord_profile.account_name,
github_account: current_user.github_account,
blog_url: current_user.blog_url,
graduated: current_user.graduated?
Expand Down
5 changes: 3 additions & 2 deletions app/controllers/users_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ def create
@user = User.new(user_params)
@user.course_id ||= Course.first.id
@user.free = true if @user.trainee?
@user.build_discord_profile
Newspaper.publish(:user_create, @user)
if @user.staff? || @user.trainee?
create_free_user!
Expand Down Expand Up @@ -147,8 +148,8 @@ def user_params
params.require(:user).permit(
:login_name, :name, :name_kana,
:email, :course_id, :description,
:discord_account, :github_account, :twitter_account,
:facebook_url, :blog_url, :times_url, :password,
:github_account, :twitter_account,
:facebook_url, :blog_url, :password,
:password_confirmation, :job, :organization,
:os, :experience,
:company_id, :nda, :avatar,
Expand Down
2 changes: 1 addition & 1 deletion app/helpers/home_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def anchor_to_required_field(attribute)
avatar_attached: 'form-user-avatar',
tag_list_count: 'form-tag-list',
after_graduation_hope: 'form-after-graduation-hope',
discord_account: 'form-discord-account',
discord_account_name: 'form-discord-account',
github_account: 'form-github-account',
blog_url: 'form-blog-url'
}[attribute]
Expand Down
4 changes: 2 additions & 2 deletions app/javascript/components/user-sns.vue
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@
i.fa-solid.fa-blog
li.sns-links__item
a.sns-links__item-link.a-button.is-sm.is-secondary.is-icon(
v-if='user.times_url',
:href='user.times_url')
v-if='user.discord_profile.times_url',
:href='user.discord_profile.times_url')
i.fa-solid.fa-clock
.sns-links__item-link.a-button.is-sm.is-disabled.is-icon(v-else)
i.fa-solid.fa-clock
Expand Down
8 changes: 5 additions & 3 deletions app/javascript/components/user.vue
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,16 @@
.a-meta
| {{ user.name }}
.card-list-item-meta__item
a.a-meta(v-if='user.times_url', :href='user.times_url')
a.a-meta(
v-if='user.discord_profile.times_url',
:href='user.discord_profile.times_url')
.a-meta__icon
i.fa-brands.fa-discord
| {{ user.discord_account }}
| {{ user.discord_profile.account_name }}
.a-meta(v-else)
.a-meta__icon
i.fa-brands.fa-discord
| {{ user.discord_account }}
| {{ user.discord_profile.account_name }}

user-sns(:user='user')
user-activity-counts(:user='user')
Expand Down
20 changes: 20 additions & 0 deletions app/models/discord_profile.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# frozen_string_literal: true

class DiscordProfile < ApplicationRecord
belongs_to :user

validates :times_url,
format: {
allow_blank: true,
with: %r{\Ahttps://discord\.com/channels/\d+/\d+\z},
message: 'はDiscordのチャンネルURLを入力してください'
}
with_options if: -> { validation_context != :retirement } do
validates :account_name,
format: {
allow_blank: true,
with: /\A[^\s\p{blank}].*[^\s\p{blank}]#\d{4}\z/,
message: 'は「ユーザー名#4桁の数字」で入力してください'
}
end
end
2 changes: 1 addition & 1 deletion app/models/required_field.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class RequiredField
attribute :avatar_attached, :boolean
attribute :tag_list_count, :integer
attribute :after_graduation_hope, :string
attribute :discord_account, :string
attribute :discord_account_name, :string
attribute :github_account, :string
attribute :blog_url, :string
attribute :graduated, :boolean
Expand Down
2 changes: 1 addition & 1 deletion app/models/times_channel_creator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ def call(user)

times_channel = Discord::TimesChannel.new(user.login_name)
if times_channel.save
user.update(times_id: times_channel.id)
user.discord_profile.update(times_id: times_channel.id)
else
Rails.logger.warn "[Discord API] #{user.login_name}の分報チャンネルが作成できませんでした。"
end
Expand Down
35 changes: 3 additions & 32 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ class User < ApplicationRecord
has_many :external_entries, dependent: :destroy
has_one :report_template, dependent: :destroy
has_one :talk, dependent: :destroy
has_one :discord_profile, dependent: :destroy
accepts_nested_attributes_for :discord_profile, allow_destroy: true

has_many :participate_events,
through: :participations,
Expand Down Expand Up @@ -148,12 +150,6 @@ class User < ApplicationRecord
validates :password, length: { minimum: 4 }, confirmation: true, if: :password_required?
validates :mail_notification, inclusion: { in: [true, false] }
validates :github_id, uniqueness: true, allow_nil: true
validates :times_url,
format: {
allow_blank: true,
with: %r{\Ahttps://discord\.com/channels/\d+/\d+\z},
message: 'はDiscordのチャンネルURLを入力してください'
}

validates :feed_url,
format: {
Expand Down Expand Up @@ -203,12 +199,6 @@ class User < ApplicationRecord
end

with_options if: -> { validation_context != :retirement } do
validates :discord_account,
format: {
allow_blank: true,
with: /\A[^\s\p{blank}].*[^\s\p{blank}]#\d{4}\z/,
message: 'は「ユーザー名#4桁の数字」で入力してください'
}
validates :twitter_account,
length: { maximum: 15 },
format: {
Expand Down Expand Up @@ -366,7 +356,7 @@ class User < ApplicationRecord
:facebook_url,
:blog_url,
:github_account,
:discord_account,
:discord_profile_account_name,
:description
)

Expand Down Expand Up @@ -666,25 +656,6 @@ def update_mentor_memo(new_memo)
update!(mentor_memo: new_memo)
end

def convert_to_channel_url!
match = times_url&.match(%r{\Ahttps://discord.gg/(?<invite_code>\w+)\z})
return if match.nil?

uri = URI("https://discord.com/api/invites/#{match[:invite_code]}")
res = Net::HTTP.get_response(uri)

case res
when Net::HTTPSuccess
data = JSON.parse(res.body)
update!(times_url: "https://discord.com/channels/#{data['guild']['id']}/#{data['channel']['id']}")
when Net::HTTPNotFound
logger.warn "[Discord API] 無効な招待URLです: #{login_name} (#{times_url})"
update!(times_url: nil)
else
logger.error "[Discord API] チャンネルURLを取得できません: #{login_name} (#{res.code} #{res.message})"
end
end

def category_active_or_unstarted_practice
if active_practices.present?
category_having_active_practice
Expand Down
2 changes: 1 addition & 1 deletion app/views/admin/users/_table.html.slim
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@
- else
| -
td.admin-table__item-value.is-text-align-center
= user.discord_account.presence || '-'
= user.discord_profile.account_name || '-'
td.admin-table__item-value.is-text-align-center
- if user.last_activity_at?
= l user.last_activity_at
Expand Down
7 changes: 6 additions & 1 deletion app/views/api/users/_list_user.json.jbuilder
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
json.(user, :id, :login_name, :name, :discord_account, :description, :github_account, :twitter_account, :facebook_url, :blog_url, :times_url, :job_seeker, :free, :job, :os, :experience, :email, :roles, :primary_role, :icon_title, :cached_completed_percentage, :completed_fraction, :graduated_on)
json.(user, :id, :login_name, :name, :description, :github_account, :twitter_account, :facebook_url, :blog_url, :job_seeker, :free, :job, :os, :experience, :email, :roles, :primary_role, :icon_title, :cached_completed_percentage, :completed_fraction, :graduated_on)
json.tag_list user.tags.pluck(:name)
json.url user_url(user)
json.updated_at l(user.updated_at)
Expand Down Expand Up @@ -27,6 +27,11 @@ if user.talk.present?
json.talkUrl talk_path(user.talk)
end

json.discord_profile do
json.account_name user.discord_profile.account_name
json.times_url user.discord_profile.times_url
end

json.company do
if user.company.present?
json.id user.company.id
Expand Down
6 changes: 3 additions & 3 deletions app/views/hibernation/new.html.slim
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,13 @@ header.page-header
placeholder: '仕事がXX月まで繁忙期で残業と休日出勤が続き、学習の時間を確保できなくなるため。',
class: 'a-text-input is-sm'

- if current_user.times_url?
- if current_user.discord_profile.times_url?
.form-item
label.a-form-label
| 分報チャンネル URL
.form-link-block
= link_to current_user.times_url, class: 'form-link-block__link a-text-link' do
= current_user.times_url
= link_to current_user.discord_profile.times_url, class: 'form-link-block__link a-text-link' do
= current_user.discord_profile.times_url
.a-form-help
p
| 休会の期間、分報チャンネルが削除されないようにします。
Expand Down
6 changes: 3 additions & 3 deletions app/views/users/_metas.html.slim
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@
.user-metas__item-label
| Discordアカウント
.user-metas__item-value
= user.discord_account.presence || '未登録'
- if user.times_url?
| (#{link_to '分報', user.times_url}
= user.discord_profile.account_name.presence || '未登録'
- if user.discord_profile.times_url?
| (#{link_to '分報', user.discord_profile.times_url}
- unless user.admin? || user.adviser?
.user-metas__item
.user-metas__item-label
Expand Down
8 changes: 4 additions & 4 deletions app/views/users/_profile.html.slim
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,16 @@
.a-meta
| #{user.name}#{user.name_kana}
.card-list-item-meta__item
- if user.times_url?
= link_to user.times_url, class: 'a-meta' do
- if user.discord_profile.times_url?
= link_to user.discord_profile.times_url, class: 'a-meta' do
.a-meta__icon
i.fab.fa-discord
= user.discord_account.presence || 'Discord未設定'
= user.discord_profile.account_name.presence || 'Discord未設定'
- else
.a-meta
.a-meta__icon
i.fab.fa-discord
= user.discord_account.presence || 'Discord未設定'
| Discord未設定
= render 'users/sns', user: user
- if admin_login?
.form-actions.is-only-mentor
Expand Down
4 changes: 2 additions & 2 deletions app/views/users/_sns.html.slim
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@
.sns-links__item-link.a-button.is-sm.is-disabled.is-icon
i.fa-solid.fa-blog
li.sns-links__item
- if user.times_url?
= link_to user.times_url, class: 'sns-links__item-link a-button is-sm is-secondary is-icon' do
- if user.discord_profile.times_url?
= link_to user.discord_profile.times_url, class: 'sns-links__item-link a-button is-sm is-secondary is-icon' do
i.fa-solid.fa-clock
- else
.sns-links__item-link.a-button.is-sm.is-disabled.is-icon
Expand Down
24 changes: 13 additions & 11 deletions app/views/users/form/_sns.html.slim
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,19 @@
| こちら
span.a-help
i.fa-solid.fa-question
.form-item#times-url
= f.label :times_url, class: 'a-form-label'
= f.text_field :times_url, class: 'a-text-input', placeholder: 'https://discord.com/channels/715806612824260640/123456789012345678'
.a-form-help
p
| Discord の分報チャンネルの URL を入力。調べ方は
label.a-form-help-link.is-danger(for='modal-times-url')
span.a-form-help-link__label
| こちら
span.a-help
i.fa-solid.fa-question
= f.fields_for :discord_profile do |discord_profile_fields|
.form-item#times-url
= discord_profile_fields.label :times_url, class: 'a-form-label'
= discord_profile_fields.text_field :times_url, class: 'a-text-input', placeholder: 'https://discord.com/channels/715806612824260640/123456789012345678'
.a-form-help
p
| Discord の分報チャンネルの URL を入力。調べ方は
label.a-form-help-link.is-danger(for='modal-times-url')
span.a-form-help-link__label
| こちら
span.a-help
i.fa-solid.fa-question

.form-item#form-github-account
= f.label :github_account, class: 'a-form-label'
Expand Down
5 changes: 3 additions & 2 deletions config/locales/ja.yml
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,6 @@ ja:
blog_url: ブログ URL
feed_url: ブログのフィードURL
github_account: GitHub アカウント
discord_account: Discord アカウント
times_url: 分報URL
password: パスワード
password_confirmation: パスワード(確認)
current_password: 現在のパスワード
Expand Down Expand Up @@ -101,6 +99,9 @@ ja:
subscription_id: サブスクリプションID
country_code:
subdivision_code: 都道府県・州
discord_profile:
account_name: Discord アカウント
times_url: 分報URL
hibernation:
reason: 休会の理由
scheduled_return_on: 復帰予定日
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# frozen_string_literal: true

class CopyUserDiscordColumnsToDiscordProfile < ActiveRecord::Migration[6.1]
def up
User.all.each do |user|
discord_profile = user.build_discord_profile
discord_profile.account_name = user.discord_account unless user.discord_account.nil?
discord_profile.times_url = user.times_url unless user.times_url.nil?
discord_profile.times_id = user.times_id unless user.times_id.nil?
discord_profile.save!
end
end

def down
raise ActiveRecord::IrreversibleMigration
end
end
Loading

0 comments on commit c6187a3

Please sign in to comment.