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.
This commit is contained in:
Ted Johansson
2023-08-04 10:53:22 +08:00
committed by GitHub
parent 0fb86e6dc1
commit 1f7e5e8e75
7 changed files with 83 additions and 32 deletions

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

View File

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