2
0
Fork 0
mirror of https://github.com/discourse/discourse.git synced 2026-03-05 15:27:34 +08:00
discourse/app/services/categories/configure.rb
Martin Brennan 75a9a3ca13
FEATURE: Show editable category type settings in tab(s) (#38143)
Builds on top of 97bd00d707

When creating or editing a category, we need to show an individual
tab for each matching category type so the admin can see and change
those settings. These settings are defined server-side in a schema
and added to the FormKit form in the UI.

At first, we expect in most cases to only really match one type,
but it's possible to match more than one.

The only tab we are adding for now is the Support type for the
discourse-solved plugin, but this will allow us to easily add more in
the
future if we want to.

This commit also adds server-side code to determine the category
types for any given category based on the type registry, and adds
functionality
to load the setting schema into the form and save the results when
submitted,
along with a lot of other glue code

For new Support categories we will also prefill the name and style type
as an emoji

---------

Co-authored-by: Joffrey JAFFEUX <j.jaffeux@gmail.com>
Co-authored-by: awesomerobot <kris.aubuchon@discourse.org>
Co-authored-by: Sam Saffron <sam.saffron@gmail.com>
2026-03-05 11:44:43 +10:00

78 lines
1.9 KiB
Ruby

# frozen_string_literal: true
module Categories
class Configure
include Service::Base
params do
attribute :category_id, :integer
attribute :category_type, :string
attribute :category_configuration_values
attribute :site_setting_configuration_values
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
model :type_class
policy :can_modify_category
transaction do
step :enable_plugin
step :configure_site_settings
step :configure_category
end
step :log_action
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 enable_plugin(type_class:)
type_class.enable_plugin
end
def configure_site_settings(type_class:, category:, guardian:, params:)
type_class.configure_site_settings(
category,
guardian:,
configuration_values: params.site_setting_configuration_values || {},
)
end
def configure_category(type_class:, category:, guardian:, params:)
type_class.configure_category(
category,
guardian:,
configuration_values: params.category_configuration_values || {},
)
end
def log_action(guardian:, category:, params:)
StaffActionLogger.new(guardian.user).log_custom(
"configure_category_type",
{ category_id: category.id, category_type: params.category_type },
)
end
end
end