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

Kumo raku step12 add end date #1459

Merged
merged 8 commits into from
Dec 12, 2022
Merged

Conversation

KumoRaku
Copy link

@KumoRaku KumoRaku commented Dec 9, 2022

  • タスクに対して、終了期限を登録できるようにしてみましょう
  • 一覧画面で、終了期限でソートできるようにしましょう
  • specを拡充しましょう
  • PRしてレビューをしてもらったら、リリースしてみましょう

https://github.com/Fablic/training/blob/develop/steps_jp.md#%E3%82%B9%E3%83%86%E3%83%83%E3%83%9712-%E7%B5%82%E4%BA%86%E6%9C%9F%E9%99%90%E3%82%92%E8%BF%BD%E5%8A%A0%E3%81%97%E3%82%88%E3%81%86--%E3%82%B9%E3%82%AD%E3%83%83%E3%83%97%E5%8F%AF%E8%83%BD

@KumoRaku KumoRaku changed the title Kumo raku step13 add end date Kumo raku step12 add end date Dec 9, 2022
@KumoRaku KumoRaku requested review from zackey-y, kenkaton and sourcewc and removed request for zackey-y and kenkaton December 9, 2022 01:21
Comment on lines 5 to 9
@tasks = case params[:sort]
when 'latest'
Task.latest
when 'expiring'
Task.expiring
Copy link

@sourcewc sourcewc Dec 9, 2022

Choose a reason for hiding this comment

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

case whenを使って判定すると今後パラメータの値が増えた時にコントローラーが膨大になりそうです。極端にいうと方向asc desc両方対応 + クリアまで考えた時に5つの条件ができると思います。
modelに判定を書くとコントローラーを軽くかけるのでこういう書き方はどうですか?

index

def index
    @tasks = Task.expiring(params[:sort]).latest
 end

myapp/app/models/task.rb

  scope :latest, -> { order(created_at: :desc) }
  scope :expiring, ->(sort) { order(end_date: :asc) if sort.eql?('expiring') }

もっというとselect boxなどを作ってasc descの部分をパラメータとかで取得するようにし、
end_date前後をボタン繰り返し選択で変えっるようにするともっといいと思います

Copy link
Author

Choose a reason for hiding this comment

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

ご指摘ありがとうございます。
確かにそうです、
今後時間の余裕があればこういうロジックをusecase層で実装したいなぁと思ってますが、
今回はmodelでsearchメソットを追加してそこで実装する。
Step13で検索機能が入ってるのでそれも兼用できると思います。
いかがでしょうか?

Comment on lines 7 to 8
scope :latest, -> { order(created_at: :desc) }
scope :expiring, -> { order(end_date: :asc) }
Copy link

Choose a reason for hiding this comment

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

コントローラー部分の書き方を参考にしてください

Copy link
Author

Choose a reason for hiding this comment

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

修正してみます。

Comment on lines 19 to 21
context 'when there are tasks,' do
let!(:task1) { FactoryBot.create(:task) }
let!(:task2) { FactoryBot.create(:task, title: 'Spec2', description: 'test2') }
let!(:task2) { FactoryBot.create(:task, title: 'Spec2', description: 'test2', end_date: Time.new(2023, 11, 1, 10, 30)) }
Copy link

@sourcewc sourcewc Dec 9, 2022

Choose a reason for hiding this comment

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

let!にする必要はないと思いました。
let!は遅延評価されてパフォーマンス的に良くないと思います。
あとlet!を使う必要があるようなデータを作成するようにも見えませんでした。
before内でまとめましょうか

before do
  FactoryBot.create(:task)
  FactoryBot.create(:task, title: 'Spec2', description: 'test2', end_date: Time.new(2023, 11, 1, 10, 30))
end

Copy link

Choose a reason for hiding this comment

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

参考議論なども併せて確認してください
rubocop/rspec-style-guide#55 (comment)

rubocop/rubocop-rspec#94 (comment)

Copy link
Author

Choose a reason for hiding this comment

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

ありがとうございます。
修正します。

@KumoRaku
Copy link
Author

KumoRaku commented Dec 9, 2022

@sourcewc さん
お疲れ様です、コメントありがとうございます。
返信、修正しましたので、
お手数ですが、再レビューお願いいたします。

Comment on lines 7 to 9
def self.search(conditions)
order("#{conditions['sort_column']} #{conditions['sort_direction']}")
end
Copy link

Choose a reason for hiding this comment

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

こういう書き方にするとユーザが任意の操作でデータベースの並び順を帰れます。
あと間違ったパラメータを渡すとアプリケーションエラーが発生するので直しましょう、、

Search用で使用するメソッドではないと思います

Copy link
Author

@KumoRaku KumoRaku Dec 9, 2022

Choose a reason for hiding this comment

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

こういう書き方にするとユーザが任意の操作でデータベースの並び順を帰れます。

すみません、ここは理解できなかった。もう詳しく説明お願いいただければありがたいです。

あと間違ったパラメータを渡すとアプリケーションエラーが発生するので直しましょう、、

ユーザが間違ったパラメーターに編集するとエラーページに遷移するとは正しいと思ってますが、
一応リクエストパラメーターのバリデーションを追加します。パラメーターが正しいときだけ結果返すようにします。

Search用で使用するメソッドではないと思います

検索機能を実装したときこのメソッドにwhere処理を追加して、検索ボタンを押すとこのメソッドを呼んで結果をとるつもりですが、何かご懸念点ございますか?

Copy link
Author

Choose a reason for hiding this comment

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

Shinさんがアドバイスした実装方法も考えてみましたが、今後たとえ並び方法が多くなったらコントローラー側は

def index
  @tasks = Task.expiring(params[:sort]).latest(params[:sort]).newest(params[:sort])... ...
end

みたいになちゃいます。latest.newestみたいなソースを読むと意味わかりづらいので、
同じ機能のscopeを全部繋ぐのは避けたいということを考えてます。mm

Choose a reason for hiding this comment

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

こういう書き方にするとユーザが任意の操作でデータベースの並び順を「変えれます」でした。。すみません誤字です。

この辺をもっと説明すると
taskで並び順を変えたいところ以外のカラムを入力しても動きます例えば現在はcreated_atを並び順変えを入れているのにdescriptionなど他のカラムをパラメーターに渡して入力しても並び順を変えれます。

こちらは開発者が意図してない並び順変更ができてしまい、
今後indexがない状態のカラムで膨大なデータがある場合、
並び順変更だけでtimeoutエラーとかDBへの負荷も高まる可能性があります。
あと意図してないカラムを他の人に漏出してしまうのも良くないと思うので
意図したカラム以外の入力はできなくしたいですね

あとアプリケーションエラーの補足説明ですが、
urlで入力するカラム名を変え入力すると500系エラーが表示されます。
一般にはアプリケーションの動作が止まることはないと思います404に移動するか、
並び順が変わったりなど動作は担保されるはずです。
image

Choose a reason for hiding this comment

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

Shinさんがアドバイスした実装方法も考えてみましたが、今後たとえ並び方法が多くなったらコントローラー側は

def index
  @tasks = Task.expiring(params[:sort]).latest(params[:sort]).newest(params[:sort])... ...
end

みたいになちゃいます。latest.newestみたいなソースを読むと意味わかりづらいので、 同じ機能のscopeを全部繋ぐのは避けたいということを考えてます。mm

sortと並び順をパラメータに渡すことでlatest、newestなどはなくせるのでは?

Choose a reason for hiding this comment

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

scopeに全部書くを避けたいイメージはどれほどの列の並び変えを考えてます?
最初のちょうさんが書いた状態からasc descの変更ができればいいと思いましたが、、

@KumoRaku
Copy link
Author

@sourcewc
ご詳しい説明ありがとうございます!
いろいろ理解して考えた上、ソースを修正しました。
お手数ですが、再レビューお願いいただけますでしょうか?

Copy link

@sourcewc sourcewc left a comment

Choose a reason for hiding this comment

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

修正お疲れ様でした!
良さそうです!
LGTM

Base automatically changed from KumoRaku-Step11-validation to KumoRaku December 12, 2022 13:23
@KumoRaku KumoRaku merged commit b6a2f83 into KumoRaku Dec 12, 2022
@KumoRaku KumoRaku deleted the KumoRaku-Step12-add-end-date branch December 12, 2022 13:24
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