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

入って1週間のユーザーにはメンターにしか表示されないマークを表示する #8349

Draft
wants to merge 8 commits into
base: main
Choose a base branch
from

Conversation

harada-webdev
Copy link
Contributor

@harada-webdev harada-webdev commented Feb 18, 2025

Issue

概要

ログインしているユーザーがメンターの場合に、入会して1週間以内の現役生と研修生のアイコンのclassにis-new-userを追加しました。ユーザーアイコンの周りを装飾するclassは、view側でis-#{user.primary_role}と定義されているので、primary_roleからnew-userを戻り値にして、is-new-userを生成しています。

is-#{user.primary_role}のclassを使ってユーザーアイコンの縁を色で囲むことができます。例えば、現在の実装では、is-graduateの場合には緑色で囲まれていたり、is-traineeの場合は黄色で囲まれています。そのため、is-new-userの場合にも、同様に何かしらの色で囲むことができます。

また、プロフィールページでは、is-graduateのclassを持つユーザーは卒業生、is-traineeを持つユーザーは研修生と表示されるので、is-new-userのクラスを持つユーザーは、プロフィールページに「新入生」と表示させるようにしました。

変更確認方法

  1. feature/add-mark-to-new-userをローカルに取り込む
    i. git fetch origin feature/add-mark-to-new-user
    ii. git checkout feature/add-mark-to-new-user
  2. foreman start -f Procfile.devで開発環境を立ち上げる
  3. ユーザー名komagata、パスワードtesttestでログインする
  4. 入会して1週間以内のユーザーのプロフィールページに移動する
  5. ユーザーアイコンの横に「新入生」が表示されていることを確認する

Screenshot

変更前

スクリーンショット 2025-02-19 043938

変更後(デザインを入れる前)

スクリーンショット 2025-02-19 044041

@harada-webdev
Copy link
Contributor Author

@machida
お疲れ様です
ユーザーのアイコンにマークを付けるためのclassであるis-new-userを実装しましたので、デザイン付けをお願いいたします🙏

@JunichiIto
Copy link
Contributor

@machida @harada-webdev
ちょっとすいません、今 @unikounio さんのコードレビュー模擬試験の練習をしているのですが、こちらのPRがコードレビューの練習の題材として良さそうなので、このPRのレビューを @unikounio さんにやってもらってもいいでしょうか?
よろしくお願いしますー。

@harada-webdev
Copy link
Contributor Author

@JunichiIto
デザイン付けがまだ行われていないのですが、自分は問題ないです!

@unikounio
Copy link
Contributor

@harada-webdev さん
ありがとうございます!
明日には完了させる予定ですので、今しばらくお待ちください🙏

@JunichiIto さん
ご調整ありがとうございます🙏

@unikounio unikounio self-requested a review February 25, 2025 22:14
Copy link
Contributor

@unikounio unikounio left a comment

Choose a reason for hiding this comment

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

@harada-webdev さん
何点かコメントさせていただきました~
お手すきの際にご確認をお願いします🙏

@@ -11,8 +11,8 @@ def initialize(product:, is_mentor:, is_admin:, current_user_id:, reply_deadline
@display_user_icon = display_user_icon
end

def role_class
"is-#{@product.user.primary_role}"
def role_class(user: helpers.current_user)
Copy link
Contributor

Choose a reason for hiding this comment

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

普通の引数でよいかもしれません。
呼び出し側をみる限り、キーワード引数じゃなくてもuserを指定していることは伝わりそうに見えました👀
同様の箇所がいくつかありそうなので、もし修正される場合はそちらも合わせて対応していただけると良さそうです。
【参考】Docs: なんでもかんでもキーワード引数にしない | FBC

Copy link
Contributor Author

@harada-webdev harada-webdev Feb 27, 2025

Choose a reason for hiding this comment

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

デフォルトで引数を指定する際、キーワード引数を使わないといけないと勘違いしていました😭
下記の例のようにして、関連する箇所すべてキーワード引数を使わないように修正しました。

def role_class(user= helpers.current_user)

commit: キーワード引数ではなくデフォルト引数を使うようにした

@@ -15,12 +15,13 @@ def roles
roles = role_list.find_all { |v| v[:value] }
.map { |v| v[:role] }
roles << :student if roles.empty?
roles.unshift('new-user') if user&.mentor? && (roles & %i[student trainee]).any? && elapsed_days <= 7
Copy link
Contributor

Choose a reason for hiding this comment

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

  • 呼び出し側を確認すると、引数として渡されるuserには「ログインしているユーザー」と「アイコンを表示するユーザー」の2パターンがあるように見えました👬
    後者の場合、「アイコンを表示するユーザー」がメンターかどうかを確認することになりそうです。
    プロフィールページの「新入生」は正しく動いていそうでしたが、アイコンの装飾をしたときにharadaさんの意図しない動きをするかもしれないなと思いました。
  • 条件式のネストが少し深いので、変数に切り出すと読みやすくなりそうです。
  • 7がマジックナンバーになっているので、定数化すると意図が明確になるかもです。

Copy link
Contributor Author

@harada-webdev harada-webdev Feb 27, 2025

Choose a reason for hiding this comment

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

呼び出し側を確認すると、引数として渡されるuserには「ログインしているユーザー」と「アイコンを表示するユーザー」の2パターンがあるように見えました👬

確かに2パターンありました!ユーザーアイコン(新入生のアイコン)を表示するために、roleprimary_roleを使う場合には、それらに渡す引数をcurrent_userのみに限定するようにしました。

また、テストコードで上記のメソッドを使う場合は、新入生アイコンの表示とテストの目的が関係ないので、引数をレシーバとするように設定しています。(テスト側でcurrent_userが定義されていないことがあり、その際にrolesなどのデフォルト引数のcurrent_userを参照するとエラーが起きるので、それを回避する目的もあります)

commit: アイコンを表示する際に、current_userを引数とした

Copy link
Contributor Author

Choose a reason for hiding this comment

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

条件式のネストが少し深いので、変数に切り出すと読みやすくなりそうです。

new_student_or_trainee = (roles & %i[student trainee]).any? && elapsed_days <= FIRSTWEEK
roles.unshift('new-user') if new_student_or_trainee && user&.mentor?

上記のように、新入生かどうかの真偽値を返す箇所を変数に切り出しました

キーワード引数ではなくデフォルト引数を使うようにした

Copy link
Contributor Author

@harada-webdev harada-webdev Feb 27, 2025

Choose a reason for hiding this comment

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

7がマジックナンバーになっているので、定数化すると意図が明確になるかもです。

7という数字は最初の1週間という意味なので、7を格納した定数FIRSTWEEKを使用することにしました

commit: 入って最初の1週間を表す7を定数に格納した

Copy link
Contributor

Choose a reason for hiding this comment

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

Descriptionで

ログインしているユーザーがメンターの場合に、入会して1週間以内の現役生と研修生のアイコンのclassにis-new-userを追加しました。

とご説明いただいているのに対して、テストケースが少し少なめに見えました👀

  1. ログインしているユーザーが
  2. メンターの場合に
  3. 入会して1週間以内の
  4. 現役生と研修生

というように結構限定的な条件になっているので、もう少しテストを追加するとより安心できるかもです😌

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@unikounio
こちら、時間の都合上、入会して1週間以内の現役生の場合のテストのみしか作れませんでした。
入会して1週間以内の研修生の場合のテストは後程作りますので、まずは、そのテストのみの確認をお願いいたします。申し訳ございません🙏

commit: 戻り値がnew-userになるかのテストを追加した

Copy link
Contributor

@unikounio unikounio left a comment

Choose a reason for hiding this comment

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

Commentのみの意図だったのですが誤ってRequest changesにしてしまいました!
申し訳ないです🙏

@harada-webdev
Copy link
Contributor Author

@unikounio
レビューありがとうございます!
明日(27日)までに修正しますのでよろしくお願いいたします🙏

@harada-webdev harada-webdev force-pushed the feature/add-mark-to-new-user branch from c4baa1e to d34c909 Compare February 27, 2025 13:21
@harada-webdev harada-webdev force-pushed the feature/add-mark-to-new-user branch from d34c909 to e8d6de8 Compare February 27, 2025 14:28
@harada-webdev
Copy link
Contributor Author

@unikounio
レビューありがとうございます!
修正を行いましたので、ご確認のほどよろしくお願いいたします🙏

Copy link
Contributor

@unikounio unikounio left a comment

Choose a reason for hiding this comment

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

ご対応・ご説明ありがとうございます🙏

いくつかコメントさせていただきました。
どれも「こうするとさらに良くなるかも」という内容ですので、ご参考までにご確認いただければと思います😊
機能としては大きな問題はないと思いましたので、Approveさせていただきます!

ただ、テストの事情を本番の実装で対応している点は技術負債になる可能性もありそうなので、その点については引き続きご検討いただけると嬉しいです💡

controller.stub(:current_user, users(:komagata)) do
new_student = decorate(users(:otameshi))

assert_equal 'new-user', new_student.primary_role
Copy link
Contributor

Choose a reason for hiding this comment

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

アサーションを追加するなどして、new_studentが「入会から1週間以内の現役生」であることがわかるようにすると、意図がより伝わりやすくなるかもしれません🔰

@@ -16,11 +18,14 @@ def roles
.map { |v| v[:role] }
roles << :student if roles.empty?

new_student_or_trainee = (roles & %i[student trainee]).any? && elapsed_days <= FIRSTWEEK
Copy link
Contributor

Choose a reason for hiding this comment

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

&&の前後も変数化するとさらに読みやすくなるかもしれません!
(以下、あくまで一例ですがご参考までに)

is_student_or_trainee = (roles & %i[student trainee]).any?
is_within_first_week = elapsed_days <= FIRSTWEEK

new_student_or_trainee = is_student_or_trainee && is_within_first_week

@@ -3,7 +3,8 @@
- if @display_user_icon
.card-list-item__user
= link_to user_url(@product.user), class: "card-list-item__user-link" do
span class=["a-user-role", role_class]
- target_user = defined?(current_user) ? current_user : @product.user
span class=["a-user-role", role_class(target_user)]
Copy link
Contributor

@unikounio unikounio Feb 28, 2025

Choose a reason for hiding this comment

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

テスト側でcurrent_userが定義されていないケースがある、という事情だったんですね!
ただ、テスト側の事情を汲むために、本番の実装に特別な処理が入っている状態は、後々理解が難しくなってしまいそうなので、少し気になりました💦

可能であれば、テスト側でcurrent_userを適切にセットする形にして、本番の実装は「ログインユーザーがメンターかどうか」を素直に見る形にした方が、設計としてはシンプルに保てるかなと思いました!
もしuserが常にcurrent_userになるようなら、関連するメソッドの引数も不要になりそうです。

def roles
FIRSTWEEK = 7

def roles(user = current_user)
Copy link
Contributor

Choose a reason for hiding this comment

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

@harada-webdev
質問なんですが、

Suggested change
def roles(user = current_user)
def roles

とした上で、

roles.unshift('new-user') if new_student_or_trainee && current_user&.mentor?

にすると都合が悪くなるケースってありますか?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants