Skip to content

01 suggestion#1

Open
yusukedayo wants to merge 41 commits intodevelopfrom
01_suggestion
Open

01 suggestion#1
yusukedayo wants to merge 41 commits intodevelopfrom
01_suggestion

Conversation

@yusukedayo
Copy link
Owner

卒業試験 事業を提案しようの解答を提出しました。
ご確認よろしくお願いします。

Copy link

@kerochelo kerochelo left a comment

Choose a reason for hiding this comment

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

@yusukedayo
LGTMですー!実装に入っていきましょう!
LGTM

@yusukedayo
Copy link
Owner Author

yusukedayo commented Jun 14, 2022

卒業試験の課題実装が完了したので確認お願いします。

質問したいこと

  • 男女比カラムで1~3が選択されているイベントに申し込むには性別を登録する必要がある。
  • 男性限定・女性限定のイベントには該当する性別の人しか申し込みできないようにする。

上の2点において今回はeventモデルに判定するためのメソッドを書き、attendances_controllerのcreateアクションでif文の判定がfalseな場合は申し込みができないという方法で実装を行いました。

しかし、この方法だとそれぞれの判定をまとめてtrueかfalseを判断する形になってしまうため失敗した場合のメッセージが1種類しか表示することができずユーザーにとってわかりにくいのではと思いました。

そこで、eventモデルにメソッドを定義するのではなくevent_attendance.rbモデルにバリデーションの形で判定のロジックを記述しそれぞれに異なるエラーメッセージを持たせる方が良いと思ったのですがこの考えが正しいかお聞きしたいです。

Copy link

@kerochelo kerochelo left a comment

Choose a reason for hiding this comment

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

@yusukedayo
そうですね、考え方的にはeventモデルにはeventに関するロジック、event_attendanceモデルに関しては参加周りのロジックをまとめておくのが良さそうですね!
いったんevent_attendanceモデルにロジック組んでみる感じでリファクタしてみましょう!

その他ちょこちょこレビューしてますのでご確認をお願いしますー:pray:

!past?
end

def check(user)

Choose a reason for hiding this comment

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

メソッド名がcheckだけだと、どんなチェックをするのかわかりにくいので、メソッド名をちょっと具体的にしても良さそうですね、そうすることでそのメソッドを使っているviewファイルをみるときにコードが読みやすくなります!

Copy link
Owner Author

Choose a reason for hiding this comment

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

変数名に関して修正しました。
9f970dd

gender_ratio == 'only_man' && user.gender == 'woman'
end

def check_participants?

Choose a reason for hiding this comment

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

こちらは参加可能かどうかの判定ロジックですよね!
であればメソッド名は、can_be_participanted?がいいかなとー(beは短縮してもokです)
ロジック名のイメージは英文を作る感じです!
これを例にすると、eventモデル(目的語)でpartcipantsメソッド(動詞)でuser(主語)って感じです

Copy link
Owner Author

Choose a reason for hiding this comment

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

ご指摘ありがとうございます!
変数名にわかりやすい名前を付けること意識できていなかったので今後気をつけます!
該当箇所は以下のコミットで修正しました。
9f970dd

end
end

def check_all_number

Choose a reason for hiding this comment

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

ここも出席制限数のチェックなので、わかりやすいメソッド名にしちゃいましょう!

Copy link
Owner Author

Choose a reason for hiding this comment

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

該当箇所は以下のコミットで修正しました。
9f970dd

Comment on lines +96 to +105
<% if @event.check_participants? %>
<%= link_to "参加する",
event_attendance_url(@event),
class: 'btn btn-primary',
method: :post,
data: { confirm: '申し込みます' }
%>
%>
<% else %>
満員です
<% end %>

Choose a reason for hiding this comment

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

男性限定や女性限定のイベントの場合の参加ボタンの表示もロジックを作って制御しちゃいましょう!

Copy link
Owner Author

Choose a reason for hiding this comment

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

こちら修正しました。
fcea2ce

</div>
<div class="mb-3">
<%= f.label :gender %>
<%= f.enum_select :gender, class: 'form-control' %>

Choose a reason for hiding this comment

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

恥ずかしながら知らなかったですー、これ利用できるgemあるんですね!

Copy link
Owner Author

Choose a reason for hiding this comment

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

参考にしたqiita記事を書いた方がそのままgem化して下さってました!
スッキリ書けるので助かってます。
https://qiita.com/tanakaworld/items/ece8718974e14d5a1e55

@yusukedayo
Copy link
Owner Author

yusukedayo commented Jun 17, 2022

3e14f9d

ご指摘ありがとうございます!
eventモデルに書いていたロジックはevent_attendanceモデルにバリデーションの形で移動させました。
1295ebb
3e14f9d
47379e2

その他のご指摘いただいた部分に関しても修正したので再度確認よろしくお願いします。

Copy link

@kerochelo kerochelo left a comment

Choose a reason for hiding this comment

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

@yusukedayo
いい感じになりましたね!
最後に1点だけコメントしましたのでご確認くださいー:pray:

Comment on lines +19 to +29
def check_set_woman
return unless Event.find(event_id).gender_ratio == 'only_woman' && User.find(user_id).gender == 'man'

errors.add(:user_id, 'このもくもく会は女性限定です')
end

def check_set_man
return unless Event.find(event_id).gender_ratio == 'only_man' && User.find(user_id).gender == 'woman'

errors.add(:user_id, 'このもくもく会は男性限定です')
end

Choose a reason for hiding this comment

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

これは1つのメソッドにしちゃってもいいですね!

def check_gender
...

Copy link
Owner Author

Choose a reason for hiding this comment

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

ご指摘いただいた通りcheck_genderで1つのメソッドに変更しました!
確認お願いします。
ac90fe9

@kerochelo
Copy link

@yusukedayo
LGTMです!お疲れ様でしたー
LGTM

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.

2 participants