From 14e04c51cac00cadc27105fe27288d276a048544 Mon Sep 17 00:00:00 2001 From: Gabriel Grubba Date: Wed, 8 May 2024 12:05:49 -0300 Subject: [PATCH] DEV: Update change status on solve implementation Update tests to verify that the change status on solve feature is working as expected. Change the implementation to loop throught the topic assignments and update the status. --- plugin.rb | 48 ++++++++++++++++++++++++++------- spec/integration/solved_spec.rb | 6 +++-- 2 files changed, 42 insertions(+), 12 deletions(-) diff --git a/plugin.rb b/plugin.rb index c38cc38..03a1f5b 100644 --- a/plugin.rb +++ b/plugin.rb @@ -591,19 +591,47 @@ after_initialize do if defined?(DiscourseAssign) on(:accepted_solution) do |post| next if SiteSetting.assignment_status_on_solve.blank? - assigned_user = User.find_by(id: post.topic.assignment.assigned_to_id) - Assigner.new(post.topic, assigned_user).assign( - assigned_user, - status: SiteSetting.assignment_status_on_solve, - ) + assignements = Assignment.where(topic: post.topic) + assignements.each do |assignment| + assigned_user = User.find_by(id: assignment.assigned_to_id) + target_id = assignment.target_id + + target = + case assignment.target_type + when "Post" + Post.find_by(id: target_id) + when "Topic" + Topic.find_by(id: target_id) + else + post.topic + end + + Assigner.new(target, assigned_user).assign( + assigned_user, + status: SiteSetting.assignment_status_on_solve, + ) + end end on(:unaccepted_solution) do |post| next if SiteSetting.assignment_status_on_unsolve.blank? - assigned_user = User.find_by(id: post.topic.assignment.assigned_to_id) - Assigner.new(post.topic, assigned_user).assign( - assigned_user, - status: SiteSetting.assignment_status_on_unsolve, - ) + assignements = Assignment.where(topic: post.topic) + assignements.each do |assignment| + assigned_user = User.find_by(id: assignment.assigned_to_id) + target_id = assignment.target_id + target = + case assignment.target_type + when "Post" + Post.find_by(id: target_id) + when "Topic" + Topic.find_by(id: target_id) + else + post.topic + end + Assigner.new(target, assigned_user).assign( + assigned_user, + status: SiteSetting.assignment_status_on_unsolve, + ) + end end end end diff --git a/spec/integration/solved_spec.rb b/spec/integration/solved_spec.rb index 06693d4..aac659f 100644 --- a/spec/integration/solved_spec.rb +++ b/spec/integration/solved_spec.rb @@ -466,7 +466,7 @@ RSpec.describe "Managing Posts solved status" do DiscourseSolved.accept_answer!(p1, user) - expect(p1.topic.assignment.status).to eq("Done") + expect(p1.reload.topic.assignment.reload.status).to eq("Done") DiscourseSolved.unaccept_answer!(p1) @@ -491,14 +491,16 @@ RSpec.describe "Managing Posts solved status" do expect(result[:success]).to eq(true) post_response = Fabricate(:post, topic: topic_question, user: user_3) + Assigner.new(post_response, user_3).assign(user_3) DiscourseSolved.accept_answer!(post_response, user_1) expect(topic_question.assignment.assigned_to_id).to eq(user_2.id) - + expect(post_response.assignment.assigned_to_id).to eq(user_3.id) DiscourseSolved.unaccept_answer!(post_response) expect(topic_question.assignment.assigned_to_id).to eq(user_2.id) + expect(post_response.assignment.assigned_to_id).to eq(user_3.id) end describe "assigned topic reminder"