diff --git a/plugins/poll/plugin.rb b/plugins/poll/plugin.rb index 78fa0ba548a..ec4a7570a6d 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|