Skip to content

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

FIX: Frozen string error in `TopicEmbed.import` (#7938)

Kyle Zhao0e1d6151b961
diff --git a/app/models/topic_embed.rb b/app/models/topic_embed.rb
index f7b46b711f1..c1948d89af8 100644
--- a/app/models/topic_embed.rb
+++ b/app/models/topic_embed.rb
@@ -36,8 +36,8 @@ class TopicEmbed < ActiveRecord::Base
if SiteSetting.embed_truncate
contents = first_paragraph_from(contents)
end
- contents ||= +''
- contents << imported_from_html(url)
+ contents ||= ''
+ contents = +contents << imported_from_html(url)
url = normalize_url(url)
diff --git a/spec/models/topic_embed_spec.rb b/spec/models/topic_embed_spec.rb
index 71c60afb3ab..18d4dd97648 100644
--- a/spec/models/topic_embed_spec.rb
+++ b/spec/models/topic_embed_spec.rb
@@ -86,6 +86,26 @@ describe TopicEmbed do
expect(post.cook_method).to eq(Post.cook_methods[:regular])
end
end
+
+ describe 'embedded content truncation' do
+ MAX_LENGTH_BEFORE_TRUNCATION = 100
+
+ let(:long_content) { "<p>#{'a' * MAX_LENGTH_BEFORE_TRUNCATION}</p>\n<p>more</p>" }
+
+ it 'truncates the imported post when truncation is enabled' do
+ SiteSetting.embed_truncate = true
+ post = TopicEmbed.import(user, url, title, long_content)
+
+ expect(post.raw).not_to include(long_content)
+ end
+
+ it 'keeps everything in the imported post when truncation is disabled' do
+ SiteSetting.embed_truncate = false
+ post = TopicEmbed.import(user, url, title, long_content)
+
+ expect(post.raw).to include(long_content)
+ end
+ end
end
context '.topic_id_for_embed' do