From 4a8f21d38743a0a6b2a161889e335581248583b9 Mon Sep 17 00:00:00 2001 From: Maja Komel Date: Thu, 29 Nov 2018 18:10:14 +0100 Subject: [PATCH] FIX: prevent minimum_required_tags on category being set to null (#6703) * FIX: prevent minimum_required_tags on category being set to null * add migration for NOT_NULL constraint for minimum_required_tags * add specs --- .../templates/components/edit-category-settings.hbs | 2 +- app/controllers/categories_controller.rb | 5 ++--- ...94518_add_not_null_minimum_required_tags_on_categories.rb | 5 +++++ spec/requests/categories_controller_spec.rb | 3 +++ 4 files changed, 11 insertions(+), 4 deletions(-) create mode 100644 db/migrate/20181129094518_add_not_null_minimum_required_tags_on_categories.rb diff --git a/app/assets/javascripts/discourse/templates/components/edit-category-settings.hbs b/app/assets/javascripts/discourse/templates/components/edit-category-settings.hbs index 4390e33e50c..a45f57ee828 100644 --- a/app/assets/javascripts/discourse/templates/components/edit-category-settings.hbs +++ b/app/assets/javascripts/discourse/templates/components/edit-category-settings.hbs @@ -169,7 +169,7 @@ - {{text-field value=category.minimum_required_tags id="category-minimum-tags" type="number"}} + {{text-field value=category.minimum_required_tags id="category-minimum-tags" type="number" min="0"}} {{/if}} diff --git a/app/controllers/categories_controller.rb b/app/controllers/categories_controller.rb index 9fa1c0d12e0..e2ab78d27a6 100644 --- a/app/controllers/categories_controller.rb +++ b/app/controllers/categories_controller.rb @@ -149,9 +149,8 @@ class CategoriesController < ApplicationController category_params.delete(:position) # properly null the value so the database constraint doesn't catch us - if category_params.has_key?(:email_in) && category_params[:email_in].blank? - category_params[:email_in] = nil - end + category_params[:email_in] = nil if category_params[:email_in]&.blank? + category_params[:minimum_required_tags] = 0 if category_params[:minimum_required_tags]&.blank? old_permissions = cat.permissions_params diff --git a/db/migrate/20181129094518_add_not_null_minimum_required_tags_on_categories.rb b/db/migrate/20181129094518_add_not_null_minimum_required_tags_on_categories.rb new file mode 100644 index 00000000000..eebefd96119 --- /dev/null +++ b/db/migrate/20181129094518_add_not_null_minimum_required_tags_on_categories.rb @@ -0,0 +1,5 @@ +class AddNotNullMinimumRequiredTagsOnCategories < ActiveRecord::Migration[5.2] + def change + change_column_null :categories, :minimum_required_tags, false, 0 + end +end diff --git a/spec/requests/categories_controller_spec.rb b/spec/requests/categories_controller_spec.rb index d6f80b31c81..871c3d02b6a 100644 --- a/spec/requests/categories_controller_spec.rb +++ b/spec/requests/categories_controller_spec.rb @@ -91,6 +91,7 @@ describe CategoriesController do it "raises an exception when the text color is missing" do post "/categories.json", params: { name: "hello", color: "ff0" } + expect(response.status).to eq(400) end describe "failure" do @@ -299,6 +300,7 @@ describe CategoriesController do custom_fields: { "dancing" => "frogs" }, + minimum_required_tags: "" } expect(response.status).to eq(200) @@ -311,6 +313,7 @@ describe CategoriesController do expect(category.color).to eq("ff0") expect(category.auto_close_hours).to eq(72) expect(category.custom_fields).to eq("dancing" => "frogs") + expect(category.minimum_required_tags).to eq(0) end it 'logs the changes correctly' do