Skip to content

A platform for community discussion. Free, open, simple.

DEV: Switch over category settings to new table - Part 2 (#20580)

Ted Johansson1f7e5e8e758f
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"}}
</label>
<NumberField
- @number={{this.category.custom_fields.num_auto_bump_daily}}
+ @number={{this.category.category_setting.num_auto_bump_daily}}
@id="category-number-daily-bump"
@type="number"
@min="0"
diff --git a/app/controllers/categories_controller.rb b/app/controllers/categories_controller.rb
index bb289d1fd39..7765ffc5d4a 100644
--- a/app/controllers/categories_controller.rb
+++ b/app/controllers/categories_controller.rb
@@ -411,7 +411,7 @@ class CategoriesController < ApplicationController
:read_only_banner,
:default_list_filter,
:reviewable_by_group_id,
- category_setting_attributes: %i[auto_bump_cooldown_days],
+ category_setting_attributes: %i[auto_bump_cooldown_days num_auto_bump_daily],
custom_fields: [custom_field_params],
permissions: [*p.try(:keys)],
allowed_tags: [],
diff --git a/app/models/category.rb b/app/models/category.rb
index db6ece10344..b0cadea1853 100644
--- a/app/models/category.rb
+++ b/app/models/category.rb
@@ -18,11 +18,9 @@ class Category < ActiveRecord::Base
REQUIRE_TOPIC_APPROVAL = "require_topic_approval"
REQUIRE_REPLY_APPROVAL = "require_reply_approval"
- NUM_AUTO_BUMP_DAILY = "num_auto_bump_daily"
register_custom_field_type(REQUIRE_TOPIC_APPROVAL, :boolean)
register_custom_field_type(REQUIRE_REPLY_APPROVAL, :boolean)
- register_custom_field_type(NUM_AUTO_BUMP_DAILY, :integer)
belongs_to :topic
belongs_to :topic_only_relative_url,
@@ -48,7 +46,11 @@ class Category < ActiveRecord::Base
has_one :category_setting, dependent: :destroy
- delegate :auto_bump_cooldown_days, to: :category_setting, allow_nil: true
+ delegate :auto_bump_cooldown_days,
+ :num_auto_bump_daily,
+ :num_auto_bump_daily=,
+ to: :category_setting,
+ allow_nil: true
has_and_belongs_to_many :web_hooks
@@ -673,14 +675,6 @@ class Category < ActiveRecord::Base
custom_fields[REQUIRE_REPLY_APPROVAL]
end
- def num_auto_bump_daily
- custom_fields[NUM_AUTO_BUMP_DAILY]
- end
-
- def num_auto_bump_daily=(v)
- custom_fields[NUM_AUTO_BUMP_DAILY] = v
- end
-
def auto_bump_limiter
return nil if num_auto_bump_daily.to_i == 0
RateLimiter.new(nil, "auto_bump_limit_#{self.id}", 1, 86_400 / num_auto_bump_daily.to_i)
@@ -691,22 +685,11 @@ 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)
-
- 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,
},
}