From 1f7e5e8e758fe0ee3c4e7181006fca91f975476c Mon Sep 17 00:00:00 2001 From: Ted Johansson Date: Fri, 4 Aug 2023 10:53:22 +0800 Subject: [PATCH] DEV: Switch over category settings to new table - Part 2 (#20580) In #20135 we prevented invalid inputs from being accepted in category setting form fields on the front-end. We didn't do anything on the back-end at that time, because we were still discussing which path we wanted to take. Eventually we decided we want to move this to a new CategorySetting model. This PR moves the num_auto_bump_daily from custom fields to the new CategorySetting model. In addition it sets the default value to 0, which exhibits the same behaviour as when the value is NULL. --- .../app/components/edit-category-settings.hbs | 2 +- app/controllers/categories_controller.rb | 2 +- app/models/category.rb | 37 +++------- app/models/category_setting.rb | 2 +- ...ory_setting_num_auto_bump_daily_default.rb | 67 +++++++++++++++++++ spec/models/category_spec.rb | 2 +- spec/requests/categories_controller_spec.rb | 3 +- 7 files changed, 83 insertions(+), 32 deletions(-) create mode 100644 db/migrate/20230727015254_change_category_setting_num_auto_bump_daily_default.rb diff --git a/app/assets/javascripts/discourse/app/components/edit-category-settings.hbs b/app/assets/javascripts/discourse/app/components/edit-category-settings.hbs index 2a36e0531d3..cd027fcb60b 100644 --- a/app/assets/javascripts/discourse/app/components/edit-category-settings.hbs +++ b/app/assets/javascripts/discourse/app/components/edit-category-settings.hbs @@ -187,7 +187,7 @@ {{i18n "category.num_auto_bump_daily"}} 0') - .pluck(:category_id) - - if (auto_bumps.length > 0) - auto_bumps.shuffle.each do |category_id| - bumped = Category.find_by(id: category_id)&.auto_bump_topic! - break if bumped - end - end - - bumped + Category + .joins(:category_setting) + .where("category_settings.num_auto_bump_daily > 0") + .shuffle + .any?(&:auto_bump_topic!) end # will automatically bump a single topic diff --git a/app/models/category_setting.rb b/app/models/category_setting.rb index 4506d634af8..8c70964e567 100644 --- a/app/models/category_setting.rb +++ b/app/models/category_setting.rb @@ -26,7 +26,7 @@ end # category_id :bigint not null # require_topic_approval :boolean # require_reply_approval :boolean -# num_auto_bump_daily :integer +# num_auto_bump_daily :integer default(0) # created_at :datetime not null # updated_at :datetime not null # auto_bump_cooldown_days :integer default(1) diff --git a/db/migrate/20230727015254_change_category_setting_num_auto_bump_daily_default.rb b/db/migrate/20230727015254_change_category_setting_num_auto_bump_daily_default.rb new file mode 100644 index 00000000000..1e1c345d888 --- /dev/null +++ b/db/migrate/20230727015254_change_category_setting_num_auto_bump_daily_default.rb @@ -0,0 +1,67 @@ +# frozen_string_literal: true + +class ChangeCategorySettingNumAutoBumpDailyDefault < ActiveRecord::Migration[7.0] + def up + change_column_default :category_settings, :num_auto_bump_daily, 0 + + execute(<<~SQL) + WITH custom_fields AS ( + SELECT + category_id, + MAX( + CASE WHEN (name = 'require_topic_approval') + THEN value ELSE NULL END + )::boolean AS require_topic_approval, + MAX( + CASE WHEN (name = 'require_reply_approval') + THEN value ELSE NULL END + )::boolean AS require_reply_approval, + MAX( + CASE WHEN (name = 'num_auto_bump_daily') + THEN value ELSE NULL END + )::integer AS num_auto_bump_daily, + NOW() AS created_at, + NOW() AS updated_at + FROM category_custom_fields + WHERE name IN ( + 'require_topic_approval', + 'require_reply_approval', + 'num_auto_bump_daily' + ) + GROUP BY category_id + ) + INSERT INTO + category_settings( + category_id, + require_topic_approval, + require_reply_approval, + num_auto_bump_daily, + created_at, + updated_at + ) + SELECT * FROM custom_fields + ON CONFLICT (category_id) DO + UPDATE SET + require_topic_approval = EXCLUDED.require_topic_approval, + require_reply_approval = EXCLUDED.require_reply_approval, + num_auto_bump_daily = EXCLUDED.num_auto_bump_daily, + updated_at = NOW() + SQL + + execute(<<~SQL) + UPDATE category_settings + SET num_auto_bump_daily = 0 + WHERE num_auto_bump_daily IS NULL; + SQL + end + + def down + change_column_default :category_settings, :num_auto_bump_daily, nil + + execute(<<~SQL) + UPDATE category_settings + SET num_auto_bump_daily = NULL + WHERE num_auto_bump_daily = 0; + SQL + end +end diff --git a/spec/models/category_spec.rb b/spec/models/category_spec.rb index 8b50208fd04..26c7eeea7af 100644 --- a/spec/models/category_spec.rb +++ b/spec/models/category_spec.rb @@ -976,10 +976,10 @@ RSpec.describe Category do category = Fabricate( :category_with_definition, - num_auto_bump_daily: 2, created_at: 1.minute.ago, category_setting_attributes: { auto_bump_cooldown_days: 1, + num_auto_bump_daily: 2, }, ) category.clear_auto_bump_cache! diff --git a/spec/requests/categories_controller_spec.rb b/spec/requests/categories_controller_spec.rb index bb5759f813a..f020e0662f8 100644 --- a/spec/requests/categories_controller_spec.rb +++ b/spec/requests/categories_controller_spec.rb @@ -716,7 +716,6 @@ RSpec.describe CategoriesController do it "updates per-category settings correctly" do category.custom_fields[Category::REQUIRE_TOPIC_APPROVAL] = false category.custom_fields[Category::REQUIRE_REPLY_APPROVAL] = false - category.custom_fields[Category::NUM_AUTO_BUMP_DAILY] = 0 category.navigate_to_first_post_after_read = false category.save! @@ -730,6 +729,8 @@ RSpec.describe CategoriesController do custom_fields: { require_reply_approval: true, require_topic_approval: true, + }, + category_setting_attributes: { num_auto_bump_daily: 10, }, }