From 099cf71bccb8e3da00324914a34e08837e65a7a3 Mon Sep 17 00:00:00 2001 From: Jean Date: Wed, 26 Jun 2024 12:34:55 -0400 Subject: [PATCH] FIX: Topic embedding importer should accept string tags (#27624) * FIX: Embedding importer should accept string tags --- app/models/topic_embed.rb | 2 +- spec/models/topic_embed_spec.rb | 24 ++++++++++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/app/models/topic_embed.rb b/app/models/topic_embed.rb index 53466cbcb33..41544bdeb2e 100644 --- a/app/models/topic_embed.rb +++ b/app/models/topic_embed.rb @@ -122,7 +122,7 @@ class TopicEmbed < ActiveRecord::Base end existing_tag_names = post.topic.tags.pluck(:name).sort - incoming_tag_names = Array(tags).map(&:name).sort + incoming_tag_names = Array(tags).map { |tag| tag.respond_to?(:name) ? tag.name : tag }.sort tags_changed = existing_tag_names != incoming_tag_names diff --git a/spec/models/topic_embed_spec.rb b/spec/models/topic_embed_spec.rb index b9888fcdbce..9fcb21efab4 100644 --- a/spec/models/topic_embed_spec.rb +++ b/spec/models/topic_embed_spec.rb @@ -349,6 +349,30 @@ RSpec.describe TopicEmbed do end end + context "when importing a topic embed with string tags" do + fab!(:tag1) { Fabricate(:tag, name: "interesting") } + fab!(:tag2) { Fabricate(:tag, name: "article") } + let(:tags) { [tag1.name, tag2.name] } + + it "associates the specified tags with the existing topic" do + imported_page = TopicEmbed.import(user, url, title, contents, tags: tags) + expect(imported_page.topic.tags).to match_array([tag1, tag2]) + end + end + + context "when updating an existing topic embed with string tags" do + fab!(:tag1) { Fabricate(:tag, name: "interesting") } + fab!(:tag2) { Fabricate(:tag, name: "article") } + let(:tags) { [tag1, tag2] } + + before { TopicEmbed.import(user, url, title, contents, tags: [tag1.name]) } + + it "associates the specified tags with the existing topic" do + imported_page = TopicEmbed.import(user, url, title, contents, tags: tags) + expect(imported_page.topic.tags).to match_array([tag1, tag2]) + end + end + context "with specified user and tags" do fab!(:tag1) { Fabricate(:tag, name: "interesting") } fab!(:tag2) { Fabricate(:tag, name: "article") }