diff --git a/app/controllers/questions_controller.rb b/app/controllers/questions_controller.rb index cbb96866e34..762dc2d26ad 100644 --- a/app/controllers/questions_controller.rb +++ b/app/controllers/questions_controller.rb @@ -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 @@ -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? diff --git a/app/models/question_callbacks.rb b/app/models/question_callbacks.rb index 6b757d90c3a..854bc8810f1 100644 --- a/app/models/question_callbacks.rb +++ b/app/models/question_callbacks.rb @@ -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 @@ -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 diff --git a/test/system/questions_test.rb b/test/system/questions_test.rb index 05ab86d98ef..b520a85c913 100644 --- a/test/system/questions_test.rb +++ b/test/system/questions_test.rb @@ -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 + assert_text '質問を作成しました。' + + visit_with_auth questions_path, 'komagata' + 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