-
Notifications
You must be signed in to change notification settings - Fork 72
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
base: main
Are you sure you want to change the base?
Conversation
@machida |
@machida @harada-webdev |
@JunichiIto |
@harada-webdev さん @JunichiIto さん |
There was a problem hiding this 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) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
普通の引数でよいかもしれません。
呼び出し側をみる限り、キーワード引数じゃなくてもuser
を指定していることは伝わりそうに見えました👀
同様の箇所がいくつかありそうなので、もし修正される場合はそちらも合わせて対応していただけると良さそうです。
【参考】Docs: なんでもかんでもキーワード引数にしない | FBC
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- 呼び出し側を確認すると、引数として渡される
user
には「ログインしているユーザー」と「アイコンを表示するユーザー」の2パターンがあるように見えました👬
後者の場合、「アイコンを表示するユーザー」がメンターかどうかを確認することになりそうです。
プロフィールページの「新入生」は正しく動いていそうでしたが、アイコンの装飾をしたときにharadaさんの意図しない動きをするかもしれないなと思いました。 - 条件式のネストが少し深いので、変数に切り出すと読みやすくなりそうです。
7
がマジックナンバーになっているので、定数化すると意図が明確になるかもです。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
呼び出し側を確認すると、引数として渡されるuserには「ログインしているユーザー」と「アイコンを表示するユーザー」の2パターンがあるように見えました👬
確かに2パターンありました!ユーザーアイコン(新入生のアイコン)を表示するために、role
やprimary_role
を使う場合には、それらに渡す引数をcurrent_user
のみに限定するようにしました。
また、テストコードで上記のメソッドを使う場合は、新入生アイコンの表示とテストの目的が関係ないので、引数をレシーバとするように設定しています。(テスト側でcurrent_user
が定義されていないことがあり、その際にroles
などのデフォルト引数のcurrent_user
を参照するとエラーが起きるので、それを回避する目的もあります)
commit: アイコンを表示する際に、current_userを引数とした
There was a problem hiding this comment.
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?
上記のように、新入生かどうかの真偽値を返す箇所を変数に切り出しました
There was a problem hiding this comment.
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を定数に格納した
test/system/generations_test.rb
Outdated
There was a problem hiding this comment.
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週間以内の
- 現役生と研修生
というように結構限定的な条件になっているので、もう少しテストを追加するとより安心できるかもです😌
There was a problem hiding this comment.
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になるかのテストを追加した
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Commentのみの意図だったのですが誤ってRequest changesにしてしまいました!
申し訳ないです🙏
@unikounio |
c4baa1e
to
d34c909
Compare
d34c909
to
e8d6de8
Compare
@unikounio |
There was a problem hiding this 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 |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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)] |
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@harada-webdev
質問なんですが、
def roles(user = current_user) | |
def roles |
とした上で、
roles.unshift('new-user') if new_student_or_trainee && current_user&.mentor?
にすると都合が悪くなるケースってありますか?
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
のクラスを持つユーザーは、プロフィールページに「新入生」と表示させるようにしました。変更確認方法
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
foreman start -f Procfile.dev
で開発環境を立ち上げるkomagata
、パスワードtesttest
でログインするScreenshot
変更前
変更後(デザインを入れる前)