FIX: include all reloadable parts of the plugin (#48)

FIX: include all reloadable parts of the plugin (#48)

Currently, if you got that plugin installed and change code in development in Discourse, you will see an error like: undefined method can_vote?’`

A reason for that was incorrectly defined reloadable_patch block. That block defines everything which should be reloaded by ActiveSupport::Reloader if code is changed.

because that was not included in that block

class ::Category
  def self.can_vote?(category_id)
  end
end

method can_vote? was not anymore available.

diff --git a/plugin.rb b/plugin.rb
index d0167d5..b8792a0 100755
--- a/plugin.rb
+++ b/plugin.rb
@@ -70,75 +70,74 @@ after_initialize do
         end
       end
     end
-  end
-
-  add_to_serializer(:topic_list_item, :vote_count) { object.vote_count }
-  add_to_serializer(:topic_list_item, :can_vote) { object.can_vote? }
-  add_to_serializer(:topic_list_item, :user_voted) {
-    object.user_voted(scope.user) if scope.user
-  }
 
-  add_to_serializer(:basic_category, :can_vote, false) do
-    SiteSetting.voting_enabled
-  end
-
-  add_to_serializer(:basic_category, :include_can_vote?) do
-    Category.can_vote?(object.id)
-  end
+    add_to_serializer(:topic_list_item, :vote_count) { object.vote_count }
+    add_to_serializer(:topic_list_item, :can_vote) { object.can_vote? }
+    add_to_serializer(:topic_list_item, :user_voted) {
+      object.user_voted(scope.user) if scope.user
+    }
 
-  class ::Category
-    def self.reset_voting_cache
-      @allowed_voting_cache["allowed"] =
-        begin
-          Set.new(
-            CategoryCustomField
-            .where(name: ::DiscourseVoting::VOTING_ENABLED, value: "true")
-            .pluck(:category_id)
-          )
-        end
+    add_to_serializer(:basic_category, :can_vote, false) do
+      SiteSetting.voting_enabled
     end
 
-    @allowed_voting_cache = DistributedCache.new("allowed_voting")
-
-    def self.can_vote?(category_id)
-      return false unless SiteSetting.voting_enabled
+    add_to_serializer(:basic_category, :include_can_vote?) do
+      Category.can_vote?(object.id)
+    end
 
-      unless set = @allowed_voting_cache["allowed"]
-        set = reset_voting_cache
+    class ::Category
+      def self.reset_voting_cache
+        @allowed_voting_cache["allowed"] =
+          begin
+            Set.new(
+              CategoryCustomField
+              .where(name: ::DiscourseVoting::VOTING_ENABLED, value: "true")
+              .pluck(:category_id)
+            )
+          end
       end
-      set.include?(category_id)
-    end
 
-    after_save :reset_voting_cache
-    before_save :reclaim_release_votes
+      @allowed_voting_cache = DistributedCache.new("allowed_voting")
 
-    protected
-    def reset_voting_cache
-      ::Category.reset_voting_cache
-    end
+      def self.can_vote?(category_id)
+        return false unless SiteSetting.voting_enabled
+
+        unless set = @allowed_voting_cache["allowed"]
+          set = reset_voting_cache
+        end
+        set.include?(category_id)
+      end
 
-    def reclaim_release_votes
-      return if self.new_record?
-      return if !SiteSetting.voting_enabled
+      after_save :reset_voting_cache
+      before_save :reclaim_release_votes
 
-      aliases = {
-        votes: DiscourseVoting::VOTES,
-        votes_archive: DiscourseVoting::VOTES_ARCHIVE,
-        category_id: id
-      }
+      protected
+      def reset_voting_cache
+        ::Category.reset_voting_cache
+      end
 
-      was_enabled = CategoryCustomField.where(
-        "name = :name AND value similar to :value AND category_id = :id",
-        id: id,
-        name: ::DiscourseVoting::VOTING_ENABLED,
-        value: '(t|T|1)%'
-      ).exists?
+      def reclaim_release_votes
+        return if self.new_record?
+        return if !SiteSetting.voting_enabled
+
+        aliases = {
+          votes: DiscourseVoting::VOTES,
+          votes_archive: DiscourseVoting::VOTES_ARCHIVE,
+          category_id: id
+        }
+
+        was_enabled = CategoryCustomField.where(
+          "name = :name AND value similar to :value AND category_id = :id",
+          id: id,
+          name: ::DiscourseVoting::VOTING_ENABLED,
+          value: '(t|T|1)%'
+        ).exists?
 
-      is_enabled = custom_fields[::DiscourseVoting::VOTING_ENABLED]
+        is_enabled = custom_fields[::DiscourseVoting::VOTING_ENABLED]
 
-      if !was_enabled && is_enabled
-        # Unarchive all votes in the category
-        DB.exec(<<~SQL, aliases)
+        if !was_enabled && is_enabled
+          # Unarchive all votes in the category
+          DB.exec(<<~SQL, aliases)
           UPDATE user_custom_fields ucf
           SET name = :votes
           FROM topics t
@@ -148,176 +147,177 @@ after_initialize do
           AND t.deleted_at IS NULL
           AND t.id::text = ucf.value
           AND t.category_id = :category_id
-        SQL
-      elsif was_enabled && !is_enabled
-        # Archive all votes in category
-        DB.exec(<<~SQL, aliases)
+          SQL
+        elsif was_enabled && !is_enabled
+          # Archive all votes in category
+          DB.exec(<<~SQL, aliases)
           UPDATE user_custom_fields ucf
           SET name = :votes_archive
           FROM topics t
           WHERE ucf.name = :votes
           AND t.id::text = ucf.value
           AND t.category_id = :category_id
-        SQL
+          SQL
+        end
       end
     end
-  end
 
-  require_dependency 'user'
-  class ::User
-    def vote_count
-      votes.length
-    end
+    require_dependency 'user'
+    class ::User
+      def vote_count
+        votes.length
+      end
 
-    def alert_low_votes?
-      (vote_limit - vote_count) <= SiteSetting.voting_alert_votes_left
-    end
+      def alert_low_votes?
+        (vote_limit - vote_count) <= SiteSetting.voting_alert_votes_left
+      end
 
-    def votes
-      votes = self.custom_fields[DiscourseVoting::VOTES] || []
-      # "" can be in there sometimes, it gets turned into a 0
-      votes = votes.reject { |v| v == 0 }.uniq
-      votes
-    end
+      def votes
+        votes = self.custom_fields[DiscourseVoting::VOTES] || []
+        # "" can be in there sometimes, it gets turned into a 0
+        votes = votes.reject { |v| v == 0 }.uniq
+        votes
+      end
 
-    def votes_archive
-      archived_votes = self.custom_fields[DiscourseVoting::VOTES_ARCHIVE] || []
-      archived_votes = archived_votes.reject { |v| v == 0 }.uniq
-      archived_votes
-    end
+      def votes_archive
+        archived_votes = self.custom_fields[DiscourseVoting::VOTES_ARCHIVE] || []
+        archived_votes = archived_votes.reject { |v| v == 0 }.uniq
+        archived_votes
+      end
 
-    def reached_voting_limit?
-      vote_count >= vote_limit
-    end
+      def reached_voting_limit?
+        vote_count >= vote_limit
+      end
+
+      def vote_limit
+        SiteSetting.public_send("voting_tl#{self.trust_level}_vote_limit")
+      end
 
-    def vote_limit
-      SiteSetting.public_send("voting_tl#{self.trust_level}_vote_limit")
     end
 
-  end
+    require_dependency 'current_user_serializer'
+    class ::CurrentUserSerializer
+      attributes :votes_exceeded,  :vote_count
 
-  require_dependency 'current_user_serializer'
-  class ::CurrentUserSerializer
-    attributes :votes_exceeded,  :vote_count
+      def votes_exceeded
+        object.reached_voting_limit?
+      end
 
-    def votes_exceeded
-      object.reached_voting_limit?
-    end
+      def vote_count
+        object.vote_count
+      end
 
-    def vote_count
-      object.vote_count
     end
 
-  end
-
-  require_dependency 'topic'
-  class ::Topic
+    require_dependency 'topic'
+    class ::Topic
 
-    def can_vote?
-      SiteSetting.voting_enabled && Category.can_vote?(category_id) && category.topic_id != id
-    end
+      def can_vote?
+        SiteSetting.voting_enabled && Category.can_vote?(category_id) && category.topic_id != id
+      end
 
-    def vote_count
-      if count = self.custom_fields[DiscourseVoting::VOTE_COUNT]
-        # we may have a weird array here, don't explode
-        # need to fix core to enforce types on fields
-        count.try(:to_i) || 0
-      else
-        0 if self.can_vote?
+      def vote_count
+        if count = self.custom_fields[DiscourseVoting::VOTE_COUNT]
+          # we may have a weird array here, don't explode
+          # need to fix core to enforce types on fields
+          count.try(:to_i) || 0
+        else
+          0 if self.can_vote?
+        end
       end
-    end
 

[... diff too long, it was truncated ...]

GitHub sha: c90fbe6f

Note… diff looks enormous but if you actually only look at non whitespace a single line moved a few rows down.

1 Like

Oh, I hope you didn’t spend too much time reading all that stuff, sorry :slight_smile:

2 Likes