Skip to content

Commit

Permalink
DEV: Fix new Rubocop offenses
Browse files Browse the repository at this point in the history
  • Loading branch information
Flink committed Mar 5, 2024
1 parent e57fa35 commit fa6fe4a
Show file tree
Hide file tree
Showing 16 changed files with 96 additions and 73 deletions.
36 changes: 30 additions & 6 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,19 +1,38 @@
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)
racc (1.7.3)
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)
Expand All @@ -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
Expand Down
34 changes: 17 additions & 17 deletions lib/discourse_topic_voting/category_extension.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
10 changes: 5 additions & 5 deletions lib/discourse_topic_voting/list_controller_extension.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
19 changes: 10 additions & 9 deletions lib/discourse_topic_voting/topic_extension.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand Down Expand Up @@ -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
Expand Down
8 changes: 3 additions & 5 deletions lib/discourse_topic_voting/user_extension.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
24 changes: 12 additions & 12 deletions lib/discourse_topic_voting/web_hook_extension.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 7 additions & 7 deletions plugin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion spec/requests/categories_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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) }

Expand Down
4 changes: 2 additions & 2 deletions spec/requests/lists_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 }

Expand Down
4 changes: 2 additions & 2 deletions spec/requests/search_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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) }
Expand Down
2 changes: 1 addition & 1 deletion spec/serializers/basic_category_serializer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion spec/serializers/category_serializer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion spec/serializers/current_user_serializer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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) }
Expand Down
2 changes: 1 addition & 1 deletion spec/system/page_objects/pages/category.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@ def select_topic(topic)
end
end

PageObjects::Pages::Category.class_eval { include TopicVotingCategory }
PageObjects::Pages::Category.include(TopicVotingCategory)
2 changes: 1 addition & 1 deletion spec/system/page_objects/pages/topic.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,4 @@ def click_vote_popup_activity
end
end

PageObjects::Pages::Topic.class_eval { include TopicVotingTopic }
PageObjects::Pages::Topic.include(TopicVotingTopic)
4 changes: 2 additions & 2 deletions spec/system/voting_spec.rb
Original file line number Diff line number Diff line change
@@ -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) }
Expand Down

0 comments on commit fa6fe4a

Please sign in to comment.