Skip to content

Commit

Permalink
質問本公開時のみメンターのWatchを自動的にonにする修正とテスト追加
Browse files Browse the repository at this point in the history
  • Loading branch information
Paru871 committed Mar 29, 2022
1 parent e62d0e6 commit 9ca3b0d
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 19 deletions.
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
45 changes: 43 additions & 2 deletions test/system/questions_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -228,13 +228,54 @@ 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
visit_with_auth questions_path, 'komagata'
click_link 'メンターのみ投稿された質問が"Watch中"になるテスト'
assert_text 'kimura (Kimura Tadasi)'
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本文'
end
click_button 'WIP'
assert_text '質問をWIPとして保存しました。'
visit_with_auth questions_path, 'komagata'
click_link 'WIPタイトル'
assert_text 'kimura (Kimura Tadasi)'
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本文'
end
click_button 'WIP'
assert_text '質問をWIPとして保存しました。'
visit questions_path
click_link 'WIPタイトル'
assert_text 'kimura (Kimura Tadasi)'
click_button '内容修正'
within 'form[name=question]' do
fill_in 'question[title]', with: '更新されたタイトル'
fill_in 'question[description]', with: '更新された本文'
end
click_button '質問を公開'
assert_text '質問を更新しました'
visit_with_auth questions_path, 'komagata'
click_link '更新されたタイトル'
assert_text 'kimura (Kimura Tadasi)'
assert_text 'Watch中'
end

Expand Down

0 comments on commit 9ca3b0d

Please sign in to comment.