DEV: Move the post and topic custom fields into a table

This PR does some preparatory work to prepare for the front-end display
of who marked an answer as solved. It migrates the custom fields from
discourse-solved of Topic and Post to a new table, except "accepted_answer_post_id"

ref: /t/-/95318
This commit is contained in:
Lhc_fl
2024-09-05 17:55:02 +08:00
parent 187b8bf19d
commit 150f7f4a35
9 changed files with 123 additions and 76 deletions
@@ -3,6 +3,10 @@ Fabricator(:custom_topic, from: :topic) do
transient :custom_topic_name
transient :value
after_create do |top, transients|
if (transients[:custom_topic_name] == DiscourseSolved::ACCEPTED_ANSWER_POST_ID_CUSTOM_FIELD)
post = Fabricate(:post)
Fabricate(:solution, topic_id: top.id, answer_post_id: post.id)
end
custom_topic =
TopicCustomField.new(
topic_id: top.id,
+6
View File
@@ -0,0 +1,6 @@
# frozen_string_literal: true
Fabricator(:solution, from: DiscourseSolved::Solution) do
topic_id
answer_post_id
end
+11 -15
View File
@@ -248,15 +248,13 @@ RSpec.describe "Managing Posts solved status" do
post "/solution/accept.json", params: { id: p1.id }
expect(response.status).to eq(200)
expect(p1.reload.custom_fields["is_accepted_answer"]).to eq("true")
expect(p1.reload.solution.present?).to eq(true)
topic.reload
expect(topic.public_topic_timer.status_type).to eq(TopicTimer.types[:silent_close])
expect(topic.custom_fields["solved_auto_close_topic_timer_id"].to_i).to eq(
topic.public_topic_timer.id,
)
expect(topic.solution.topic_timer_id).to eq(topic.public_topic_timer.id)
expect(topic.public_topic_timer.execute_at).to eq_time(Time.zone.now + 2.hours)
@@ -274,15 +272,13 @@ RSpec.describe "Managing Posts solved status" do
post "/solution/accept.json", params: { id: post_2.id }
expect(response.status).to eq(200)
expect(post_2.reload.custom_fields["is_accepted_answer"]).to eq("true")
expect(post_2.reload.solution.present?).to eq(true)
topic_2.reload
expect(topic_2.public_topic_timer.status_type).to eq(TopicTimer.types[:silent_close])
expect(topic_2.custom_fields["solved_auto_close_topic_timer_id"].to_i).to eq(
topic_2.public_topic_timer.id,
)
expect(topic_2.solution.topic_timer_id).to eq(topic_2.public_topic_timer.id)
expect(topic_2.public_topic_timer.execute_at).to eq_time(Time.zone.now + 4.hours)
@@ -322,7 +318,7 @@ RSpec.describe "Managing Posts solved status" do
p1.reload
topic.reload
expect(p1.custom_fields["is_accepted_answer"]).to eq("true")
expect(p1.solution.present?).to eq(true)
expect(topic.public_topic_timer).to eq(nil)
expect(topic.closed).to eq(true)
end
@@ -338,7 +334,7 @@ RSpec.describe "Managing Posts solved status" do
expect(response.status).to eq(200)
p1.reload
expect(p1.custom_fields["is_accepted_answer"]).to eq("true")
expect(p1.solution.present?).to eq(true)
end
it "removes the solution when the post is deleted" do
@@ -348,13 +344,13 @@ RSpec.describe "Managing Posts solved status" do
expect(response.status).to eq(200)
reply.reload
expect(reply.custom_fields["is_accepted_answer"]).to eq("true")
expect(reply.solution.present?).to eq(true)
expect(reply.topic.custom_fields["accepted_answer_post_id"].to_i).to eq(reply.id)
PostDestroyer.new(Discourse.system_user, reply).destroy
reply.reload
expect(reply.custom_fields["is_accepted_answer"]).to eq(nil)
expect(reply.solution).to eq(nil)
expect(reply.topic.custom_fields["accepted_answer_post_id"]).to eq(nil)
end
@@ -395,7 +391,7 @@ RSpec.describe "Managing Posts solved status" do
expect(response.status).to eq(200)
p1.reload
expect(p1.custom_fields["is_accepted_answer"]).to eq(nil)
expect(p1.solution).to eq(nil)
expect(p1.topic.custom_fields["accepted_answer_post_id"]).to eq(nil)
end
end
@@ -457,7 +453,7 @@ RSpec.describe "Managing Posts solved status" do
expect(p1.topic.assignment.status).to eq("New")
DiscourseSolved.accept_answer!(p1, user)
expect(p1.reload.custom_fields["is_accepted_answer"]).to eq("true")
expect(p1.reload.solution.present?).to eq(true)
expect(p1.topic.assignment.reload.status).to eq("Done")
end
@@ -472,7 +468,7 @@ RSpec.describe "Managing Posts solved status" do
DiscourseSolved.unaccept_answer!(p1)
expect(p1.reload.custom_fields["is_accepted_answer"]).to eq(nil)
expect(p1.reload.solution).to eq(nil)
expect(p1.reload.topic.assignment.reload.status).to eq("New")
end
+5 -8
View File
@@ -45,11 +45,11 @@ RSpec.describe TopicsController do
expect(response.body).to include(schema_json(0))
p2.custom_fields["is_accepted_answer"] = true
p2.save_custom_fields
topic.custom_fields["accepted_answer_post_id"] = p2.id
topic.save_custom_fields
Fabricate(:solution, topic_id: topic.id, answer_post_id: p2.id)
get "/t/#{topic.slug}/#{topic.id}"
expect(response.body).to include(schema_json(1))
@@ -68,10 +68,9 @@ RSpec.describe TopicsController do
it "should include user name in output with the corresponding site setting" do
SiteSetting.display_name_on_posts = true
p2.custom_fields["is_accepted_answer"] = true
p2.save_custom_fields
topic.custom_fields["accepted_answer_post_id"] = p2.id
topic.save_custom_fields
Fabricate(:solution, topic_id: topic.id, answer_post_id: p2.id)
get "/t/#{topic.slug}/#{topic.id}.json"
@@ -87,10 +86,9 @@ RSpec.describe TopicsController do
it "should not include user name when site setting is disabled" do
SiteSetting.display_name_on_posts = false
p2.custom_fields["is_accepted_answer"] = true
p2.save_custom_fields
topic.custom_fields["accepted_answer_post_id"] = p2.id
topic.save_custom_fields
Fabricate(:solution, topic_id: topic.id, answer_post_id: p2.id)
get "/t/#{topic.slug}/#{topic.id}.json"
@@ -106,10 +104,9 @@ RSpec.describe TopicsController do
it "includes the correct schema information" do
DiscourseTagging.add_or_create_tags_by_name(topic, [tag.name])
p2.custom_fields["is_accepted_answer"] = true
p2.save_custom_fields
topic.custom_fields["accepted_answer_post_id"] = p2.id
topic.save_custom_fields
Fabricate(:solution, topic_id: topic.id, answer_post_id: p2.id)
get "/t/#{topic.slug}/#{topic.id}"