diff --git a/Gemfile.lock b/Gemfile.lock index 53ede95..4c42077 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,11 +1,30 @@ GEM remote: https://rubygems.org/ specs: + activesupport (7.1.3.2) + base64 + bigdecimal + concurrent-ruby (~> 1.0, >= 1.0.2) + connection_pool (>= 2.2.5) + drb + i18n (>= 1.6, < 2) + minitest (>= 5.1) + mutex_m + tzinfo (~> 2.0) ast (2.4.2) + base64 (0.2.0) + bigdecimal (3.1.6) + concurrent-ruby (1.2.3) + connection_pool (2.4.1) + drb (2.2.1) + i18n (1.14.1) + concurrent-ruby (~> 1.0) json (2.7.1) language_server-protocol (3.17.0.3) + minitest (5.22.2) + mutex_m (0.2.0) parallel (1.24.0) - parser (3.3.0.4) + parser (3.3.0.5) ast (~> 2.4.1) racc prettier_print (1.2.1) @@ -13,7 +32,7 @@ GEM rainbow (3.1.1) regexp_parser (2.9.0) rexml (3.2.6) - rubocop (1.60.0) + rubocop (1.61.0) json (~> 2.3) language_server-protocol (>= 3.17.0) parallel (~> 1.10) @@ -24,22 +43,27 @@ GEM rubocop-ast (>= 1.30.0, < 2.0) ruby-progressbar (~> 1.7) unicode-display_width (>= 2.4.0, < 3.0) - rubocop-ast (1.30.0) - parser (>= 3.2.1.0) + rubocop-ast (1.31.1) + parser (>= 3.3.0.4) rubocop-capybara (2.20.0) rubocop (~> 1.41) - rubocop-discourse (3.6.0) + rubocop-discourse (3.7.1) + activesupport (>= 6.1) rubocop (>= 1.59.0) + rubocop-capybara (>= 2.0.0) + rubocop-factory_bot (>= 2.0.0) rubocop-rspec (>= 2.25.0) rubocop-factory_bot (2.25.1) rubocop (~> 1.41) - rubocop-rspec (2.26.1) + rubocop-rspec (2.27.1) rubocop (~> 1.40) rubocop-capybara (~> 2.17) rubocop-factory_bot (~> 2.22) ruby-progressbar (1.13.0) syntax_tree (6.2.0) prettier_print (>= 1.2.0) + tzinfo (2.0.6) + concurrent-ruby (~> 1.0) unicode-display_width (2.5.0) PLATFORMS diff --git a/lib/discourse_topic_voting/category_extension.rb b/lib/discourse_topic_voting/category_extension.rb index d43e3a8..c2a8d30 100644 --- a/lib/discourse_topic_voting/category_extension.rb +++ b/lib/discourse_topic_voting/category_extension.rb @@ -2,29 +2,29 @@ module DiscourseTopicVoting module CategoryExtension - def self.prepended(base) - base.class_eval do - has_one :discourse_topic_voting_category_setting, - class_name: "DiscourseTopicVoting::CategorySetting", - dependent: :destroy + extend ActiveSupport::Concern - accepts_nested_attributes_for :discourse_topic_voting_category_setting, allow_destroy: true + prepended do + has_one :discourse_topic_voting_category_setting, + class_name: "DiscourseTopicVoting::CategorySetting", + dependent: :destroy - after_save :reset_voting_cache + accepts_nested_attributes_for :discourse_topic_voting_category_setting, allow_destroy: true - @allowed_voting_cache = DistributedCache.new("allowed_voting") + after_save :reset_voting_cache, if: -> { SiteSetting.voting_enabled? } - def self.reset_voting_cache - @allowed_voting_cache["allowed"] = DiscourseTopicVoting::CategorySetting.pluck( - :category_id, - ) - end + @allowed_voting_cache = DistributedCache.new("allowed_voting") + end + + class_methods do + def reset_voting_cache + @allowed_voting_cache["allowed"] = DiscourseTopicVoting::CategorySetting.pluck(:category_id) + end - def self.can_vote?(category_id) - return false if !SiteSetting.voting_enabled + def can_vote?(category_id) + return false if !SiteSetting.voting_enabled - (@allowed_voting_cache["allowed"] || reset_voting_cache).include?(category_id) - end + (@allowed_voting_cache["allowed"] || reset_voting_cache).include?(category_id) end end diff --git a/lib/discourse_topic_voting/list_controller_extension.rb b/lib/discourse_topic_voting/list_controller_extension.rb index cd61fda..dddf6e9 100644 --- a/lib/discourse_topic_voting/list_controller_extension.rb +++ b/lib/discourse_topic_voting/list_controller_extension.rb @@ -2,11 +2,11 @@ module DiscourseTopicVoting module ListControllerExtension - def self.prepended(base) - base.class_eval do - before_action :ensure_discourse_topic_voting, only: %i[voted_by votes_feed] - skip_before_action :ensure_logged_in, only: %i[voted_by votes_feed] - end + extend ActiveSupport::Concern + + prepended do + before_action :ensure_discourse_topic_voting, only: %i[voted_by votes_feed] + skip_before_action :ensure_logged_in, only: %i[voted_by votes_feed] end def voted_by diff --git a/lib/discourse_topic_voting/topic_extension.rb b/lib/discourse_topic_voting/topic_extension.rb index 078e9fc..5f15090 100644 --- a/lib/discourse_topic_voting/topic_extension.rb +++ b/lib/discourse_topic_voting/topic_extension.rb @@ -2,14 +2,15 @@ module DiscourseTopicVoting module TopicExtension - def self.prepended(base) - base.class_eval do - has_one :topic_vote_count, - class_name: "DiscourseTopicVoting::TopicVoteCount", - dependent: :destroy - has_many :votes, class_name: "DiscourseTopicVoting::Vote", dependent: :destroy - attribute :current_user_voted - end + extend ActiveSupport::Concern + + prepended do + has_one :topic_vote_count, + class_name: "DiscourseTopicVoting::TopicVoteCount", + dependent: :destroy + has_many :votes, class_name: "DiscourseTopicVoting::Vote", dependent: :destroy + + attribute :current_user_voted end def can_vote? @@ -46,7 +47,7 @@ def update_vote_count end def who_voted - return nil if !SiteSetting.voting_show_who_voted + return if !SiteSetting.voting_show_who_voted self.votes.map(&:user) end diff --git a/lib/discourse_topic_voting/user_extension.rb b/lib/discourse_topic_voting/user_extension.rb index ff424f9..c95c679 100644 --- a/lib/discourse_topic_voting/user_extension.rb +++ b/lib/discourse_topic_voting/user_extension.rb @@ -2,11 +2,9 @@ module DiscourseTopicVoting module UserExtension - def self.prepended(base) - base.class_eval do - has_many :votes, class_name: "DiscourseTopicVoting::Vote", dependent: :destroy - end - end + extend ActiveSupport::Concern + + prepended { has_many :votes, class_name: "DiscourseTopicVoting::Vote", dependent: :destroy } def vote_count topics_with_vote.length diff --git a/lib/discourse_topic_voting/web_hook_extension.rb b/lib/discourse_topic_voting/web_hook_extension.rb index a622002..275e522 100644 --- a/lib/discourse_topic_voting/web_hook_extension.rb +++ b/lib/discourse_topic_voting/web_hook_extension.rb @@ -2,18 +2,18 @@ module DiscourseTopicVoting module WebHookExtension - def self.prepended(base) - base.class_eval do - def self.enqueue_topic_voting_hooks(event, topic, payload) - if active_web_hooks(event).exists? - WebHook.enqueue_hooks( - :topic_voting, - event, - category_id: topic.category_id, - tag_ids: topic.tags.pluck(:id), - payload: payload, - ) - end + extend ActiveSupport::Concern + + class_methods do + def enqueue_topic_voting_hooks(event, topic, payload) + if active_web_hooks(event).exists? + WebHook.enqueue_hooks( + :topic_voting, + event, + category_id: topic.category_id, + tag_ids: topic.tags.pluck(:id), + payload: payload, + ) end end end diff --git a/plugin.rb b/plugin.rb index c0df817..44be931 100755 --- a/plugin.rb +++ b/plugin.rb @@ -45,13 +45,13 @@ class Engine < ::Rails::Engine ].each { |path| require_relative path } reloadable_patch do - CategoriesController.class_eval { prepend DiscourseTopicVoting::CategoriesControllerExtension } - Category.class_eval { prepend DiscourseTopicVoting::CategoryExtension } - ListController.class_eval { prepend DiscourseTopicVoting::ListControllerExtension } - Topic.class_eval { prepend DiscourseTopicVoting::TopicExtension } - TopicQuery.class_eval { prepend DiscourseTopicVoting::TopicQueryExtension } - User.class_eval { prepend DiscourseTopicVoting::UserExtension } - WebHook.class_eval { prepend DiscourseTopicVoting::WebHookExtension } + CategoriesController.prepend(DiscourseTopicVoting::CategoriesControllerExtension) + Category.prepend(DiscourseTopicVoting::CategoryExtension) + ListController.prepend(DiscourseTopicVoting::ListControllerExtension) + Topic.prepend(DiscourseTopicVoting::TopicExtension) + TopicQuery.prepend(DiscourseTopicVoting::TopicQueryExtension) + User.prepend(DiscourseTopicVoting::UserExtension) + WebHook.prepend(DiscourseTopicVoting::WebHookExtension) end add_to_serializer(:post, :can_vote, include_condition: -> { object.post_number == 1 }) do diff --git a/spec/requests/categories_controller_spec.rb b/spec/requests/categories_controller_spec.rb index d8ad13f..b188912 100644 --- a/spec/requests/categories_controller_spec.rb +++ b/spec/requests/categories_controller_spec.rb @@ -3,7 +3,7 @@ require "rails_helper" describe CategoriesController do - fab!(:category) { Fabricate(:category) } + fab!(:category) fab!(:topic) { Fabricate(:topic, category: category) } fab!(:admin) { Fabricate(:user, admin: true) } diff --git a/spec/requests/lists_controller_spec.rb b/spec/requests/lists_controller_spec.rb index a4ae5fc..2de549d 100644 --- a/spec/requests/lists_controller_spec.rb +++ b/spec/requests/lists_controller_spec.rb @@ -3,8 +3,8 @@ require "rails_helper" describe ListController do - fab!(:user) { Fabricate(:user) } - fab!(:topic) { Fabricate(:topic) } + fab!(:user) + fab!(:topic) # "topics/voted-by/:username" before { SiteSetting.voting_enabled = true } diff --git a/spec/requests/search_controller_spec.rb b/spec/requests/search_controller_spec.rb index 727d2f0..252e765 100644 --- a/spec/requests/search_controller_spec.rb +++ b/spec/requests/search_controller_spec.rb @@ -3,9 +3,9 @@ require "rails_helper" describe SearchController do - fab!(:user) { Fabricate(:user) } + fab!(:user) - fab!(:category) { Fabricate(:category) } + fab!(:category) fab!(:topic) { Fabricate(:topic, category: category) } fab!(:topic_2) { Fabricate(:topic, category: category) } diff --git a/spec/serializers/basic_category_serializer_spec.rb b/spec/serializers/basic_category_serializer_spec.rb index 9a2140d..0ae41ca 100644 --- a/spec/serializers/basic_category_serializer_spec.rb +++ b/spec/serializers/basic_category_serializer_spec.rb @@ -3,7 +3,7 @@ require "rails_helper" describe BasicCategorySerializer do - fab!(:category) { Fabricate(:category) } + fab!(:category) it "does not return can_vote when voting disabled" do SiteSetting.voting_enabled = false diff --git a/spec/serializers/category_serializer_spec.rb b/spec/serializers/category_serializer_spec.rb index fe69f2c..3dcbeff 100644 --- a/spec/serializers/category_serializer_spec.rb +++ b/spec/serializers/category_serializer_spec.rb @@ -3,7 +3,7 @@ require "rails_helper" describe CategorySerializer do - fab!(:category) { Fabricate(:category) } + fab!(:category) it "does not return enable_topic_voting voting disabled" do SiteSetting.voting_enabled = false diff --git a/spec/serializers/current_user_serializer_spec.rb b/spec/serializers/current_user_serializer_spec.rb index bae1945..02f0cd0 100644 --- a/spec/serializers/current_user_serializer_spec.rb +++ b/spec/serializers/current_user_serializer_spec.rb @@ -4,7 +4,7 @@ fab!(:user1) { Fabricate(:user, trust_level: 3) } let(:user2) { Fabricate(:user) } fab!(:guardian) { Guardian.new(user1) } - fab!(:category) { Fabricate(:category) } + fab!(:category) fab!(:topic1) { Fabricate(:topic, category_id: category.id) } let(:topic2) { Fabricate(:topic, category_id: category.id) } let(:topic3) { Fabricate(:topic, category_id: category.id) } diff --git a/spec/system/page_objects/pages/category.rb b/spec/system/page_objects/pages/category.rb index 4bf59c2..7872cec 100644 --- a/spec/system/page_objects/pages/category.rb +++ b/spec/system/page_objects/pages/category.rb @@ -16,4 +16,4 @@ def select_topic(topic) end end -PageObjects::Pages::Category.class_eval { include TopicVotingCategory } +PageObjects::Pages::Category.include(TopicVotingCategory) diff --git a/spec/system/page_objects/pages/topic.rb b/spec/system/page_objects/pages/topic.rb index 5b8f9fd..46b5b9f 100644 --- a/spec/system/page_objects/pages/topic.rb +++ b/spec/system/page_objects/pages/topic.rb @@ -27,4 +27,4 @@ def click_vote_popup_activity end end -PageObjects::Pages::Topic.class_eval { include TopicVotingTopic } +PageObjects::Pages::Topic.include(TopicVotingTopic) diff --git a/spec/system/voting_spec.rb b/spec/system/voting_spec.rb index 6849d3b..cbbe88c 100644 --- a/spec/system/voting_spec.rb +++ b/spec/system/voting_spec.rb @@ -1,8 +1,8 @@ # frozen_string_literal: true RSpec.describe "Voting", type: :system, js: true do - fab!(:user) { Fabricate(:user) } - fab!(:admin) { Fabricate(:admin) } + fab!(:user) + fab!(:admin) fab!(:category1) { Fabricate(:category) } fab!(:category2) { Fabricate(:category) } fab!(:topic1) { Fabricate(:topic, category: category1) }