Skip to content
This repository was archived by the owner on Jul 22, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/discourse_voting/categories_controller_extension.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
module DiscourseVoting
module CategoriesControllerExtension
def category_params
@vote_enabled ||= params[:custom_fields] && params[:custom_fields].delete(:enable_topic_voting) == "true"
@vote_enabled ||= !!ActiveRecord::Type::Boolean.new.cast(params[:custom_fields]&.delete(:enable_topic_voting))
category_params = super
if @vote_enabled
category_params[:category_setting_attributes] = {}
Expand Down
28 changes: 28 additions & 0 deletions spec/requests/categories_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# frozen_string_literal: true

require 'rails_helper'

describe CategoriesController do
fab!(:category) { Fabricate(:category) }
fab!(:topic) { Fabricate(:topic, category: category) }
fab!(:admin) { Fabricate(:user, admin: true) }

before do
SiteSetting.voting_enabled = true
Copy link
Contributor

@nattsw nattsw Apr 11, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Btw is this needed? (I don't see it used above)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed yes. Category.can_vote? will be false if the plugin is disabled.

sign_in(admin)
end

it "enables voting correctly" do
put "/categories/#{category.id}.json", params: {
custom_fields: { "enable_topic_voting" => true }
}
expect(Category.can_vote?(category.id)).to eq(true)
end
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice nice, can you add a failing scenario please too?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, I think the failure should be with a simple false value. Reason being, we don't need to exercise ActiveRecord::Type::Boolean here right?

Copy link
Contributor

@nattsw nattsw Apr 11, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup! Actually there's other cases where other custom_fields may exist after deleting enable_topic_voting. But that's alright. I trust you clicked through the scenarios.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, the following piece of code(with category_setting_attributes) should also be tested I agree. But I didn't test that coz it doesn't have to do anything with my fix. 😅


it "disables voting correctly" do
put "/categories/#{category.id}.json", params: {
custom_fields: { "enable_topic_voting" => false }
}
expect(Category.can_vote?(category.id)).to eq(false)
end
end