discourse/app/services/categories/unconfigure.rb
Martin Brennan 1ce5697430
UX: Add or remove category types with a dropdown (#39477)
The current UX of removing category types ("Remove X type" button
in the relevant tab like Support/Ideas is not ideal, nor
is the way of adding more category types (hunting down the
right setting and enabling it).

This PR solves both problems by adding a multi-select dropdown
to the General tab of the category edit page, which lists all available
category types and allows adding or removing them in one place.

On our hosting, certain category types will be gated by plan
level.
2026-04-30 11:38:50 +10:00

60 lines
1.5 KiB
Ruby

# frozen_string_literal: true
module Categories
class Unconfigure
include Service::Base
params do
attribute :category_id, :integer
attribute :category_type, :string
validates :category_id, presence: true
validates :category_type, presence: true
validate :category_type_is_valid
def category_type_is_valid
return if category_type.blank?
return if Categories::TypeRegistry.valid?(category_type)
errors.add(:category_type, :invalid)
end
end
model :category
policy :can_modify_category
model :type_class
step :unconfigure_category
step :log_action
step :clear_category_type_counts_cache
private
def fetch_category(params:)
Category.find_by(id: params.category_id)
end
def can_modify_category(guardian:, category:)
guardian.can_edit_category?(category)
end
def fetch_type_class(params:)
Categories::TypeRegistry.get(params.category_type)
end
def unconfigure_category(type_class:, category:, guardian:)
type_class.unconfigure_category(category, guardian:)
end
def log_action(guardian:, category:, params:)
StaffActionLogger.new(guardian.user).log_custom(
"unconfigure_category_type",
{ category_id: category.id, category_type: params.category_type },
)
end
def clear_category_type_counts_cache
Discourse.cache.delete(Categories::TypeRegistry::COUNTS_CACHE_KEY)
end
end
end