-
Notifications
You must be signed in to change notification settings - Fork 1
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
Feature/#34 add tags to memos #79
Conversation
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.
@nova818
一旦テスト以外で気になったところをコメントしました!
…ationsのみincludeする
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.
@nova818
コメントしました!
前回よりかなりコードが整理されてきましたね! 🎉
@@ -16,22 +16,24 @@ def show | |||
|
|||
# POST /memos | |||
def create | |||
memo = Memo.new(memo_params) | |||
if memo.save | |||
memo_form = Memo::BuildForm.new(params: create_params) |
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.
IMO/Simple
formで伝わるかなと思います!
memo_form = Memo::BuildForm.new(params: create_params) | |
form = Memo::BuildForm.new(params: create_params) |
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.
確かにmemoつけなくて良さそうですね🙆♂️
end | ||
end | ||
|
||
# PUT /memos/:id | ||
def update | ||
memo = Memo.find(params[:id]) | ||
memo_form = Memo::UpdateForm.new(params: update_params, memo: memo) |
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.
こちらも同上です!
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.
併せて修正します🫡
end | ||
|
||
def update_memo_params | ||
params.require(:memo).permit(:content) | ||
def update_params |
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.
@rikuya98 @nova818
[仕様の確認]
メモはタイトルの更新はできない、でいいんでしたっけ?
(zennとかQiitaとか、esaとかだとtitleも更新できる)
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.
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.
タイトルも更新可能にすることになったかと思いますので、そのような認識で進めます!
|
||
attr_reader :params | ||
|
||
validate :memo_valid? |
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.
Must
- 以下のPRで使用したcascadeバリデーターを使用するようにしましょう!(使い方はPRの説明部分に書いてあります!)
- 子モデルのバリデーションエラーをcascadeするバリデータを追加する #81
- ちなみにこれはEachValidatorを継承したカスタムバリデータです。
- 子モデルのバリデーションエラーをcascadeするバリデータを追加する #81
- memo_tagsのバリデーションも実行するようにしましょう!(
memo_idとtag_idの組み合わせ
の一意性検証バリデーションを実行したい。)
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.
cascadeバリデーターを利用する形に修正します!
ただ、実力不足からcascadeバリデーターのコードで詳細に理解できていない部分があり、引き続きコードリーディングを進めます...😢
@params = params | ||
end | ||
|
||
def memo |
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.
Must
このメソッドは外部に公開する必要はありますか?
みたところその必要性はなさそうですので、privateメソッドでいいと思いました!
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.
確かに、memoはクラス内部でのみ仕様されるメソッドですね💦
privateメソッドに変更します🙆♂️
let(:params) { { title: 'Test Memo', content: 'This is a test memo', tag_ids: tag_ids } } | ||
let(:memo_build_form) { described_class.new(params: params) } | ||
|
||
specify 'メモが新規作成されること' do |
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.
IMO
MemoTagのカウントも確認したいです!
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.
MemoTagのカウントを確認するコードを追加します🙆♂️
let(:memo_build_form) { described_class.new(params: params) } | ||
|
||
specify 'メモが新規作成されないこと' do | ||
expect { memo_build_form.save }.not_to(change(Memo, :count)) |
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.
TagMemoのカウントも確認したいです!
backend/spec/requests/memos_spec.rb
Outdated
expect do | ||
post '/memos', params: { memo_form: invalid_memo_form_params }, as: :json | ||
end.not_to change(Memo, :count) | ||
end.not_to change(Tag, :count) |
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.
Question
Tagではなく、MemoTagの個数を確認するべきではないでしょうか? 🤔
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.
MemoTagの個数を確認すべきですね。なんでタグの数を確認したんだろう...笑
確認不足でした💦 修正します🙏
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.
BuildFormでコメントしたことを参考にして、修正してみて欲しいです!
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.
承知しました!詳細なコメントありがとうございます🙇♂️
end | ||
|
||
describe '#update' do | ||
context 'フォームの値が有効な場合' do |
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.
既存のmemo.memo_tagsが存在する場合もテストしたいです!
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.
こちら、テストの冒頭書き部分で既存のmemo_tagsを作成していたため、既存のmemo.memo_tagsが存在する場合のテストはできているのかなと考えていたのですが、下記とは別に改めてテストケースを追加した方が良いということでしょうか?🤔
before { create(:memo_tag, memo:, tag:) }
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.
すみません、こちらは対応不要ですね 💦
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.
@nova818
コメントしました
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.
IMO
memo_tags_to_removeとmemo_tags_to_addの部分が、
作成される部分と削除される部分の処理が追いづらいので、以下のように修正するといいと思います。
# frozen_string_literal: true
class Memo
class UpdateForm
include ActiveModel::Validations
validates :memo, cascade: true
validates :memo_tags, cascade: true, if: -> { errors.empty? }
def initialize(params:, id:)
@params = params
@id = id
end
def save
return false if invalid?
ActiveRecord::Base.transaction do
save_record!(memo)
memo.memo_tags.select(&:marked_for_destruction?)
.each { destroy_record!(_1) }
end
errors.empty?
end
private
attr_reader :params, :id
def save_record!(record)
return true if record.save
errors.add(:base, record.error_message)
raise ActiveRecord::Rollback
end
def memo
@memo ||= \
Memo.find(id)
.tap do |model|
model.assign_attributes(
title: params[:title],
content: params[:content],
tags: tags
)
end
end
def destroy_record!(record)
return true if record.destroy
errors.add(:base, record.error_message)
raise ActiveRecord::Rollback
end
def memo_tags
@memo_tags ||= memo.tap do |model|
mark_for_destruction_memo_tags!
end.memo_tags.reject(&:marked_for_destruction?)
end
def tag_ids = params[:tag_ids] || []
def tags = Tag.where(id: tag_ids)
def before_save_tag_ids
@before_save_tag_ids ||= memo.memo_tags.pluck(:tag_id)
end
def destroy_target_tag_ids = before_save_tag_ids - tag_ids
def mark_for_destruction_memo_tags!
memo.memo_tags.each do |memo_tag|
next unless destroy_target_tag_ids.include?(memo_tag.tag_id)
memo_tag.mark_for_destruction
end
end
end
end
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.
改善案の提示ありがとうございます🙇♂️
上記案に沿って修正を進めます。
title: params[:title] || model.title, | ||
content: params[:content] || model.content, |
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.
Must
フロントからは何も編集フォームを入力しなければ、
params[:hoge]
がnilではなく、createしたときの値のままでリクエストされるはずなので、この処理はおかしいと思います。
title: params[:title] || model.title, | |
content: params[:content] || model.content, | |
title: params[:title], | |
content: params[:content], |
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.
フロントからは何も編集フォームを入力しない場合、params[:hoge]がnilになると思い込んでいました💦
修正します。
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.
@nova818
コメントしました!
def memo_params | ||
params.require(:memo).permit(:title, :content, :poster) | ||
def form_params | ||
params.require(:form).permit(:title, :content, :poster, tag_ids: []) |
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.
Idiomatic
memoに関するものなので、memoをtopレベルのkeyとするようにしましょう!
params.require(:form).permit(:title, :content, :poster, tag_ids: []) | |
params.require(:memo).permit(:title, :content, :poster, tag_ids: []) |
@@ -45,7 +47,7 @@ def destroy | |||
|
|||
private | |||
|
|||
def memo_params | |||
params.require(:memo).permit(:title, :content, :poster) | |||
def form_params |
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.
IMO/Readable
memo_paramsのままにしておきましょう!
end | ||
end | ||
|
||
# PUT /memos/:id | ||
def update | ||
memo = Memo.find(params[:id]) | ||
# memo = Memo.find(params[:id]) |
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.
このコメントは不要なので消しましょう!
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.
こちら確認不足でした💦 余計な負担をお掛けしてしまい申し訳ありませんでした🙇♂️
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.
対応するissue
対応内容