DEV: Fix poll plugin causing error when server reloads in dev env.

DEV: Fix poll plugin causing error when server reloads in dev env.

diff --git a/plugins/poll/plugin.rb b/plugins/poll/plugin.rb
index 78fa0ba..ec4a757 100644
--- a/plugins/poll/plugin.rb
+++ b/plugins/poll/plugin.rb
@@ -451,28 +451,30 @@ after_initialize do
     mount ::DiscoursePoll::Engine, at: "/polls"
   end
 
-  Post.class_eval do
-    attr_accessor :extracted_polls
+  reloadable_patch do
+    Post.class_eval do
+      attr_accessor :extracted_polls
 
-    has_many :polls, dependent: :destroy
+      has_many :polls, dependent: :destroy
 
-    after_save do
-      polls = self.extracted_polls
-      next if polls.blank? || !polls.is_a?(Hash)
-      post = self
+      after_save do
+        polls = self.extracted_polls
+        next if polls.blank? || !polls.is_a?(Hash)
+        post = self
 
-      Poll.transaction do
-        polls.values.each do |poll|
-          DiscoursePoll::Poll.create!(post.id, poll)
+        Poll.transaction do
+          polls.values.each do |poll|
+            DiscoursePoll::Poll.create!(post.id, poll)
+          end
+          post.custom_fields[DiscoursePoll::HAS_POLLS] = true
+          post.save_custom_fields(true)
         end
-        post.custom_fields[DiscoursePoll::HAS_POLLS] = true
-        post.save_custom_fields(true)
       end
     end
-  end
 
-  User.class_eval do
-    has_many :poll_votes, dependent: :delete_all
+    User.class_eval do
+      has_many :poll_votes, dependent: :delete_all
+    end
   end
 
   validate(:post, :validate_polls) do |force = nil|

GitHub sha: 6359a5f8

1 Like