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

[Q&A]質問の本公開時にメンター側のWatchを自動的にonにするように修正 #4530

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 0 additions & 17 deletions app/controllers/questions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ def create
@question.user = current_user
@question.wip = params[:commit] == 'WIP'
if @question.save
create_mentors_watch
redirect_to @question, notice: notice_message(@question)
else
render :new
Expand Down Expand Up @@ -90,22 +89,6 @@ def questions_property
end
end

def create_mentors_watch
Watch.insert_all(watch_records) # rubocop:disable Rails/SkipsModelValidations
end

def watch_records
User.mentor.map do |mentor|
{
watchable_type: 'Question',
watchable_id: @question.id,
created_at: Time.current,
updated_at: Time.current,
user_id: mentor.id
}
end
end

def notice_message(question)
return '質問をWIPとして保存しました。' if question.wip?

Expand Down
18 changes: 18 additions & 0 deletions app/models/question_callbacks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ def after_save(question)
return unless question.saved_change_to_attribute?(:published_at, from: nil)

send_notification_to_mentors(question)
create_mentors_watch(question)
notify_to_chat(question)
Cache.delete_not_solved_question_count
end
Expand Down Expand Up @@ -32,4 +33,21 @@ def send_notification_to_mentors(question)
def delete_notification(question)
Notification.where(link: "/questions/#{question.id}").destroy_all
end

def create_mentors_watch(question)
watch_question_records = watch_records(question)
Watch.insert_all(watch_question_records) # rubocop:disable Rails/SkipsModelValidations
end

def watch_records(question)
User.mentor.map do |mentor|
{
watchable_type: 'Question',
watchable_id: question.id,
created_at: Time.current,
updated_at: Time.current,
user_id: mentor.id
}
end
end
end
50 changes: 48 additions & 2 deletions test/system/questions_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -228,13 +228,59 @@ class QuestionsTest < ApplicationSystemTestCase
assert_selector '.thread-list-item', count: 25
end

test 'mentor create a question' do
visit_with_auth new_question_path, 'komagata'
test "mentor's watch-button is automatically on when new question is published" do
visit_with_auth new_question_path, 'kimura'
within 'form[name=question]' do
fill_in 'question[title]', with: 'メンターのみ投稿された質問が"Watch中"になるテスト'
fill_in 'question[description]', with: 'メンターのみ投稿された質問が"Watch中"になるテスト'
click_button '登録する'
end
Comment on lines 233 to 237
Copy link
Contributor

Choose a reason for hiding this comment

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

click_buttonがブロック内外にありますので統一した方が良いかと思いました。
本ファイルを上から見ていくと、ブロック内で統一された方がよさそうです。

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ありがとうございます!
今回作成したテスト3つについて、ブロック内にclick_buttonを書くように統一いたしました。

assert_text '質問を作成しました。'

visit_with_auth questions_path, 'komagata'
Copy link
Contributor

@aim2bpg aim2bpg Apr 8, 2022

Choose a reason for hiding this comment

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

visit_with_authの前に空行があると見やすくなるのではと思いました。
そういえば、この前には「質問を作成しました。」はチェックされないのですね。
以降では、メッセージをチェックしているのでここも統一感を持たせた方が良いかと思いました。

Copy link
Contributor Author

Choose a reason for hiding this comment

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

「質問を作成しました。」のチェックが抜けていましたので追加しました。
見つけてくださってありがとうございます!助かりましたー🙏
統一感が大事、しっかり覚えたいと思います。

Copy link
Contributor Author

Choose a reason for hiding this comment

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

visit_with_authの前に空行を作るのも、今回作成した3つのテストについて統一して修正しました。
ありがとうございます!

click_link 'メンターのみ投稿された質問が"Watch中"になるテスト'
assert_text '削除する'
assert_text 'Watch中'
end

test "mentor's watch-button is not automatically on when new question is created as WIP" do
visit_with_auth new_question_path, 'kimura'
within 'form[name=question]' do
fill_in 'question[title]', with: 'WIPタイトル'
fill_in 'question[description]', with: 'WIP本文'
click_button 'WIP'
end
assert_text '質問をWIPとして保存しました。'

visit_with_auth questions_path, 'komagata'
click_link 'WIPタイトル'
assert_text '削除する'
assert_no_text 'Watch中'
end

test "mentor's watch-button is automatically on when WIP question is updated as published" do
visit_with_auth new_question_path, 'kimura'
within 'form[name=question]' do
fill_in 'question[title]', with: 'WIPタイトル'
fill_in 'question[description]', with: 'WIP本文'
click_button 'WIP'
end
assert_text '質問をWIPとして保存しました。'

visit questions_path
click_link 'WIPタイトル'
assert_text '削除する'
click_button '内容修正'
within 'form[name=question]' do
fill_in 'question[title]', with: '更新されたタイトル'
fill_in 'question[description]', with: '更新された本文'
click_button '質問を公開'
end
assert_text '質問を更新しました'

visit_with_auth questions_path, 'komagata'
click_link '更新されたタイトル'
assert_text '削除する'
assert_text 'Watch中'
end

Expand Down