Skip to content

A platform for community discussion. Free, open, simple.

FIX: Cater for reviewable tag payloads that have been saved as names (#37477)

Natalie Tayde338fcb1e36
diff --git a/app/models/reviewable_queued_post.rb b/app/models/reviewable_queued_post.rb
index 8768157851c..7255cbb0c20 100644
--- a/app/models/reviewable_queued_post.rb
+++ b/app/models/reviewable_queued_post.rb
@@ -107,6 +107,9 @@ class ReviewableQueuedPost < Reviewable
result[:cooking_options].symbolize_keys! if result[:cooking_options]
result[:topic_id] = topic_id if topic_id
result[:category] = category_id if category_id
+ if result[:tags].is_a?(Array)
+ result[:tags] = result[:tags].map { |t| t.is_a?(Hash) ? t["name"] : t }
+ end
result
end
diff --git a/spec/models/reviewable_queued_post_spec.rb b/spec/models/reviewable_queued_post_spec.rb
index cf3175c40c1..28485d5e53c 100644
--- a/spec/models/reviewable_queued_post_spec.rb
+++ b/spec/models/reviewable_queued_post_spec.rb
@@ -278,6 +278,17 @@ RSpec.describe ReviewableQueuedPost, type: :model do
expect(create_options[:archetype]).to eq("regular")
end
+ it "normalizes tag objects to strings in create_options" do
+ reviewable.payload["tags"] = [
+ { "id" => 1, "name" => "tag-one", "count" => 5 },
+ { "id" => 2, "name" => "tag-two", "count" => 10 },
+ ]
+
+ create_options = reviewable.create_options
+
+ expect(create_options[:tags]).to eq(%w[tag-one tag-two])
+ end
+
it "creates the post and topic when approved" do
topic_count, post_count = Topic.count, Post.count
result = reviewable.perform(moderator, :approve_post)
@@ -308,6 +319,21 @@ RSpec.describe ReviewableQueuedPost, type: :model do
expect(result.created_post_topic.tags.pluck(:name)).to match_array(reviewable.payload["tags"])
end
+ it "approves successfully when tags are stored as objects instead of strings" do
+ tag1 = Fabricate(:tag, name: "tag-one")
+ tag2 = Fabricate(:tag, name: "tag-two")
+ reviewable.payload["tags"] = [
+ { "id" => tag1.id, "name" => "tag-one", "count" => 5 },
+ { "id" => tag2.id, "name" => "tag-two", "count" => 10 },
+ ]
+ reviewable.save!
+
+ result = reviewable.perform(moderator, :approve_post)
+
+ expect(result.success?).to eq(true)
+ expect(result.created_post_topic.tags.pluck(:name)).to contain_exactly("tag-one", "tag-two")
+ end
+
it "does not create the post and topic when rejected" do
topic_count, post_count = Topic.count, Post.count
result = reviewable.perform(moderator, :reject_post)