From 578891a5fecc934bc8618b072672f5a6c461b2bd Mon Sep 17 00:00:00 2001 From: Ted Johansson Date: Wed, 8 Mar 2023 14:25:13 +0800 Subject: [PATCH 1/3] DEV: Preparatory refactor of Category.auto_bump_topic! --- app/models/category.rb | 13 ++----------- 1 file changed, 2 insertions(+), 11 deletions(-) diff --git a/app/models/category.rb b/app/models/category.rb index 32397ee6aa..23aab1c218 100644 --- a/app/models/category.rb +++ b/app/models/category.rb @@ -647,22 +647,13 @@ class Category < ActiveRecord::Base end def self.auto_bump_topic! - bumped = false - auto_bumps = CategoryCustomField .where(name: Category::NUM_AUTO_BUMP_DAILY) .where('NULLIF(value, \'\')::int > 0') - .pluck(:category_id) + .select(: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.where(id: auto_bumps).shuffle.any? { |c| c.auto_bump_topic! } end # will automatically bump a single topic From cda91aa1fed84a26cbf01f36a224906de73ff1f1 Mon Sep 17 00:00:00 2001 From: Ted Johansson Date: Wed, 8 Mar 2023 15:06:17 +0800 Subject: [PATCH 2/3] DEV: Switch over num_auto_bump_daily from custom field to setting table --- .../app/components/edit-category-settings.hbs | 2 +- app/controllers/categories_controller.rb | 2 +- app/models/category.rb | 28 +++++++------------ spec/models/category_spec.rb | 2 +- spec/requests/categories_controller_spec.rb | 3 +- 5 files changed, 15 insertions(+), 22 deletions(-) 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 2a36e0531d..cd027fcb60 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') - .select(:category_id) - - Category.where(id: auto_bumps).shuffle.any? { |c| c.auto_bump_topic! } + Category + .joins(:category_setting) + .where("category_settings.num_auto_bump_daily > 0") + .shuffle + .any? { |c| c.auto_bump_topic! } end # will automatically bump a single topic diff --git a/spec/models/category_spec.rb b/spec/models/category_spec.rb index 3157f3e4db..48515c50db 100644 --- a/spec/models/category_spec.rb +++ b/spec/models/category_spec.rb @@ -974,10 +974,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 863ad47be4..fdcbb19f34 100644 --- a/spec/requests/categories_controller_spec.rb +++ b/spec/requests/categories_controller_spec.rb @@ -743,7 +743,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! @@ -757,6 +756,8 @@ RSpec.describe CategoriesController do custom_fields: { require_reply_approval: true, require_topic_approval: true, + }, + category_setting_attributes: { num_auto_bump_daily: 10, }, } From 52a142ac637151ab6ebc00a63eb8c2f9f5f46e32 Mon Sep 17 00:00:00 2001 From: Ted Johansson Date: Fri, 10 Mar 2023 16:51:31 +0800 Subject: [PATCH 3/3] DEV: Default num_auto_bump_daily to 0 --- app/models/category.rb | 2 +- app/models/category_setting.rb | 2 +- ...ory_setting_num_auto_bump_daily_default.rb | 23 +++++++++++++++++++ 3 files changed, 25 insertions(+), 2 deletions(-) create mode 100644 db/migrate/20230310082546_change_category_setting_num_auto_bump_daily_default.rb diff --git a/app/models/category.rb b/app/models/category.rb index 427dbc3fe6..684c68d49c 100644 --- a/app/models/category.rb +++ b/app/models/category.rb @@ -645,7 +645,7 @@ class Category < ActiveRecord::Base .joins(:category_setting) .where("category_settings.num_auto_bump_daily > 0") .shuffle - .any? { |c| c.auto_bump_topic! } + .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 4506d634af..8c70964e56 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/20230310082546_change_category_setting_num_auto_bump_daily_default.rb b/db/migrate/20230310082546_change_category_setting_num_auto_bump_daily_default.rb new file mode 100644 index 0000000000..89c0d72004 --- /dev/null +++ b/db/migrate/20230310082546_change_category_setting_num_auto_bump_daily_default.rb @@ -0,0 +1,23 @@ +# frozen_string_literal: true + +class ChangeCategorySettingNumAutoBumpDailyDefault < ActiveRecord::Migration[7.0] + def up + change_column_default :category_settings, :num_auto_bump_daily, 0 + + 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