FEATURE: Update the topic excerpt when the OP is rebaked (#9852)

* We now have a site setting "topic_excerpt_maxlength" that is used when the OP is created or revised to generate a topic excerpt.
* However, posts created before this setting was introduced cannot benefit from this change unless they are revised, and if the topic excerpt length setting is changed that situation is also not covererd.
* This PR makes a change to rebake! to update the topic excerpt IF the post is the OP.
This commit is contained in:
Martin Brennan 2020-05-22 13:04:15 +10:00 committed by GitHub
parent c0779df99d
commit f9d55b4941
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 31 additions and 5 deletions

View File

@ -650,6 +650,10 @@ class Post < ActiveRecord::Base
baked_version: BAKED_VERSION baked_version: BAKED_VERSION
) )
if is_first_post?
topic.update_excerpt(excerpt_for_topic)
end
if invalidate_broken_images if invalidate_broken_images
custom_fields.delete(BROKEN_IMAGES) custom_fields.delete(BROKEN_IMAGES)
save_custom_fields save_custom_fields

View File

@ -1476,6 +1476,13 @@ class Topic < ActiveRecord::Base
private_topic private_topic
end end
def update_excerpt(excerpt)
update_column(:excerpt, excerpt)
if archetype == "banner"
ApplicationController.banner_json_cache.clear
end
end
def pm_with_non_human_user? def pm_with_non_human_user?
sql = <<~SQL sql = <<~SQL
SELECT 1 FROM topics SELECT 1 FROM topics

View File

@ -567,11 +567,7 @@ class PostRevisor
end end
def update_topic_excerpt def update_topic_excerpt
excerpt = @post.excerpt_for_topic @topic.update_excerpt(@post.excerpt_for_topic)
@topic.update_column(:excerpt, excerpt)
if @topic.archetype == "banner"
ApplicationController.banner_json_cache.clear
end
end end
def update_category_description def update_category_description

View File

@ -1195,6 +1195,25 @@ describe Post do
expect(post.cooked).to eq(first_cooked) expect(post.cooked).to eq(first_cooked)
expect(result).to eq(true) expect(result).to eq(true)
end end
it "updates the topic excerpt at the same time if it is the OP" do
post = create_post
post.topic.update(excerpt: "test")
DB.exec("UPDATE posts SET cooked = 'frogs' WHERE id = ?", [ post.id ])
post.reload
result = post.rebake!
post.topic.reload
expect(post.topic.excerpt).not_to eq("test")
end
it "does not update the topic excerpt if the post is not the OP" do
post = create_post
post2 = create_post
post.topic.update(excerpt: "test")
result = post2.rebake!
post.topic.reload
expect(post.topic.excerpt).to eq("test")
end
end end
describe "#set_owner" do describe "#set_owner" do