FIX: Restore the ability to rebake posts in deleted topics (#9951)

FIX: Restore the ability to rebake posts in deleted topics (#9951)

Lost in #9852

diff --git a/app/models/post.rb b/app/models/post.rb
index 7e92f0e..37b5dea 100644
--- a/app/models/post.rb
+++ b/app/models/post.rb
@@ -651,7 +651,7 @@ class Post < ActiveRecord::Base
     )
 
     if is_first_post?
-      topic.update_excerpt(excerpt_for_topic)
+      topic&.update_excerpt(excerpt_for_topic)
     end
 
     if invalidate_broken_images
diff --git a/spec/models/post_spec.rb b/spec/models/post_spec.rb
index 5bc5206..fe4fe1a 100644
--- a/spec/models/post_spec.rb
+++ b/spec/models/post_spec.rb
@@ -1214,6 +1214,13 @@ describe Post do
       post.topic.reload
       expect(post.topic.excerpt).to eq("test")
     end
+
+    it "works with posts in deleted topics" do
+      post = create_post
+      post.topic.trash!
+      post.reload
+      post.rebake!
+    end
   end
 
   describe "#set_owner" do

GitHub sha: 00aab498

This commit appears in #9951 which was approved by martin. It was merged by CvX.