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:
parent
3d9f4c2996
commit
1da6677212
|
@ -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] = {}
|
||||||
|
|
|
@ -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
|
Loading…
Reference in New Issue