DEV: Switch over num_auto_bump_daily from custom field to setting table

This commit is contained in:
Ted Johansson 2023-03-08 15:06:17 +08:00
parent 578891a5fe
commit cda91aa1fe
No known key found for this signature in database
GPG Key ID: 2E801F82D9A4C6E9
5 changed files with 15 additions and 22 deletions

View File

@ -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"

View File

@ -406,7 +406,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: [],

View File

@ -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
@ -629,14 +631,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)
@ -647,13 +641,11 @@ class Category < ActiveRecord::Base
end
def self.auto_bump_topic!
auto_bumps =
CategoryCustomField
.where(name: Category::NUM_AUTO_BUMP_DAILY)
.where('NULLIF(value, \'\')::int > 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

View File

@ -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!

View File

@ -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,
},
}