FIX: handle boolean correctly on server (#116)

* FIX: handle boolean correctly on server

* added a test

* added a negative test case
This commit is contained in:
Faizaan Gagan 2022-04-11 18:18:50 +05:30 committed by GitHub
parent 3d9f4c2996
commit 1da6677212
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 29 additions and 1 deletions

View File

@ -3,7 +3,7 @@
module DiscourseVoting module DiscourseVoting
module CategoriesControllerExtension module CategoriesControllerExtension
def category_params 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 category_params = super
if @vote_enabled if @vote_enabled
category_params[:category_setting_attributes] = {} category_params[:category_setting_attributes] = {}

View File

@ -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
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
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