Skip to content

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

DEV: Allow passing cook_method to TopicEmbed.import to override default (#14209)

Rafael dos Santos Silvaef0471d7aec6
diff --git a/app/models/topic_embed.rb b/app/models/topic_embed.rb
index 380daafb5b1..2b2523aa126 100644
--- a/app/models/topic_embed.rb
+++ b/app/models/topic_embed.rb
@@ -27,10 +27,10 @@ class TopicEmbed < ActiveRecord::Base
end
# Import an article from a source (RSS/Atom/Other)
- def self.import(user, url, title, contents, category_id: nil)
+ def self.import(user, url, title, contents, category_id: nil, cook_method: nil)
return unless url =~ /^https?\:\/\//
- if SiteSetting.embed_truncate
+ if SiteSetting.embed_truncate && cook_method.nil?
contents = first_paragraph_from(contents)
end
contents ||= ''
@@ -47,7 +47,7 @@ class TopicEmbed < ActiveRecord::Base
Topic.transaction do
eh = EmbeddableHost.record_for_url(url)
- cook_method = if SiteSetting.embed_support_markdown
+ cook_method ||= if SiteSetting.embed_support_markdown
Post.cook_methods[:regular]
else
Post.cook_methods[:raw_html]
diff --git a/spec/models/topic_embed_spec.rb b/spec/models/topic_embed_spec.rb
index decb5028061..6a4e0025dbb 100644
--- a/spec/models/topic_embed_spec.rb
+++ b/spec/models/topic_embed_spec.rb
@@ -99,11 +99,22 @@ describe TopicEmbed do
it "creates the topic in the category passed as a parameter" do
Jobs.run_immediately!
- SiteSetting.embed_unlisted = false
imported_post = TopicEmbed.import(user, "http://eviltrout.com/abcd", title, "some random content", category_id: category.id)
expect(imported_post.topic.category).not_to eq(embeddable_host.category)
expect(imported_post.topic.category).to eq(category)
end
+
+ it "respects overriding the cook_method when asked" do
+ Jobs.run_immediately!
+ SiteSetting.embed_support_markdown = false
+ stub_request(:get, "https://www.youtube.com/watch?v=K56soYl0U1w")
+ .to_return(status: 200, body: "", headers: {})
+ stub_request(:get, "https://www.youtube.com/embed/K56soYl0U1w")
+ .to_return(status: 200, body: "", headers: {})
+
+ imported_post = TopicEmbed.import(user, "http://eviltrout.com/abcd", title, "https://www.youtube.com/watch?v=K56soYl0U1w", cook_method: Post.cook_methods[:regular])
+ expect(imported_post.cooked).to match(/onebox|iframe/)
+ end
end
context "post creation supports markdown rendering" do