A platform for community discussion. Free, open, simple.
DEV: Allow TopicEmbed.import to optionally receive a list of tags (#14301)
Rafael dos Santos Silva2e0992c7575b
diff --git a/app/models/topic_embed.rb b/app/models/topic_embed.rbindex 2b2523aa126..61554f6f5cf 100644--- a/app/models/topic_embed.rb+++ b/app/models/topic_embed.rb@@ -27,7 +27,7 @@ class TopicEmbed < ActiveRecord::Baseend# Import an article from a source (RSS/Atom/Other)- def self.import(user, url, title, contents, category_id: nil, cook_method: nil)+ def self.import(user, url, title, contents, category_id: nil, cook_method: nil, tags: nil)return unless url =~ /^https?\:\/\//if SiteSetting.embed_truncate && cook_method.nil?@@ -58,7 +58,8 @@ class TopicEmbed < ActiveRecord::Baseraw: absolutize_urls(url, contents),skip_validations: true,cook_method: cook_method,- category: category_id || eh.try(:category_id)+ category: category_id || eh.try(:category_id),+ tags: SiteSetting.tagging_enabled ? tags : nil,}if SiteSetting.embed_unlisted?create_args[:visible] = falsediff --git a/spec/models/topic_embed_spec.rb b/spec/models/topic_embed_spec.rbindex 6a4e0025dbb..694ad884248 100644--- a/spec/models/topic_embed_spec.rb+++ b/spec/models/topic_embed_spec.rb@@ -17,6 +17,7 @@ describe TopicEmbed dolet(:contents) { "<p>hello world new post <a href='/hello'>hello</a> <img src='/images/wat.jpg'></p>" }fab!(:embeddable_host) { Fabricate(:embeddable_host) }fab!(:category) { Fabricate(:category) }+ fab!(:tag) { Fabricate(:tag) }it "returns nil when the URL is malformed" doexpect(TopicEmbed.import(user, "invalid url", title, contents)).to eq(nil)@@ -104,6 +105,13 @@ describe TopicEmbed doexpect(imported_post.topic.category).to eq(category)end+ it "creates the topic with the tag passed as a parameter" do+ Jobs.run_immediately!+ SiteSetting.tagging_enabled = true+ imported_post = TopicEmbed.import(user, "http://eviltrout.com/abcd", title, "some random content", tags: [tag.name])+ expect(imported_post.topic.tags).to include(tag)+ end+it "respects overriding the cook_method when asked" doJobs.run_immediately!SiteSetting.embed_support_markdown = false