FIX: prevents error if the associated post has been destroyed

FIX: prevents error if the associated post has been destroyed

diff --git a/plugin.rb b/plugin.rb
index 0e61dda..2b4232c 100644
--- a/plugin.rb
+++ b/plugin.rb
@@ -169,7 +169,7 @@ after_initialize do
   end
 
   add_to_serializer(:post, :include_event?) do
-    SiteSetting.discourse_post_event_enabled
+    SiteSetting.discourse_post_event_enabled && !object.nil? && !object.deleted_at.present?
   end
 
   on(:post_process_cooked) do |doc, post|
diff --git a/spec/serializers/discourse_post_event/post_serializer_spec.rb b/spec/serializers/discourse_post_event/post_serializer_spec.rb
new file mode 100644
index 0000000..e9fd519
--- /dev/null
+++ b/spec/serializers/discourse_post_event/post_serializer_spec.rb
@@ -0,0 +1,35 @@
+# frozen_string_literal: true
+
+require "rails_helper"
+require_relative '../../fabricators/event_fabricator'
+
+describe PostSerializer do
+  before do
+    Jobs.run_immediately!
+    SiteSetting.calendar_enabled = true
+    SiteSetting.discourse_post_event_enabled = true
+  end
+
+  context 'post has an event' do
+    let(:user) { Fabricate(:user, admin: true) }
+    let(:topic_1) { Fabricate(:topic, user: user) }
+    let(:post_1) { Fabricate(:post, topic: topic_1) }
+    let!(:post_event_1) { Fabricate(:event, post: post_1) }
+
+    it 'serializes the associated event' do
+      json = PostSerializer.new(post_1, scope: Guardian.new).as_json
+      expect(json[:post][:event]).to be_present
+    end
+
+    context 'the post has been destroyed' do
+      before do
+        PostDestroyer.new(Discourse.system_user, post_1).destroy
+      end
+
+      it 'doesn’t serialize the associated event' do
+        json = PostSerializer.new(post_1, scope: Guardian.new).as_json
+        expect(json[:post][:event]).to_not be_present
+      end
+    end
+  end
+end

GitHub sha: cced2c8c