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

卒業証書のPDFファイルをアップロードできる機能を追加 #8190

Open
wants to merge 13 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion app/controllers/admin/users_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ def show
def edit; end

def update
@user.diploma_file = nil if params[:user][:remove_diploma] == '1'
if @user.update(user_params)
destroy_subscription(@user)
Newspaper.publish(:retirement_create, { user: @user }) if @user.saved_change_to_retired_on?
Expand Down Expand Up @@ -67,7 +68,7 @@ def user_params
:job_seeker, :github_collaborator,
:officekey_permission, :tag_list, :training_ends_on,
:auto_retire, :invoice_payment, :hide_mentor_profile,
:profile_image, :profile_name, :profile_job, :mentor,
:profile_image, :profile_name, :profile_job, :mentor, :diploma_file,
:profile_text, { authored_books_attributes: %i[id title url cover _destroy] },
:country_code, :subdivision_code, discord_profile_attributes: %i[account_name times_url times_id]
)
Expand Down
14 changes: 14 additions & 0 deletions app/javascript/fileinput.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,3 +85,17 @@ document.addEventListener('DOMContentLoaded', () => {
}
initializeFileInput(document)
})

Copy link
Contributor

Choose a reason for hiding this comment

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

今回実装する部分を関数にして↑の DOMContentLoadedに入れてはどうでしょうか?

あと画面の読み込み時に実行していますが、該当する画面でない場合のreturnがなさそうです。

Copy link
Contributor Author

Choose a reason for hiding this comment

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

変更しました
6b5d653

document.addEventListener('DOMContentLoaded', () => {
const removeDiplomaButton = document.getElementById('remove-diploma-button')
const diplomaUploadField = document.getElementById('diploma-upload-field')
const diplomaFileLink = document.getElementById('diploma-file-link')
const removeDiplomaFlag = document.getElementById('remove-diploma-flag')

removeDiplomaButton.addEventListener('click', () => {
diplomaFileLink && (diplomaFileLink.style.display = 'none')
Copy link
Contributor

@mousu-a mousu-a Dec 2, 2024

Choose a reason for hiding this comment

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

すみません、ここの説明ほしいです🙏
なぜこのコードが必要なのかがわからない感じです。
diplomaFileLink && (diplomaFileLink.style.display = 'none')

Copy link
Contributor Author

@hagiya0121 hagiya0121 Dec 3, 2024

Choose a reason for hiding this comment

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

diplomaFileLink && (diplomaFileLink.style.display = 'none')の動作としてはファイルアップロード後(更新するを押した後)のリンクを非表示にしています。見た目はリンクっぽくないんですけど画像の赤枠全体がリンクです。
52e890b704da06768146c252c390f26657317389149c9040ab3448a4598540a3
これがないとファイルアップロード後に削除を押しても画像のようにリンクが残ったままになってしまいます。
image

Copy link
Contributor

@mousu-a mousu-a Dec 3, 2024

Choose a reason for hiding this comment

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

diplomaFileLinkが存在する場合のみ右辺が発火するわけですね。
勉強になります🙏

その場合下記のような形ではどうでしょうか?

if (diplomaFileLink) diplomaFileLink.style.display = 'none'

Copy link
Contributor Author

Choose a reason for hiding this comment

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

町田さんがデザインを入れた際に変更してくれたみたいです🤔18c14ab2e02b8526752

diplomaUploadField.style.display = 'block'
diplomaUploadField.value = ''
removeDiplomaFlag.value = '1'
})
})
3 changes: 3 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ class User < ApplicationRecord

has_one_attached :avatar
has_one_attached :profile_image
has_one_attached :diploma_file

after_create UserCallbacks.new

Expand Down Expand Up @@ -207,6 +208,8 @@ class User < ApplicationRecord
message: 'はPNG, JPG, GIF, HEIC, HEIF形式にしてください'
}

validates :diploma_file, content_type: { in: ['application/pdf'], message: 'はPDF形式にしてください' }

validates :country_code, inclusion: { in: ISO3166::Country.codes }, allow_blank: true

validates :subdivision_code, inclusion: { in: ->(user) { user.subdivision_codes } }, allow_blank: true, if: -> { country_code.present? }
Expand Down
9 changes: 9 additions & 0 deletions app/views/users/_form.html.slim
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,15 @@
.form-item-block__item
= render 'users/form/graduate', f: f

.form-item
= f.hidden_field :remove_diploma, value: '0', id: 'remove-diploma-flag'
= f.label :diploma_file, class: 'a-form-label'
- if @user.diploma_file.attached?
= link_to @user.diploma_file.filename, url_for(@user.diploma_file), class: 'a-text-input', id: 'diploma-file-link'
- display = @user.diploma_file.attached? ? 'display: none' : 'display: block'
= f.file_field :diploma_file, class: 'a-text-input', id: 'diploma-upload-field', style: display
Copy link
Contributor

@mousu-a mousu-a Dec 2, 2024

Choose a reason for hiding this comment

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

display → display_styleでもいいかもです。

Copy link
Contributor Author

Choose a reason for hiding this comment

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

そちらの名前の方がわかりやすいですね。変更しました!
fd1d8db

button type='button' id='remove-diploma-button' class='a-button is-md is-secondary' 削除

Copy link
Contributor

@mousu-a mousu-a Dec 2, 2024

Choose a reason for hiding this comment

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

button_tagが使えそうです!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

変更しました
3aeb25a

Copy link
Contributor

Choose a reason for hiding this comment

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

ファイルがアップロードされていない状態で削除ボタンがあるのはおかしい気がします🤔

.form-item-block
.form-item-block__inner
header.form-item-block__header
Expand Down
1 change: 1 addition & 0 deletions config/locales/ja.yml
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ ja:
other_editor: その他のエディタ
hide_mentor_profile: プロフィール非公開
invoice_payment: 「請求書払い」
diploma_file: 卒業証書のアップロード
discord_profile:
account_name: Discord アカウント
times_url: 分報URL
Expand Down