Skip to content

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

FEATURE: Show editable category type settings in tab(s) (#38143)

Martin Brennan75a9a3ca136e
diff --git a/app/assets/stylesheets/admin/badges.scss b/app/assets/stylesheets/admin/badges.scss
index 4da01218ddb..d611f0ffd3e 100644
--- a/app/assets/stylesheets/admin/badges.scss
+++ b/app/assets/stylesheets/admin/badges.scss
@@ -98,6 +98,7 @@
.readonly-field {
color: var(--primary-high);
+ font-size: var(--font-down-1-rem);
}
.badge-preview {
diff --git a/app/assets/stylesheets/common/base/edit-category.scss b/app/assets/stylesheets/common/base/edit-category.scss
index b484a901970..f5911d28755 100644
--- a/app/assets/stylesheets/common/base/edit-category.scss
+++ b/app/assets/stylesheets/common/base/edit-category.scss
@@ -123,6 +123,7 @@ div.edit-category {
.category-permission-hint {
font-size: var(--font-down-1);
+ color: var(--primary-high-or-secondary-low);
}
}
diff --git a/app/assets/stylesheets/common/base/uc-simplified-category-setup-edit-categories.scss b/app/assets/stylesheets/common/base/uc-simplified-category-setup-edit-categories.scss
index 349f1b2c2af..ea6d2d73863 100644
--- a/app/assets/stylesheets/common/base/uc-simplified-category-setup-edit-categories.scss
+++ b/app/assets/stylesheets/common/base/uc-simplified-category-setup-edit-categories.scss
@@ -140,8 +140,11 @@
border-bottom: 1px solid var(--content-border-color);
}
+ .edit-category-page-header {
+ margin-bottom: 1.5em;
+ }
+
.edit-category-content {
- margin-top: 2em;
grid-area: unset;
}
}
diff --git a/app/assets/stylesheets/common/form-kit/_checkbox-group.scss b/app/assets/stylesheets/common/form-kit/_checkbox-group.scss
index 75b9128fa2d..777a1181f45 100644
--- a/app/assets/stylesheets/common/form-kit/_checkbox-group.scss
+++ b/app/assets/stylesheets/common/form-kit/_checkbox-group.scss
@@ -1,4 +1,4 @@
.form-kit__checkbox-group {
display: flex;
- gap: 0;
+ gap: 0.25em;
}
diff --git a/app/assets/stylesheets/common/form-kit/_container.scss b/app/assets/stylesheets/common/form-kit/_container.scss
index c45bd06dc9a..fbfd874b970 100644
--- a/app/assets/stylesheets/common/form-kit/_container.scss
+++ b/app/assets/stylesheets/common/form-kit/_container.scss
@@ -15,9 +15,8 @@
gap: 0.25em;
margin: 0;
font-size: var(--font-down-1-rem);
- color: var(--primary-high);
- font-weight: bold;
- padding-bottom: 0.25em;
+ color: var(--primary);
+ font-weight: 600 !important;
}
&-subtitle {
@@ -26,8 +25,7 @@
gap: 0.25em;
margin: 0;
font-size: var(--font-down-1-rem);
- color: var(--primary-high);
- padding-bottom: 0.25em;
+ color: var(--primary-medium);
> * {
margin: 0;
diff --git a/app/assets/stylesheets/common/form-kit/_control-checkbox.scss b/app/assets/stylesheets/common/form-kit/_control-checkbox.scss
index c368ead8a60..4010161e0ca 100644
--- a/app/assets/stylesheets/common/form-kit/_control-checkbox.scss
+++ b/app/assets/stylesheets/common/form-kit/_control-checkbox.scss
@@ -9,9 +9,14 @@
&-label {
display: flex;
gap: 0.5em;
- font-weight: normal !important;
margin: 0;
+ font-weight: normal !important;
color: var(--primary);
+ font-size: var(--font-down-1-rem);
+
+ .form-kit__control-checkbox-title {
+ font-weight: 600 !important;
+ }
.form-kit__field[data-disabled] & {
cursor: not-allowed;
diff --git a/app/assets/stylesheets/common/form-kit/_control-menu.scss b/app/assets/stylesheets/common/form-kit/_control-menu.scss
index 50007c3e828..d9cf41454ca 100644
--- a/app/assets/stylesheets/common/form-kit/_control-menu.scss
+++ b/app/assets/stylesheets/common/form-kit/_control-menu.scss
@@ -2,6 +2,7 @@
justify-content: space-between;
@include default-input;
+ width: auto;
}
.form-kit__control-menu-content {
diff --git a/app/assets/stylesheets/common/form-kit/_control-radio.scss b/app/assets/stylesheets/common/form-kit/_control-radio.scss
index 0d00d5bf072..ddb708fbff2 100644
--- a/app/assets/stylesheets/common/form-kit/_control-radio.scss
+++ b/app/assets/stylesheets/common/form-kit/_control-radio.scss
@@ -1,9 +1,10 @@
.form-kit__control-radio-label {
display: flex;
gap: 0.5em;
- font-weight: normal !important;
+ font-weight: 500 !important;
margin: 0;
color: var(--primary);
+ font-size: var(--font-down-1-rem);
.form-kit__field[data-disabled] & {
cursor: not-allowed;
diff --git a/app/assets/stylesheets/common/form-kit/_field.scss b/app/assets/stylesheets/common/form-kit/_field.scss
index 670b6f3cc6c..c8119bb5dff 100644
--- a/app/assets/stylesheets/common/form-kit/_field.scss
+++ b/app/assets/stylesheets/common/form-kit/_field.scss
@@ -35,17 +35,18 @@
.form-kit__container-description {
margin: 0;
- font-size: var(--font-down-1-rem);
+ font-size: var(--font-down-1);
+ color: var(--primary-medium);
pre,
code {
- font-size: var(--font-down-2-rem);
+ font-size: var(--font-down-2);
}
}
.form-kit__container-help-text {
margin: 0;
- font-size: var(--font-down-1-rem);
+ font-size: var(--font-down-1);
color: var(--primary-high-or-secondary-low);
}
diff --git a/app/assets/stylesheets/common/form-kit/_section.scss b/app/assets/stylesheets/common/form-kit/_section.scss
index 05ef24389db..19703f9f69c 100644
--- a/app/assets/stylesheets/common/form-kit/_section.scss
+++ b/app/assets/stylesheets/common/form-kit/_section.scss
@@ -1,8 +1,9 @@
.form-kit__section {
display: flex;
flex-direction: column;
- gap: 1.5rem;
+ gap: 1rem;
width: 100%;
+ padding-bottom: 1.5rem;
&.form-kit__actions {
align-items: center;
@@ -13,9 +14,39 @@
.form-kit__section-title {
margin: 0;
- font-size: var(--font-up-1-rem);
+ font-size: var(--font-up-1);
+}
+
+.form-kit__section-header {
+ border-bottom: 1px solid var(--content-border-color);
+ padding-bottom: 0.5em;
}
.form-kit__section-subtitle {
margin-top: calc(var(--space-4) * -1); // Offset the gap of the section
+ color: var(--primary-medium);
+ font-size: var(--font-down-1);
+}
+
+.form-kit__section.emphasis {
+ border: 2px solid var(--tertiary);
+ border-radius: var(--d-border-radius);
+ padding: 1em;
+ box-sizing: border-box;
+
+ &.emphasis-success {
+ border-color: var(--success-medium);
+ }
+
+ &.emphasis-error {
+ border-color: var(--danger-medium);
+ }
+
+ &.emphasis-warning {
+ border-color: var(--highlight);
+ }
+
+ &.emphasis-info {
+ border-color: var(--tertiary-medium);
+ }
}
diff --git a/app/controllers/categories_controller.rb b/app/controllers/categories_controller.rb
index 5b0f72859bb..b58638ae275 100644
--- a/app/controllers/categories_controller.rb
+++ b/app/controllers/categories_controller.rb
@@ -173,8 +173,17 @@ class CategoriesController < ApplicationController
@category.move_to(position.to_i) if position
if category_type.present? &&
- UpcomingChanges.enabled_for_user?(:enable_simplified_category_creation, current_user)
- Categories::Configure.call(guardian:, params: { category_id: @category.id, category_type: })
+ UpcomingChanges.enabled_for_user?(:enable_simplified_category_creation, current_user) &&
+ SiteSetting.enable_category_type_setup
+ Categories::Configure.call(
+ guardian:,
+ params: {
+ category_id: @category.id,
+ category_type:,
+ site_setting_configuration_values: params[:category_type_site_settings],
+ category_configuration_values: category_params[:custom_fields],
+ },
+ )
end
Scheduler::Defer.later "Log staff action create category" do
@@ -199,15 +208,46 @@ class CategoriesController < ApplicationController
old_custom_fields = cat.custom_fields.dup
if category_params[:custom_fields]
category_params[:custom_fields].each do |key, value|
- if value.present?
- cat.custom_fields[key] = value
- else
+ if value.nil? || value == ""
cat.custom_fields.delete(key)
+ else
+ cat.custom_fields[key] = value
end
end
end
category_params.delete(:custom_fields)
+ if SiteSetting.enable_category_type_setup &&
+ UpcomingChanges.enabled_for_user?(:enable_simplified_category_creation, current_user) &&
+ params[:category_type_site_settings].present?
+ # NOTE: The code in this block is pretty similar to what we are doing in
+ # configure_site_settings in Categories::Types::Base, however here we
+ # need to be able to update site settings across all category types for
+ # the category and it's best to do this in one query rather than
+ # multiple.
+ #
+ # Maybe in future we do something different, but this is a good starting point.
+ category_type_settings =
+ params[:category_type_site_settings].permit!.to_h.map do |name, value|
+ { setting_name: name, value: }
+ end
+
+ # We do this because we want to allow updating hidden settings for the
+ # category type, but not other settings. The configuration schema for a
+ # category type defines which settings it wants to change, so that's a
+ # good source to use as an allowlist here.
+ allowed_setting_names = cat.category_type_site_setting_names
+ SiteSetting::Update.call(
+ guardian:,
+ options: {
+ allow_changing_hidden: allowed_setting_names,
+ },
+ params: {
+ settings: category_type_settings,
+ },
+ )
+ end
+
# properly null the value so the database constraint doesn't catch us
category_params[:email_in] = nil if category_params[:email_in]&.blank?
category_params[:minimum_required_tags] = 0 if category_params[:minimum_required_tags]&.blank?
diff --git a/app/models/category.rb b/app/models/category.rb
index fa84c0b239c..73191d0d81d 100644
--- a/app/models/category.rb
+++ b/app/models/category.rb
@@ -312,6 +312,25 @@ class Category < ActiveRecord::Base
Category.reset_topic_ids_cache
end
+ def category_types
+ return {} if !SiteSetting.enable_simplified_category_creation
+ Categories::TypeRegistry
+ .all
+ .values
+ .each_with_object({}) do |type_klass, result|
+ result[type_klass.type_id] = type_klass.metadata if type_klass.category_matches?(self)
+ end
+ end
+
+ def category_type_site_setting_names
+ category_types
+ .values
+ .filter_map { |type_metadata| type_metadata.dig(:configuration_schema, :site_settings) }
+ .flatten(1)
+ .map { |setting| setting[:key].to_sym }
+ .uniq
+ end
+
# Accepts an array of slugs with each item in the array
# Returns the category ids of the last slug in the array. The slugs array has to follow the proper category
# nesting hierarchy. If any of the slug in the array is invalid or if the slugs array does not follow the proper
diff --git a/app/serializers/category_serializer.rb b/app/serializers/category_serializer.rb
index 15c6de1d9ca..94693c4cfda 100644
--- a/app/serializers/category_serializer.rb
+++ b/app/serializers/category_serializer.rb
@@ -36,7 +36,8 @@ class CategorySerializer < SiteCategorySerializer
:default_slow_mode_seconds,
:style_type,
:emoji,
- :icon
+ :icon,
+ :category_types
has_one :category_setting, serializer: CategorySettingSerializer, embed: :objects
has_many :category_localizations, serializer: CategoryLocalizationSerializer, embed: :objects
@@ -148,4 +149,11 @@ class CategorySerializer < SiteCategorySerializer
def description
category_description
end
+
+ def category_types
+ if !SiteSetting.enable_simplified_category_creation || !SiteSetting.enable_category_type_setup
+ return {}
+ end
+ object.category_types
+ end
end
diff --git a/app/services/categories/configure.rb b/app/services/categories/configure.rb
index 79aea64fb05..c2cbb30d748 100644
--- a/app/services/categories/configure.rb
+++ b/app/services/categories/configure.rb
@@ -7,7 +7,8 @@ module Categories
params do
attribute :category_id, :integer
attribute :category_type, :string
- attribute :configuration_values
+ attribute :category_configuration_values
+ attribute :site_setting_configuration_values
validates :category_id, presence: true
validates :category_type, presence: true
@@ -51,17 +52,19 @@ module Categories
type_class.enable_plugin
end
- def configure_site_settings(type_class:, category:, params:)
+ def configure_site_settings(type_class:, category:, guardian:, params:)
type_class.configure_site_settings(
category,
- configuration_values: params.configuration_values || {},
+ guardian:,
+ configuration_values: params.site_setting_configuration_values || {},
)
end
- def configure_category(type_class:, category:, params:)
+ def configure_category(type_class:, category:, guardian:, params:)
type_class.configure_category(
category,
- configuration_values: params.configuration_values || {},
+ guardian:,
+ configuration_values: params.category_configuration_values || {},
)
end
diff --git a/app/services/categories/types/base.rb b/app/services/categories/types/base.rb
index 42cefd08275..a78aa654283 100644
--- a/app/services/categories/types/base.rb
+++ b/app/services/categories/types/base.rb
@@ -3,7 +3,63 @@
module Categories
module Types
class Base
+ CONFIGURATION_SCHEMA_DEFINITION = {
+ "type" => "object",
+ "additionalProperties" => false,
+ "properties" => {
+ "general_category_settings" => {
+ "type" => "object",
+ "additionalProperties" => {
+ "$ref" => "#/$defs/field_config",
+ },
+ },
+ "site_settings" => {
+ "type" => "object",
+ },
+ "category_custom_fields" => {
+ "type" => "object",
+ "additionalProperties" => {
+ "$ref" => "#/$defs/field_config",
+ },
+ },
+ "category_settings" => {
+ "type" => "object",
+ "additionalProperties" => {
+ "$ref" => "#/$defs/field_config",
+ },
+ },
+ },
+ "$defs" => {
+ "field_config" => {
+ "type" => "object",
+ "required" => %w[default type label],
+ "additionalProperties" => false,
+ "properties" => {
+ "default" => true,
+ "type" => {
+ "type" => "string",
+ "minLength" => 1,
+ },
+ "label" => {
+ "type" => "string",
+ "minLength" => 1,
+ },
+ "description" => {
+ "type" => "string",
+ },
+ "show_on_create" => {
+ "type" => "boolean",
+ },
+ "show_on_edit" => {
+ "type" => "boolean",
+ },
+ },
+ },
+ },
+ }.freeze
+
class << self
+ # Every category type must have a unique type_id.
def type_id(id = nil)
if id
@type_id = id.to_sym
@@ -12,23 +68,115 @@ module Categories
end
end
- def enable_plugin
+ # Returns true if the category provided is of this type,
+ # based on settings, category attributes, etc.
+ #
+ # This MUST be overridden by category types.
+ def category_matches?(category)
+ raise NotImplementedError
end
- def configure_site_settings(category, configuration_values: {})
- configuration_schema[:site_settings]&.each do |setting_name, default_value|
- value = configuration_values.fetch(setting_name.to_s, default_value)
- SiteSetting.public_send("#{setting_name}=", value)
- end
+ # Use this to enable any related plugin for the category type,
+ # since we register category types without the plugin being enabled.
+ #
+ # This SHOULD be overridden by category types if they are related to a plugin.
+ def enable_plugin
end
- def configure_category(category, configuration_values: {})
+ # Configure any category-specific settings or custom fields that are
+ # specific to this category type.
+ #
+ # This SHOULD be overridden by category types.
+ def configure_category(category, guardian:, configuration_values: {})
end
+ # Returns a hash describing the configuration schema for this category type.
+ # This schema drives both the UI (what settings are shown to admins in the
+ # category creator) and the site-setting update allowlist.
+ #
+ # The hash MAY include any subset of the following top-level keys.
+ # All top-level keys are optional; an empty hash is valid.
+ #
+ # {
+ # general_category_settings: {
+ # # Used to prefill basic category fields when creating a new category.
+ # # Each key is the category field name (Symbol).
+ # # Each value is a config Hash:
+ # # default: (required) Any value; the field's default when creating a new category.
+ # # type: (required) Symbol — e.g. :integer, :string, :boolean, matching site setting types.
+ # name: {
+ # value: "My Category",
+ # type: :string,
+ # },
+ # style_type: {
+ # value: "emoji",
+ # type: :string,
+ # },
+ # emoji: {
+ # value: "🔥",
+ # type: :string,
+ # },
+ # },
+ #
+ # site_settings: {
+ # # Each key must be a valid SiteSetting name.
+ # # The value is the desired default to apply when this category type is configured.
+ # show_filter_by_solved_status: true,
+ # },
+ #
+ # category_custom_fields: {
+ # # Each key is the custom field name (Symbol).
+ # # Each value is a config Hash:
+ # # default: (required) Any value; the field's default.
+ # # type: (required) Symbol — e.g. :integer, :string, :boolean, matching site setting types.
+ # # label: (required) String — FormKit label shown in UI
+ # # description: (optional) String — FormKit description/help text shown in UI
+ # # show_on_create: (optional) Boolean — Whether to show the field on category creation. Defaults to true.
+ # # show_on_edit: (optional) Boolean — Whether to show the field on category edit. Defaults to true.
+ # solved_topics_auto_close_hours: {
+ # default: 48,
+ # type: :integer,
+ # label: "Auto-close after solved (hours)",
+ # description: "Close topics this many hours after being solved.",
+ # },
+ # },
+ #
+ # category_settings: {
+ # # Same structure as category_custom_fields above.
+ # },
+ # }
+ #
+ # Use +validate_schema!+ to verify a schema conforms to this contract.
def configuration_schema
{}
end
+ # Validates the hash returned by +configuration_schema+ using JSONSchemer.
+ # Raises +ArgumentError+ with a descriptive message if invalid.
+ # Also validates that any site_settings keys are real SiteSettings (a
+ # runtime check that cannot be expressed in JSON Schema).
+ def validate_schema!
+ # Normalize Ruby symbol keys/values to strings via JSON round-trip
+ schema_as_json = JSON.parse(configuration_schema.to_json)
+
+ schemer = JSONSchemer.schema(CONFIGURATION_SCHEMA_DEFINITION)
+ errors = schemer.validate(schema_as_json).to_a
+ if errors.any?
+ messages = errors.map { |err| JSONSchemer::Errors.pretty(err) }.join("; ")
+ raise ArgumentError, "#{name}#configuration_schema is invalid: #{messages}"
+ end
+
+ # Validate site_settings keys are real SiteSettings (runtime check)
+ schema_as_json["site_settings"]&.each_key do |setting_name|
+ unless SiteSetting.has_setting?(setting_name)
+ raise ArgumentError,
+ "#{name}#configuration_schema[:site_settings] references unknown SiteSetting: #{setting_name.inspect}"
+ end
+ end
+ end
+
+ # Used as an extension point to limit access to a category type
+ # based on certain conditions, mostly for Discourse hosting.
def available?
true
end
@@ -37,6 +185,53 @@ module Categories
"memo"
end
+ # Configure any custom fields that are specific to this category type,
+ # should be called from within +configure_category+ for each type.
+ #
+ # This SHOULD NOT be overridden by category types.
+ def configure_custom_fields(category, guardian:, configuration_values: {})
+ configuration_schema[:category_custom_fields]&.each do |field_name, config|
+ value = configuration_values.fetch(field_name.to_s, config[:default])
+ category.custom_fields[field_name.to_s] = value
+ end
+
+ category.save_custom_fields
+ end
+
+ # Configure any site settings that are specific to this category type.
+ # The configuration schema must be defined for this, as it is also used
+ # to show related settings in the UI for the category creator based
+ # on type.
+ #
+ # This SHOULD NOT be overridden by category types.
+ def configure_site_settings(category, guardian:, configuration_values: {})
+ category_type_settings =
+ configuration_schema[:site_settings]&.map do |setting_name, default_value|
+ {
+ setting_name: setting_name.to_s,
+ value: configuration_values.fetch(setting_name.to_s, default_value),
+ }
+ end
+
+ return if category_type_settings.blank?
+
+ # We do this because we want to allow updating hidden settings for the
+ # category type, but not other settings. The configuration schema for
+ # a category type defines which settings it wants to change, so that's
+ # a good source to use as an allowlist here.
+ allowed_setting_names = category.category_type_site_setting_names
+ SiteSetting::Update.call(
+ guardian:,
+ options: {
+ allow_changing_hidden: allowed_setting_names,
+ },
+ params: {
+ settings: category_type_settings,
+ },
+ )
+ end
+
+ # Used when serializing the category configuration schema to the client.
def metadata
name = I18n.t("category_types.#{type_id}.name", default: type_id.to_s.titleize)
{
@@ -44,7 +239,7 @@ module Categories
name: name,
title: I18n.t("category_types.#{type_id}.title", default: name),
description: I18n.t("category_types.#{type_id}.description", default: ""),
- icon: icon,
+ icon:,
available: available?,
configuration_schema: resolved_configuration_schema,
}
@@ -54,28 +249,64 @@ module Categories
def resolved_configuration_schema
schema = configuration_schema
- return [] if schema.blank?
+ return {} if schema.blank?
+
+ entries = {
+ general_category_settings: [],
+ site_settings: [],
+ category_settings: [],
+ category_custom_fields: [],
+ }
- entries = []
+ schema[:general_category_settings]&.each do |setting_name, config|
+ entries[:general_category_settings] << {
+ key: setting_name.to_s,
+ default: config[:default],
+ type: config[:type].to_s,
+ required: config[:required],
+ show_on_create: config[:show_on_create].nil? ? true : config[:show_on_create],
+ show_on_edit: config[:show_on_edit].nil? ? true : config[:show_on_edit],
+ }
+ end
schema[:site_settings]&.each do |setting_name, target_value|
meta = SiteSetting.setting_metadata_hash(setting_name)
- entries << {
+ entries[:site_settings] << {
key: setting_name.to_s,
default: target_value,
+ current: SiteSetting.public_send(setting_name),
type: meta[:type],
label: meta[:humanized_name],
description: meta[:description],
+ required: false,
+ show_on_create: true,
+ show_on_edit: true,
}
end
schema[:category_settings]&.each do |field_name, config|
- entries << {
+ entries[:category_settings] << {
+ key: field_name.to_s,
+ default: config[:default],
+ type: config[:type].to_s,
+ label: config[:label],
+ description: config[:description],
+ required: config[:required],
+ show_on_create: config[:show_on_create].nil? ? true : config[:show_on_create],
+ show_on_edit: config[:show_on_edit].nil? ? true : config[:show_on_edit],
+ }
+ end
+
+ schema[:category_custom_fields]&.each do |field_name, config|
+ entries[:category_custom_fields] << {
key: field_name.to_s,
default: config[:default],
type: config[:type].to_s,
label: config[:label],
description: config[:description],
+ required: config[:required],
+ show_on_create: config[:show_on_create].nil? ? true : config[:show_on_create],
+ show_on_edit: config[:show_on_edit].nil? ? true : config[:show_on_edit],
}
end
diff --git a/app/services/categories/types/discussion.rb b/app/services/categories/types/discussion.rb
index 9d0010ffa03..14bceb4d577 100644
--- a/app/services/categories/types/discussion.rb
+++ b/app/services/categories/types/discussion.rb
@@ -3,6 +3,21 @@
module Categories
module Types
class Discussion < Base
+ type_id :discussion
+
+ class << self
+ def category_matches?(category)
+ # NOTE (martin) For now, all categories are considered discussion categories,
+ # a discussion category is basically the old "vanilla" category type
+ # for Discourse.
+ #
+ # Discussion categories don't have a special tab to show their settings like
+ # e.g. Solved
+ #
+ # Maybe we will reconsider this in future iterations.
+ true
+ end
+ end
end
end
end
diff --git a/app/services/site_setting/update.rb b/app/services/site_setting/update.rb
index 91a0a05697b..636a4675d37 100644
--- a/app/services/site_setting/update.rb
+++ b/app/services/site_setting/update.rb
@@ -54,6 +54,11 @@ class SiteSetting::Update
raw_value
end
+ # Make sure to get the correct value here based on type, otherwise
+ # we can end up inserting a staff action log when e.g. setting the value
+ # to "false" when the setting value is already false.
+ setting.value = SiteSetting.type_supervisor.to_rb_value(setting.name, setting.value)
+
setting
end
end
diff --git a/config/locales/client.en.yml b/config/locales/client.en.yml
index 2a3d366b49a..e3da750e7de 100644
--- a/config/locales/client.en.yml
+++ b/config/locales/client.en.yml
@@ -2592,6 +2592,7 @@ en:
form_kit:
reset: Reset
optional: optional
+ required: required
errors_summary_title:
one: "Fix this error to continue"
other: "Fix these errors to continue"
@@ -4674,6 +4675,11 @@ en:
create: "New category"
create_with_type: "New %{typeName} category"
create_long: "Create a new category"
+ settings_apply_to_all_of_type_warning: "Changes below will apply to all categories of this type"
+ type_settings_schema:
+ category_settings: "Category settings"
+ category_custom_fields: "Category settings"
+ site_settings: "Site settings"
choose_type:
title: "Choose category type"
requires_plugin: "Requires plugin"
@@ -7975,6 +7981,7 @@ en:
upcoming_change_available: "upcoming change available"
change_site_setting_groups: "change site setting groups"
discourse_id_regenerate_credentials: "regenerate Discourse ID credentials"
+ configure_category_type: "configure category type"
screened_emails:
title: "Screened emails"
description: "When someone tries to create a new account, the following email addresses will be checked and the registration will be blocked, or some other action performed."
diff --git a/config/locales/server.en.yml b/config/locales/server.en.yml
index e14dfcbe8e0..0048cf8fcea 100644
--- a/config/locales/server.en.yml
+++ b/config/locales/server.en.yml
@@ -898,7 +898,7 @@ en:
discussion:
name: "Discussion"
title: "discussion"
- description: "All the essentials for paragraph-based conversation."
+ description: "All the essentials for paragraph-based conversation"
trust_levels:
admin: "Admin"
diff --git a/frontend/discourse/admin/components/edit-category-tab.gjs b/frontend/discourse/admin/components/edit-category-tab.gjs
index cd475015bb8..1a3821f0dad 100644
--- a/frontend/discourse/admin/components/edit-category-tab.gjs
+++ b/frontend/discourse/admin/components/edit-category-tab.gjs
@@ -20,7 +20,7 @@ export default class EditCategoryTab extends Component {
@computed("tab")
get tabClassName() {
- return "edit-category-" + this.tab;
+ return `edit-category-${this.tab}`;
}
@computed("tab", "tabTitle")
diff --git a/frontend/discourse/admin/components/upsert-category/general.gjs b/frontend/discourse/admin/components/upsert-category/general.gjs
index d2db4bec9df..b2df01bf2ff 100644
--- a/frontend/discourse/admin/components/upsert-category/general.gjs
+++ b/frontend/discourse/admin/components/upsert-category/general.gjs
@@ -537,15 +537,14 @@ export default class UpsertCategoryGeneral extends Component {
@onChange={{this.onChangeAccessGroups}}
@options={{hash disabled=this.isParentRestricted}}
/>
+ {{! template-lint-disable no-invalid-interactive }}
+ <span
+ class="category-permission-hint"
+ {{on "click" this.goToSecurityTab}}
+ >
+ {{this.permissionHint}}
+ </span>
</@form.Container>
-
- {{! template-lint-disable no-invalid-interactive }}
- <span
- class="category-permission-hint"
- {{on "click" this.goToSecurityTab}}
- >
- {{this.permissionHint}}
- </span>
</Content>
</cc.Contents>
</@form.ConditionalContent>
diff --git a/frontend/discourse/admin/controllers/edit-category/tabs.js b/frontend/discourse/admin/controllers/edit-category/tabs.js
index e5af92f8a30..798ed4e9821 100644
--- a/frontend/discourse/admin/controllers/edit-category/tabs.js
+++ b/frontend/discourse/admin/controllers/edit-category/tabs.js
@@ -92,23 +92,51 @@ export default class EditCategoryTabsController extends Controller {
@and("showTooltip", "model.cannot_delete_reason") showDeleteReason;
get formData() {
- const simplified = this.siteSettings.enable_simplified_category_creation;
+ const enableSimplifiedCategoryCreation =
+ this.siteSettings.enable_simplified_category_creation;
const data = getProperties(
this.model,
- ...(simplified ? SIMPLIFIED_FIELD_LIST : FIELD_LIST)
+ ...(enableSimplifiedCategoryCreation ? SIMPLIFIED_FIELD_LIST : FIELD_LIST)
);
- if (simplified && !this.model.styleType) {
- data.style_type = "icon";
- }
+ if (enableSimplifiedCategoryCreation) {
+ if (!this.model.styleType) {
+ data.style_type = "icon";
+ }
- if (simplified) {
data.required_tag_groups = Array.from(
data.required_tag_groups ?? [],
(rtg) => ({ ...rtg })
);
data.category_setting = { ...(this.model.category_setting ?? {}) };
data.custom_fields = { ...(this.model.custom_fields ?? {}) };
+
+ Object.entries(data.custom_fields).forEach(([key, value]) => {
+ // Coerce to boolean otherwise FormKit checkboxes do not work correctly.
+ if (value === "true" || value === "false") {
+ data.custom_fields[key] = value === "true";
+ }
+ });
+
+ if (this.siteSettings.enable_category_type_setup) {
+ data.category_type_site_settings = {};
+
+ Object.values(this.model.category_types).forEach((categoryType) => {
+ categoryType.configuration_schema.category_custom_fields?.forEach(
+ (field) => {
+ data.custom_fields[field.key] ??= field.default;
+ }
+ );
+
+ categoryType.configuration_schema.site_settings?.forEach(
+ (setting) => {
+ data.category_type_site_settings[setting.key] = this.model.id
+ ? setting.current
+ : setting.default;
+ }
+ );
+ });
+ }
}
return data;
@@ -140,9 +168,10 @@ export default class EditCategoryTabsController extends Controller {
});
}
- if (this.model.category_type_title) {
+ const types = Object.values(this.model.category_types ?? {});
+ if (types.length > 0) {
return i18n("category.create_with_type", {
- typeName: this.model.category_type_title,
+ typeName: types[0].name.toLowerCase(),
});
}
diff --git a/frontend/discourse/admin/routes/new-category/tabs.js b/frontend/discourse/admin/routes/new-category/tabs.js
index 1926ea501b8..debff95a4d8 100644
--- a/frontend/discourse/admin/routes/new-category/tabs.js
+++ b/frontend/discourse/admin/routes/new-category/tabs.js
@@ -25,12 +25,24 @@ export default class NewCategoryTabs extends DiscourseRoute {
setupController(controller, model, transition) {
super.setupController(...arguments);
- const result = this.categoryTypeChooser.consume();
+ // NOTE (martin) This was previously consume() which got rid of the selection
+ // of category type in memory, but model() is called every time the tab is changed
+ // when creating a new category, so we need to keep the selection in memory.
+ //
+ // categoryTypeChooser is reset when leaving the newCategory route anyway.
+ const result = this.categoryTypeChooser.currentSelection();
if (result) {
- controller.model.set("category_type", result.type);
- controller.model.set("category_type_name", result.typeName);
- controller.model.set("category_type_title", result.typeTitle);
- controller.model.set("category_type_schema", result.typeSchema);
+ const initialTypes = {};
+ initialTypes[result.type] = {
+ id: result.type,
+ name: result.typeName,
+ configuration_schema: result.typeSchema,
+ };
+ model.set("category_types", initialTypes);
+
+ result.typeSchema.general_category_settings?.forEach((setting) => {
+ model.set(setting.key, setting.default);
+ });
}
const selectedTab = transition.to.params.tab;
diff --git a/frontend/discourse/admin/templates/edit-category/tabs-horizontal.gjs b/frontend/discourse/admin/templates/edit-category/tabs-horizontal.gjs
index 388070cb4b6..f006cba02e5 100644
--- a/frontend/discourse/admin/templates/edit-category/tabs-horizontal.gjs
+++ b/frontend/discourse/admin/templates/edit-category/tabs-horizontal.gjs
@@ -43,6 +43,7 @@ export default class EditCategoryTabsHorizontalTemplate extends Component {
@titleLabel={{@controller.baseTitle}}
@showDrawer={{true}}
@collapseActionsOnMobile={{false}}
+ ...attributes
>
<:actions>
diff --git a/frontend/discourse/admin/templates/edit-category/tabs.gjs b/frontend/discourse/admin/templates/edit-category/tabs.gjs
index 68e6c7ee725..82f4d13ce97 100644
--- a/frontend/discourse/admin/templates/edit-category/tabs.gjs
+++ b/frontend/discourse/admin/templates/edit-category/tabs.gjs
@@ -60,7 +60,10 @@ function componentFor(name, useSimplified) {
export default <template>
<div class="edit-category {{if @controller.expandedMenu 'expanded-menu'}}">
{{#if @controller.siteSettings.enable_simplified_category_creation}}
- <EditCategoryTabsHorizontal @controller={{@controller}} />
+ <EditCategoryTabsHorizontal
+ @controller={{@controller}}
+ class="edit-category-page-header"
+ />
{{else}}
<EditCategoryTabsVertical @controller={{@controller}} />
{{/if}}
@@ -168,7 +171,7 @@ export default <template>
</Form>
{{#if @controller.siteSettings.enable_simplified_category_creation}}
- {{#if @controller.isFormDirty}}
+ {{#if (or @controller.model.isNew @controller.isFormDirty)}}
<ChangesBanner
@bannerLabel={{i18n "category.unsaved_changes"}}
@saveLabel={{if
diff --git a/frontend/discourse/app/components/category-type-cards.gjs b/frontend/discourse/app/components/category-type-cards.gjs
index b550c2a703f..8dd8795961e 100644
--- a/frontend/discourse/app/components/category-type-cards.gjs
+++ b/frontend/discourse/app/components/category-type-cards.gjs
@@ -1,8 +1,9 @@
import Component from "@glimmer/component";
-import { fn } from "@ember/helper";
+import { concat, fn } from "@ember/helper";
import { on } from "@ember/modifier";
import { action } from "@ember/object";
import { service } from "@ember/service";
+import concatClass from "discourse/helpers/concat-class";
import emoji from "discourse/helpers/emoji";
import { i18n } from "discourse-i18n";
@@ -26,8 +27,11 @@ export default class CategoryTypeCards extends Component {
{{#each @types as |type|}}
<button
type="button"
- class="category-type-cards__card
- {{unless type.available '--unavailable'}}"
+ class={{concatClass
+ "category-type-cards__card"
+ (unless type.available "--unavailable")
+ (concat "--category-type-" type.id)
+ }}
disabled={{unless type.available true}}
{{on "click" (fn this.selectType type)}}
>
diff --git a/frontend/discourse/app/components/d-page-header.gjs b/frontend/discourse/app/components/d-page-header.gjs
index 2ea77c4a0e7..df687a366b3 100644
--- a/frontend/discourse/app/components/d-page-header.gjs
+++ b/frontend/discourse/app/components/d-page-header.gjs
@@ -65,7 +65,7 @@ export default class DPageHeader extends Component {
<template>
{{#if this.shouldDisplay}}
- <div class="d-page-header">
+ <div class="d-page-header" ...attributes>
{{#if (has-block "breadcrumbs")}}
<div class="d-page-header__breadcrumbs">
<DBreadcrumbsContainer />
diff --git a/frontend/discourse/app/components/edit-category-type-schema-fields.gjs b/frontend/discourse/app/components/edit-category-type-schema-fields.gjs
new file mode 100644
index 00000000000..fb1b400260a
--- /dev/null
+++ b/frontend/discourse/app/components/edit-category-type-schema-fields.gjs
@@ -0,0 +1,109 @@
+import Component from "@glimmer/component";
+import { bind } from "discourse/lib/decorators";
+import { eq } from "discourse/truth-helpers";
+import { i18n } from "discourse-i18n";
+
+// NOTE: In future we may want to use FormKit versions of site setting components
+// here rather than this custom implementation. We are also only supporting a small
+// subset of site setting types / category field types for now, we can expand this as
+// needed.
+const SchemaFormField = <template>
+ {{#if (eq @entry.type "bool")}}
+ <@formObject.Field
+ @name={{@entry.key}}
+ @title={{@entry.label}}
+ @validation={{if @entry.required "required"}}
+ @format="full"
+ as |field|
+ >
+ <field.Checkbox>{{@entry.description}}</field.Checkbox>
+ </@formObject.Field>
+ {{else if (eq @entry.type "integer")}}
+ <@formObject.Field
+ @name={{@entry.key}}
+ @title={{@entry.label}}
+ @description={{@entry.description}}
+ @validation={{if @entry.required "required"}}
+ @titleFormat="full"
+ @descriptionFormat="full"
+ @format="small"
+ as |field|
+ >
+ <field.Input @type="number" />
+ </@formObject.Field>
+ {{else}}
+ <@formObject.Field
+ @name={{@entry.key}}
+ @title={{@entry.label}}
+ @description={{@entry.description}}
+ @validation={{if @entry.required "required"}}
+ @titleFormat="full"
+ @descriptionFormat="full"
+ @format="large"
+ as |field|
+ >
+ <field.Input />
+ </@formObject.Field>
+ {{/if}}
+</template>;
+
+export default class EditCategoryTypeSchemaFields extends Component {
+ get schema() {
+ return (
+ this.args.category?.getType(this.args.categoryType)
+ ?.configuration_schema ?? []
+ );
+ }
+
+ get className() {
+ let classes = ["edit-category-type-schema-fields"];
+ classes.push(`--category-type-${this.args.categoryType}`);
+ if (this.args.active) {
+ classes.push(" active");
+ }
+ return classes.join(" ");
+ }
+
+ @bind
+ shouldDisplayField(entry) {
+ if (this.args.category.isCreated) {
+ return entry.show_on_edit;
+ }
+
+ return entry.show_on_create;
+ }
+
+ <template>
+ <div class={{this.className}}>
+ <@form.Section>
+ <@form.Object @name="custom_fields" as |customFields|>
+ {{#each this.schema.category_custom_fields as |entry|}}
+ {{#if (this.shouldDisplayField entry)}}
+ <SchemaFormField
+ @category={{@category}}
+ @entry={{entry}}
+ @formObject={{customFields}}
+ />
+ {{/if}}
+ {{/each}}
+ </@form.Object>
+ </@form.Section>
+
+ <@form.Emphasis
+ @type="info"
+ @title={{i18n "category.type_settings_schema.site_settings"}}
+ @subtitle={{i18n "category.settings_apply_to_all_of_type_warning"}}
+ >
+ <@form.Object @name="category_type_site_settings" as |siteSettings|>
+ {{#each this.schema.site_settings as |entry|}}
+ <SchemaFormField
+ @category={{@category}}
+ @entry={{entry}}
+ @formObject={{siteSettings}}
+ />
+ {{/each}}
+ </@form.Object>
+ </@form.Emphasis>
+ </div>
+ </template>
+}
diff --git a/frontend/discourse/app/form-kit/components/fk/control-wrapper.gjs b/frontend/discourse/app/form-kit/components/fk/control-wrapper.gjs
index 1ecc95b3652..675ae375c43 100644
--- a/frontend/discourse/app/form-kit/components/fk/control-wrapper.gjs
+++ b/frontend/discourse/app/form-kit/components/fk/control-wrapper.gjs
@@ -121,6 +121,7 @@ export default class FKControlWrapper extends Component {
@categoryId={{@categoryId}}
@allowCreate={{@allowCreate}}
@placeholder={{@placeholder}}
+ @hasBlock={{has-block}}
@onControlWidthChange={{fn (mut this.controlWidth)}}
id={{@field.id}}
name={{@field.name}}
diff --git a/frontend/discourse/app/form-kit/components/fk/control/checkbox.gjs b/frontend/discourse/app/form-kit/components/fk/control/checkbox.gjs
index 922bb872cd0..548498f3dd8 100644
--- a/frontend/discourse/app/form-kit/components/fk/control/checkbox.gjs
+++ b/frontend/discourse/app/form-kit/components/fk/control/checkbox.gjs
@@ -5,6 +5,7 @@ import FKLabel from "discourse/form-kit/components/fk/label";
import FKOptional from "discourse/form-kit/components/fk/optional";
import FKTooltip from "discourse/form-kit/components/fk/tooltip";
import { eq } from "discourse/truth-helpers";
+import { i18n } from "discourse-i18n";
export default class FKControlCheckbox extends Component {
static controlType = "checkbox";
@@ -27,10 +28,17 @@ export default class FKControlCheckbox extends Component {
<span class="form-kit__control-checkbox-content">
<span class="form-kit__control-checkbox-title">
<span>{{@field.title}}</span>
- <FKOptional @field={{@field}} />
+
+ {{#if @showRequiredLabel}}
+ <span class="form-kit__container-required">({{i18n
+ "form_kit.required"
+ }})</span>
+ {{else}}
+ <FKOptional @field={{@field}} />
+ {{/if}}
<FKTooltip @field={{@field}} />
</span>
- {{#if (has-block)}}
+ {{#if @hasBlock}}
<span class="form-kit__control-checkbox-description">{{yield}}</span>
{{/if}}
</span>
diff --git a/frontend/discourse/app/form-kit/components/fk/emphasis.gjs b/frontend/discourse/app/form-kit/components/fk/emphasis.gjs
new file mode 100644
index 00000000000..b710f32aa3a
--- /dev/null
+++ b/frontend/discourse/app/form-kit/components/fk/emphasis.gjs
@@ -0,0 +1,36 @@
+import Component from "@glimmer/component";
+import { concat } from "@ember/helper";
+import concatClass from "discourse/helpers/concat-class";
+import { or } from "discourse/truth-helpers/index";
+
+export default class FKEmphasis extends Component {
+ get type() {
+ return this.args.type || "info";
+ }
+
+ <template>
+ <div
+ class={{concatClass
+ "form-kit__section"
+ "emphasis"
+ (concat "emphasis-" this.type)
+ @class
+ }}
+ ...attributes
+ >
+ {{#if (or @title @subtitle)}}
+ <div class="form-kit__section-header">
+ {{#if @title}}
+ <h2 class="form-kit__section-title">{{@title}}</h2>
+ {{/if}}
+
+ {{#if @subtitle}}
+ <span class="form-kit__section-subtitle">{{@subtitle}}</span>
+ {{/if}}
+ </div>
+ {{/if}}
+
+ {{yield}}
+ </div>
+ </template>
+}
diff --git a/frontend/discourse/app/form-kit/components/fk/form.gjs b/frontend/discourse/app/form-kit/components/fk/form.gjs
index 855afa8146c..0cce24f7cc2 100644
--- a/frontend/discourse/app/form-kit/components/fk/form.gjs
+++ b/frontend/discourse/app/form-kit/components/fk/form.gjs
@@ -14,6 +14,7 @@ import FKCheckboxGroup from "discourse/form-kit/components/fk/checkbox-group";
import FKCollection from "discourse/form-kit/components/fk/collection";
import FKContainer from "discourse/form-kit/components/fk/container";
import FKControlConditionalContent from "discourse/form-kit/components/fk/control/conditional-content";
+import FKEmphasis from "discourse/form-kit/components/fk/emphasis";
import FKErrorsSummary from "discourse/form-kit/components/fk/errors-summary";
import FKField from "discourse/form-kit/components/fk/field";
import FKFieldset from "discourse/form-kit/components/fk/fieldset";
@@ -320,6 +321,7 @@ class FKForm extends Component {
(hash
Row=Row
Section=FKSection
+ Emphasis=FKEmphasis
Fieldset=FKFieldset
ConditionalContent=(component FKControlConditionalContent)
Container=FKContainer
diff --git a/frontend/discourse/app/form-kit/components/fk/required.gjs b/frontend/discourse/app/form-kit/components/fk/required.gjs
new file mode 100644
index 00000000000..d5599f72f98
--- /dev/null
+++ b/frontend/discourse/app/form-kit/components/fk/required.gjs
@@ -0,0 +1,11 @@
+import { i18n } from "discourse-i18n";
+
+const FKRequired = <template>
+ {{#if @field.required}}
+ <span class="form-kit__container-required">({{i18n
+ "form_kit.required"
+ }})</span>
+ {{/if}}
+</template>;
+
+export default FKRequired;
diff --git a/frontend/discourse/app/form-kit/components/fk/section.gjs b/frontend/discourse/app/form-kit/components/fk/section.gjs
index e69da76f079..f8ca90321fe 100644
--- a/frontend/discourse/app/form-kit/components/fk/section.gjs
+++ b/frontend/discourse/app/form-kit/components/fk/section.gjs
@@ -1,13 +1,19 @@
import concatClass from "discourse/helpers/concat-class";
+import { or } from "discourse/truth-helpers/index";
const FKSection = <template>
<div class={{concatClass "form-kit__section" @class}} ...attributes>
- {{#if @title}}
- <h2 class="form-kit__section-title">{{@title}}</h2>
- {{/if}}
- {{#if @subtitle}}
- <span class="form-kit__section-subtitle">{{@subtitle}}</span>
+ {{#if (or @title @subtitle)}}
+ <div class="form-kit__section-header">
+ {{#if @title}}
+ <h2 class="form-kit__section-title">{{@title}}</h2>
+ {{/if}}
+
+ {{#if @subtitle}}
+ <span class="form-kit__section-subtitle">{{@subtitle}}</span>
+ {{/if}}
+ </div>
{{/if}}
{{yield}}
diff --git a/frontend/discourse/app/models/category.js b/frontend/discourse/app/models/category.js
index 86a5a8b86df..bee83188e3d 100644
--- a/frontend/discourse/app/models/category.js
+++ b/frontend/discourse/app/models/category.js
@@ -836,15 +836,29 @@ export default class Category extends RestModel {
...(this.siteSettings.content_localization_enabled && {
category_localizations: this.localizations,
}),
- ...(!id && this.category_type
- ? { category_type: this.category_type }
- : {}),
+ ...this._categoryTypeSaveProperties(id),
...this._pluginSaveProperties(),
}),
type: id ? "PUT" : "POST",
});
}
+ _categoryTypeSaveProperties(id) {
+ if (!this.siteSettings.enable_category_type_setup) {
+ return {};
+ }
+
+ const props = {
+ category_type_site_settings: this.category_type_site_settings,
+ };
+
+ if (!id && this.category_types) {
+ props.category_type = Object.keys(this.category_types)[0];
+ }
+
+ return props;
+ }
+
_pluginSaveProperties() {
return Array.from(pluginSaveProperties).reduce((obj, key) => {
obj[key] = this[key];
@@ -895,6 +909,14 @@ export default class Category extends RestModel {
});
}
+ isType(type) {
+ return Object.keys(this.category_types ?? {}).includes(type);
+ }
+
+ getType(type) {
+ return this.category_types?.[type];
+ }
+
@computed("topics")
get latestTopic() {
if (this.topics && this.topics.length) {
diff --git a/frontend/discourse/app/services/category-type-chooser.js b/frontend/discourse/app/services/category-type-chooser.js
index 29ef68b833d..4e4e31d2516 100644
--- a/frontend/discourse/app/services/category-type-chooser.js
+++ b/frontend/discourse/app/services/category-type-chooser.js
@@ -23,10 +23,8 @@ export default class CategoryTypeChooser extends Service {
this._setupComplete = true;
}
- consume() {
- const selection = this._selection;
- this._selection = null;
- return selection;
+ currentSelection() {
+ return this._selection;
}
reset() {
diff --git a/frontend/discourse/tests/acceptance/category-new-test.js b/frontend/discourse/tests/acceptance/category-new-test.js
index 5bc702e8618..7a6fd6f8890 100644
--- a/frontend/discourse/tests/acceptance/category-new-test.js
+++ b/frontend/discourse/tests/acceptance/category-new-test.js
@@ -136,6 +136,7 @@ acceptance("Category type setup page", function (needs) {
name: "Discussion",
icon: "comments",
description: "General discussion",
+ configuration_schema: {},
available: true,
},
{
@@ -143,6 +144,7 @@ acceptance("Category type setup page", function (needs) {
name: "Support",
icon: "circle-question",
description: "Q&A support",
+ configuration_schema: {},
available: true,
},
],
diff --git a/plugins/discourse-solved/app/models/discourse_solved/category_type.rb b/plugins/discourse-solved/app/models/discourse_solved/category_type.rb
deleted file mode 100644
index 6089edd93fe..00000000000
--- a/plugins/discourse-solved/app/models/discourse_solved/category_type.rb
+++ /dev/null
@@ -1,65 +0,0 @@
-# frozen_string_literal: true
-
-module DiscourseSolved
- class CategoryType < Categories::Types::Base
- type_id :support
-
- class << self
- def enable_plugin
- SiteSetting.solved_enabled = true
- end
-
- def configure_category(category, configuration_values: {})
- category.custom_fields[DiscourseSolved::ENABLE_ACCEPTED_ANSWERS_CUSTOM_FIELD] = "true"
-
- configuration_schema[:category_settings]&.each do |field_name, config|
- value = configuration_values.fetch(field_name.to_s, config[:default])
- category.custom_fields[field_name.to_s] = value
- end
- category.save_custom_fields
- DiscourseSolved::AcceptedAnswerCache.reset_accepted_answer_cache
- end
-
- def configuration_schema
- {
- site_settings: {
- show_filter_by_solved_status: true,
- notify_on_staff_accept_solved: true,
- empty_box_on_unsolved: true,
- },
- category_settings: {
- solved_topics_auto_close_hours: {
- default: 48,
- type: :integer,
- label: I18n.t("discourse_solved.category_type.solved_topics_auto_close_hours.label"),
- description:
- I18n.t("discourse_solved.category_type.solved_topics_auto_close_hours.description"),
- },
- },
- }
- end
-
- ICON_VARIANTS = %w[
- person_raising_hand:t2
- person_raising_hand:t3
- person_raising_hand:t4
- person_raising_hand:t5
- person_raising_hand:t6
- man_raising_hand:t2
- man_raising_hand:t3
- man_raising_hand:t4
- man_raising_hand:t5
- man_raising_hand:t6
- woman_raising_hand:t2
- woman_raising_hand:t3
- woman_raising_hand:t4
- woman_raising_hand:t5
- woman_raising_hand:t6
- ].freeze
-
- def icon
- ICON_VARIANTS.sample
- end
- end
- end
-end
diff --git a/plugins/discourse-solved/app/services/discourse_solved/categories/types/support.rb b/plugins/discourse-solved/app/services/discourse_solved/categories/types/support.rb
new file mode 100644
index 00000000000..37acf5e1f62
--- /dev/null
+++ b/plugins/discourse-solved/app/services/discourse_solved/categories/types/support.rb
@@ -0,0 +1,102 @@
+# frozen_string_literal: true
+
+module DiscourseSolved
+ module Categories
+ module Types
+ class Support < ::Categories::Types::Base
+ type_id :support
+
+ class << self
+ def enable_plugin
+ SiteSetting.solved_enabled = true
+ end
+
+ def category_matches?(category)
+ category.custom_fields[DiscourseSolved::ENABLE_ACCEPTED_ANSWERS_CUSTOM_FIELD] == "true"
+ end
+
+ def configure_category(category, guardian:, configuration_values: {})
+ configuration_values.merge!(
+ DiscourseSolved::ENABLE_ACCEPTED_ANSWERS_CUSTOM_FIELD => "true",
+ )
+ configure_custom_fields(category, guardian:, configuration_values:)
+
+ # NOTE (martin) In future we may want to handle category_settings
+ # here.
+ #
+ # Maybe more things from plugins need to move into this dedicated
+ # model, but for now we do need a distinction between this and category
+ # custom fields, not sure if this will be used at all yet.
+
+ DiscourseSolved::AcceptedAnswerCache.reset_accepted_answer_cache
+ end
+
+ def configuration_schema
+ {
+ general_category_settings: {
+ name: {
+ default: I18n.t("category_types.support.name"),
+ type: :string,
+ },
+ style_type: {
+ default: "emoji",
+ type: :string,
+ },
+ emoji: {
+ default: "red_question_mark",
+ type: :string,
+ },
+ },
+ site_settings: {
+ show_filter_by_solved_status: true,
+ notify_on_staff_accept_solved: true,
+ empty_box_on_unsolved: true,
+ },
+ category_custom_fields: {
+ DiscourseSolved::ENABLE_ACCEPTED_ANSWERS_CUSTOM_FIELD => {
+ default: true,
+ type: :bool,
+ label: I18n.t("discourse_solved.category_type.allow_accepted_answers.label"),
+ required: true,
+ show_on_create: false,
+ },
+ :solved_topics_auto_close_hours => {
+ default: 48,
+ type: :integer,
+ label:
+ I18n.t("discourse_solved.category_type.solved_topics_auto_close_hours.label"),
+ description:
+ I18n.t(
+ "discourse_solved.category_type.solved_topics_auto_close_hours.description",
+ ),
+ },
+ },
+ }
+ end
+
+ ICON_VARIANTS = %w[
+ person_raising_hand:t2
+ person_raising_hand:t3
+ person_raising_hand:t4
+ person_raising_hand:t5
+ person_raising_hand:t6
+ man_raising_hand:t2
+ man_raising_hand:t3
+ man_raising_hand:t4
+ man_raising_hand:t5
+ man_raising_hand:t6
+ woman_raising_hand:t2
+ woman_raising_hand:t3
+ woman_raising_hand:t4
+ woman_raising_hand:t5
+ woman_raising_hand:t6
+ ].freeze
+
+ def icon
+ ICON_VARIANTS.sample
+ end
+ end
+ end
+ end
+ end
+end
diff --git a/plugins/discourse-solved/assets/javascripts/discourse/components/upsert-category-support.gjs b/plugins/discourse-solved/assets/javascripts/discourse/components/upsert-category-support.gjs
index d0e7e99c767..b3bac7da5ae 100644
--- a/plugins/discourse-solved/assets/javascripts/discourse/components/upsert-category-support.gjs
+++ b/plugins/discourse-solved/assets/javascripts/discourse/components/upsert-category-support.gjs
@@ -1,62 +1,11 @@
-import Component from "@glimmer/component";
-import { eq } from "discourse/truth-helpers";
+import EditCategoryTypeSchemaFields from "discourse/components/edit-category-type-schema-fields";
-export default class UpsertCategorySupport extends Component {
- constructor() {
- super(...arguments);
- this.applyDefaults();
- }
+const UpsertCategorySupport = <template>
+ <EditCategoryTypeSchemaFields
+ @category={{@category}}
+ @categoryType="support"
+ @form={{@form}}
+ />
+</template>;
- get schema() {
- return this.args.category?.category_type_schema ?? [];
- }
-
- applyDefaults() {
- for (const entry of this.schema) {
- if (
- entry.default !== undefined &&
- this.args.transientData?.[entry.key] === undefined
- ) {
- this.args.form.set(entry.key, entry.default);
- }
- }
- }
-
- <template>
- <@form.Section
- class="edit-category-tab edit-category-tab-support
- {{if (eq @selectedTab 'support') 'active'}}"
- >
- {{#each this.schema as |entry|}}
- {{#if (eq entry.type "bool")}}
- <@form.Field
- @name={{entry.key}}
- @title={{entry.label}}
- @format="large"
- as |field|
- >
- <field.Checkbox />
- </@form.Field>
- {{else if (eq entry.type "integer")}}
- <@form.Field
- @name={{entry.key}}
- @title={{entry.label}}
- @format="large"
- as |field|
- >
- <field.Input @type="number" />
- </@form.Field>
- {{else}}
- <@form.Field
- @name={{entry.key}}
- @title={{entry.label}}
- @format="large"
- as |field|
- >
- <field.Input />
- </@form.Field>
- {{/if}}
- {{/each}}
- </@form.Section>
- </template>
-}
+export default UpsertCategorySupport;
diff --git a/plugins/discourse-solved/assets/javascripts/discourse/connectors/category-custom-settings/solved-settings-upsert.gjs b/plugins/discourse-solved/assets/javascripts/discourse/connectors/category-custom-settings/solved-settings-upsert.gjs
index dabba0231b7..ba9773b9e8d 100644
--- a/plugins/discourse-solved/assets/javascripts/discourse/connectors/category-custom-settings/solved-settings-upsert.gjs
+++ b/plugins/discourse-solved/assets/javascripts/discourse/connectors/category-custom-settings/solved-settings-upsert.gjs
@@ -5,7 +5,17 @@ import { i18n } from "discourse-i18n";
export default class SolvedSettingsUpsert extends Component {
static shouldRender(args, context) {
- return context.siteSettings.enable_simplified_category_creation;
+ // NOTE: There is a separate Support tab when the solved plugin
+ // is enabled for a category. In future, we may want to get rid
+ // of this component entirely and just rely on the schema defined
+ // in Categories::Types::Support.
+ return (
+ context.siteSettings.enable_simplified_category_creation &&
+ !(
+ context.siteSettings.enable_category_type_setup &&
+ args.category?.isType("support")
+ )
+ );
}
@service siteSettings;
diff --git a/plugins/discourse-solved/assets/javascripts/discourse/initializers/solved-category-type-tab.js b/plugins/discourse-solved/assets/javascripts/discourse/initializers/solved-category-type-tab.js
index e79b605fdc9..04130e1b107 100644
--- a/plugins/discourse-solved/assets/javascripts/discourse/initializers/solved-category-type-tab.js
+++ b/plugins/discourse-solved/assets/javascripts/discourse/initializers/solved-category-type-tab.js
@@ -11,12 +11,7 @@ export default {
name: "Support",
primary: true,
component: UpsertCategorySupport,
- condition: ({ category, siteSettings }) =>
- // TODO (martin) We only populate the category_type on category creation
- // at the moment, we need to do the same in the serializer on edit
- category.get("category_type") === "support" &&
- siteSettings.enable_category_type_setup &&
- siteSettings.enable_simplified_category_creation,
+ condition: ({ category }) => category.isType("support"),
});
});
},
diff --git a/plugins/discourse-solved/config/locales/server.en.yml b/plugins/discourse-solved/config/locales/server.en.yml
index ef391c01362..1bef167330c 100644
--- a/plugins/discourse-solved/config/locales/server.en.yml
+++ b/plugins/discourse-solved/config/locales/server.en.yml
@@ -5,10 +5,12 @@ en:
support:
name: "Support"
title: "support"
- description: "Ask questions and allow replies to be marked as solutions."
+ description: "Ask questions and allow replies to be marked as solutions"
discourse_solved:
category_type:
+ allow_accepted_answers:
+ label: "Allow topic owner and staff to mark a reply as the solution"
solved_topics_auto_close_hours:
label: "Auto close solved topics (hours)"
description: "Automatically close topic after this many hours once marked as solved. Set to 0 to disable."
diff --git a/plugins/discourse-solved/plugin.rb b/plugins/discourse-solved/plugin.rb
index 72e30271028..8d3223e4c9a 100644
--- a/plugins/discourse-solved/plugin.rb
+++ b/plugins/discourse-solved/plugin.rb
@@ -25,11 +25,10 @@ end
require_relative "lib/discourse_solved/engine"
after_initialize do
- register_category_type(DiscourseSolved::CategoryType)
-
SeedFu.fixture_paths << Rails.root.join("plugins", "discourse-solved", "db", "fixtures").to_s
reloadable_patch do
+ register_category_type(DiscourseSolved::Categories::Types::Support)
::Guardian.prepend(DiscourseSolved::GuardianExtensions)
::WebHook.prepend(DiscourseSolved::WebHookExtension)
::TopicViewSerializer.prepend(DiscourseSolved::TopicViewSerializerExtension)
diff --git a/plugins/discourse-solved/spec/models/discourse_solved/category_type_spec.rb b/plugins/discourse-solved/spec/models/discourse_solved/categories_types_support_spec.rb
similarity index 85%
rename from plugins/discourse-solved/spec/models/discourse_solved/category_type_spec.rb
rename to plugins/discourse-solved/spec/models/discourse_solved/categories_types_support_spec.rb
index f7f1cea1e47..ffecaf8e621 100644
--- a/plugins/discourse-solved/spec/models/discourse_solved/category_type_spec.rb
+++ b/plugins/discourse-solved/spec/models/discourse_solved/categories_types_support_spec.rb
@@ -2,7 +2,7 @@
require "rails_helper"
-RSpec.describe DiscourseSolved::CategoryType do
+RSpec.describe DiscourseSolved::Categories::Types::Support do
fab!(:category)
describe ".enable_plugin" do
@@ -15,6 +15,8 @@ RSpec.describe DiscourseSolved::CategoryType do
end
describe ".configure_site_settings" do
+ fab!(:admin)
+
before do
SiteSetting.show_filter_by_solved_status = false
SiteSetting.notify_on_staff_accept_solved = false
@@ -22,7 +24,7 @@ RSpec.describe DiscourseSolved::CategoryType do
end
it "enables sensible defaults for support categories" do
- described_class.configure_site_settings(category)
+ described_class.configure_site_settings(category, guardian: admin.guardian)
expect(SiteSetting.show_filter_by_solved_status).to eq(true)
expect(SiteSetting.notify_on_staff_accept_solved).to eq(true)
@@ -32,6 +34,7 @@ RSpec.describe DiscourseSolved::CategoryType do
it "uses provided configuration_values over defaults" do
described_class.configure_site_settings(
category,
+ guardian: admin.guardian,
configuration_values: {
"show_filter_by_solved_status" => false,
"empty_box_on_unsolved" => false,
@@ -57,13 +60,13 @@ RSpec.describe DiscourseSolved::CategoryType do
it "allows accepting answers on the category" do
expect(Guardian.new(admin).can_accept_answer?(topic, reply)).to eq(false)
- described_class.configure_category(category)
+ described_class.configure_category(category, guardian: admin.guardian)
expect(Guardian.new(admin).can_accept_answer?(topic, reply)).to eq(true)
end
it "sets default auto-close hours" do
- described_class.configure_category(category)
+ described_class.configure_category(category, guardian: admin.guardian)
expect(category.custom_fields["solved_topics_auto_close_hours"]).to eq("48")
end
@@ -71,6 +74,7 @@ RSpec.describe DiscourseSolved::CategoryType do
it "uses provided configuration_values for auto-close hours" do
described_class.configure_category(
category,
+ guardian: admin.guardian,
configuration_values: {
"solved_topics_auto_close_hours" => 72,
},
diff --git a/plugins/discourse-solved/spec/system/support_category_type_setup_spec.rb b/plugins/discourse-solved/spec/system/support_category_type_setup_spec.rb
new file mode 100644
index 00000000000..cb302a975c3
--- /dev/null
+++ b/plugins/discourse-solved/spec/system/support_category_type_setup_spec.rb
@@ -0,0 +1,38 @@
+# frozen_string_literal: true
+
+RSpec.describe "Support Category Type Setup", type: :system do
+ fab!(:admin)
+
+ let(:category_page) { PageObjects::Pages::Category.new }
+ let(:form) { PageObjects::Components::FormKit.new(".form-kit") }
+ let(:category_type_card) { PageObjects::Components::CategoryTypeCard.new }
+ let(:banner) { PageObjects::Components::AdminChangesBanner.new }
+
+ before do
+ SiteSetting.enable_simplified_category_creation = true
+ SiteSetting.enable_category_type_setup = true
+ sign_in(admin)
+ end
+
+ it "works with correct defaults and configures site settings and category custom field automatically" do
+ visit("/new-category/setup")
+ category_type_card.find_type_card("support").click
+ expect(page).to have_content(I18n.t("js.category.create_with_type", typeName: "support"))
+
+ # Preload basic data for this category type
+ expect(form.field("name").value).to eq("Support")
+ expect(
+ form.field("style_type").find(".form-kit__control-radio[type='radio'][value='emoji']")[
+ "checked"
+ ],
+ ).to eq(true)
+ expect(form.field("style_type").find("#control-emoji").find("img.emoji")["title"]).to eq(
+ "red_question_mark",
+ )
+
+ expect(banner).to be_visible
+ banner.click_save
+
+ expect(page).to have_content(I18n.t("js.category.edit_dialog_title", categoryName: "Support"))
+ end
+end
diff --git a/spec/requests/api/schemas/json/category_create_response.json b/spec/requests/api/schemas/json/category_create_response.json
index a5942396779..e8b48972ace 100644
--- a/spec/requests/api/schemas/json/category_create_response.json
+++ b/spec/requests/api/schemas/json/category_create_response.json
@@ -316,6 +316,9 @@
"string",
"null"
]
+ },
+ "category_types": {
+ "type": "object"
}
},
"required": [
diff --git a/spec/requests/api/schemas/json/category_update_response.json b/spec/requests/api/schemas/json/category_update_response.json
index 47ae892e111..069885b00a5 100644
--- a/spec/requests/api/schemas/json/category_update_response.json
+++ b/spec/requests/api/schemas/json/category_update_response.json
@@ -319,6 +319,9 @@
"string",
"null"
]
+ },
+ "category_types": {
+ "type": "object"
}
},
"required": [
diff --git a/spec/requests/categories_controller_spec.rb b/spec/requests/categories_controller_spec.rb
index 39aec135abd..584038303af 100644
--- a/spec/requests/categories_controller_spec.rb
+++ b/spec/requests/categories_controller_spec.rb
@@ -590,6 +590,65 @@ RSpec.describe CategoriesController do
expect(category.description).not_to include("&lt;script&gt;")
expect(category.description).to include("&lt;img")
end
+
+ describe "when category_type is provided" do
+ before do
+ SiteSetting.enable_category_type_setup = true
+ SiteSetting.enable_simplified_category_creation = true
+ end
+
+ it "creates a category with the category type" do
+ post "/categories.json", params: { name: "Test Category", category_type: "discussion" }
+
+ expect(response.status).to eq(200)
+ cat_json = response.parsed_body["category"]
+ expect(cat_json).to be_present
+ expect(cat_json["category_types"]).to eq(
+ {
+ "discussion" => {
+ "available" => true,
+ "configuration_schema" => {
+ },
+ "description" => I18n.t("category_types.discussion.description"),
+ "icon" => "memo",
+ "id" => "discussion",
+ "name" => I18n.t("category_types.discussion.name"),
+ "title" => "discussion",
+ },
+ },
+ )
+ end
+
+ it "can set site_settings for the category type when they match the schema" do
+ Categories::Types::Discussion.stubs(:configuration_schema).returns(
+ { site_settings: { max_category_nesting: 2 } },
+ )
+ post "/categories.json",
+ params: {
+ name: "Test Category",
+ category_type: "discussion",
+ category_type_site_settings: {
+ "max_category_nesting" => 3,
+ },
+ }
+
+ expect(response.status).to eq(200)
+ cat_json = response.parsed_body["category"]
+ expect(cat_json).to be_present
+ expect(SiteSetting.max_category_nesting).to eq(3)
+ end
+
+ it "will set the schema value for site settings when overrides are not provided" do
+ SiteSetting.max_category_nesting = 3
+ Categories::Types::Discussion.stubs(:configuration_schema).returns(
+ { site_settings: { max_category_nesting: 2 } },
+ )
+ post "/categories.json", params: { name: "Test Category", category_type: "discussion" }
+
+ expect(response.status).to eq(200)
+ expect(SiteSetting.max_category_nesting).to eq(2)
+ end
+ end
end
end
end
@@ -1061,6 +1120,16 @@ RSpec.describe CategoriesController do
expect(category.form_template_ids.count).to eq(0)
end
+ it "persists boolean false for custom fields" do
+ put "/categories/#{category.id}.json",
+ params: { custom_fields: { bool_field: false } }.to_json,
+ headers: {
+ "CONTENT_TYPE" => "application/json",
+ }
+ expect(response.status).to eq(200)
+ expect(category.reload.custom_fields).to have_key("bool_field")
+ end
+
it "doesn't set category moderation groups if the enable_category_group_moderation setting is false" do
SiteSetting.enable_category_group_moderation = false
@@ -1118,6 +1187,39 @@ RSpec.describe CategoriesController do
expect(category.reload.email_in).to eq("[email protected]")
expect(category.reload.minimum_required_tags).to eq(5)
end
+
+ context "when category_type_site_settings are provided" do
+ before do
+ SiteSetting.enable_category_type_setup = true
+ SiteSetting.enable_simplified_category_creation = true
+ end
+
+ it "can set site_settings for the category type when they match the schema" do
+ Categories::Types::Discussion.stubs(:configuration_schema).returns(
+ { site_settings: { max_category_nesting: 2 } },
+ )
+ put "/categories/#{category.id}.json",
+ params: {
+ category_type_site_settings: {
+ "max_category_nesting" => 3,
+ },
+ }
+
+ expect(response.status).to eq(200)
+ expect(SiteSetting.max_category_nesting).to eq(3)
+ end
+
+ it "does not set the schema value for site settings when overrides are not provided" do
+ SiteSetting.max_category_nesting = 3
+ Categories::Types::Discussion.stubs(:configuration_schema).returns(
+ { site_settings: { max_category_nesting: 2 } },
+ )
+ put "/categories/#{category.id}.json", params: {}
+
+ expect(response.status).to eq(200)
+ expect(SiteSetting.max_category_nesting).to eq(3)
+ end
+ end
end
end
@@ -1634,7 +1736,7 @@ RSpec.describe CategoriesController do
end
it "matches categories with accented names using unaccented search term" do
- accented_category = Fabricate(:category, name: "Éditions")
+ Fabricate(:category, name: "Éditions")
post "/categories/search.json", params: { term: "editions" }
diff --git a/spec/services/categories/types/base_spec.rb b/spec/services/categories/types/base_spec.rb
index 3b16f62e29e..e33ff612c30 100644
--- a/spec/services/categories/types/base_spec.rb
+++ b/spec/services/categories/types/base_spec.rb
@@ -1,6 +1,9 @@
# frozen_string_literal: true
RSpec.describe Categories::Types::Base do
+ fab!(:admin)
+ fab!(:category)
+
describe ".type_id" do
it "can be explicitly set" do
test_type = Class.new(described_class)
@@ -16,7 +19,8 @@ RSpec.describe Categories::Types::Base do
id: :base,
icon: "memo",
available: true,
- configuration_schema: [],
+ configuration_schema: {
+ },
)
end
end
@@ -32,7 +36,7 @@ RSpec.describe Categories::Types::Base do
end
end
- test_type.configure_site_settings(nil)
+ test_type.configure_site_settings(category, guardian: admin.guardian)
expect(SiteSetting.title).to eq("Configured Forum")
end
@@ -46,11 +50,128 @@ RSpec.describe Categories::Types::Base do
end
end
- test_type.configure_site_settings(nil, configuration_values: { "title" => "Override" })
+ test_type.configure_site_settings(
+ category,
+ guardian: admin.guardian,
+ configuration_values: {
+ "title" => "Override",
+ },
+ )
expect(SiteSetting.title).to eq("Override")
end
end
+ describe ".validate_schema!" do
+ it "accepts an empty hash" do
+ expect { described_class.validate_schema! }.not_to raise_error
+ end
+
+ it "accepts a valid schema with site_settings" do
+ test_type =
+ Class.new(described_class) do
+ def self.configuration_schema
+ { site_settings: { title: "My Forum" } }
+ end
+ end
+ expect { test_type.validate_schema! }.not_to raise_error
+ end
+
+ it "accepts a valid schema with category_custom_fields" do
+ test_type =
+ Class.new(described_class) do
+ def self.configuration_schema
+ {
+ category_custom_fields: {
+ my_field: {
+ default: 42,
+ type: :integer,
+ label: "My Field",
+ },
+ },
+ }
+ end
+ end
+ expect { test_type.validate_schema! }.not_to raise_error
+ end
+
+ it "accepts optional :description" do
+ test_type =
+ Class.new(described_class) do
+ def self.configuration_schema
+ {
+ category_custom_fields: {
+ my_field: {
+ default: nil,
+ type: :string,
+ label: "My Field",
+ description: "Details",
+ },
+ },
+ }
+ end
+ end
+ expect { test_type.validate_schema! }.not_to raise_error
+ end
+
+ it "accepts empty sub-hashes" do
+ test_type =
+ Class.new(described_class) do
+ def self.configuration_schema
+ { category_settings: {} }
+ end
+ end
+ expect { test_type.validate_schema! }.not_to raise_error
+ end
+
+ it "raises on unknown top-level keys" do
+ test_type =
+ Class.new(described_class) do
+ def self.configuration_schema
+ { unknown_key: {} }
+ end
+ end
+ expect { test_type.validate_schema! }.to raise_error(ArgumentError)
+ end
+
+ it "raises when site_settings references an unknown SiteSetting" do
+ test_type =
+ Class.new(described_class) do
+ def self.configuration_schema
+ { site_settings: { not_a_real_setting_xyzzy: true } }
+ end
+ end
+ expect { test_type.validate_schema! }.to raise_error(ArgumentError, /unknown SiteSetting/)
+ end
+
+ it "raises when a field config is missing :default" do
+ test_type =
+ Class.new(described_class) do
+ def self.configuration_schema
+ { category_custom_fields: { my_field: { type: :integer, label: "My Field" } } }
+ end
+ end
+ expect { test_type.validate_schema! }.to raise_error(ArgumentError)
+ end
+
+ it "raises when a field config :label is empty" do
+ test_type =
+ Class.new(described_class) do
+ def self.configuration_schema
+ { category_custom_fields: { my_field: { default: 1, type: :integer, label: "" } } }
+ end
+ end
+ expect { test_type.validate_schema! }.to raise_error(ArgumentError)
+ end
+ end
+
+ describe "all registered category types" do
+ it "have valid configuration schemas" do
+ Categories::TypeRegistry.all.each_value do |type_klass|
+ expect { type_klass.validate_schema! }.not_to raise_error
+ end
+ end
+ end
+
describe ".resolved_configuration_schema" do
it "resolves site setting metadata" do
test_type =
@@ -63,9 +184,11 @@ RSpec.describe Categories::Types::Base do
end
schema = test_type.send(:resolved_configuration_schema)
- expect(schema.length).to eq(1)
+ expect(schema.keys).to eq(
+ %i[general_category_settings site_settings category_settings category_custom_fields],
+ )
- entry = schema.first
+ entry = schema[:site_settings].first
expect(entry[:key]).to eq("title")
expect(entry[:default]).to eq("My Forum")
expect(entry[:type]).to be_present
@@ -83,13 +206,39 @@ RSpec.describe Categories::Types::Base do
end
schema = test_type.send(:resolved_configuration_schema)
- expect(schema.length).to eq(1)
+ expect(schema.keys).to eq(
+ %i[general_category_settings site_settings category_settings category_custom_fields],
+ )
- entry = schema.first
+ entry = schema[:category_settings].first
expect(entry[:key]).to eq("my_field")
expect(entry[:default]).to eq(42)
expect(entry[:type]).to eq("integer")
expect(entry[:label]).to eq("My Field")
end
+
+ it "returns a hash (not array) when configuration_schema is empty" do
+ schema = described_class.send(:resolved_configuration_schema)
+ expect(schema).to be_a(Hash)
+ end
+ end
+
+ describe "category_type_site_setting_names with empty schema" do
+ it "does not raise when a matched type has no configuration_schema" do
+ test_type =
+ Class.new(described_class) do
+ type_id :no_schema_test
+ def self.category_matches?(_category)
+ true
+ end
+ end
+
+ Categories::TypeRegistry.register(test_type, plugin_identifier: "test")
+ SiteSetting.enable_simplified_category_creation = true
+
+ expect { category.category_type_site_setting_names }.not_to raise_error
+ ensure
+ Categories::TypeRegistry.all.delete(:no_schema_test)
+ end
end
end
diff --git a/spec/services/site_setting/update_spec.rb b/spec/services/site_setting/update_spec.rb
index f9e098e4a51..bfcb3123742 100644
--- a/spec/services/site_setting/update_spec.rb
+++ b/spec/services/site_setting/update_spec.rb
@@ -1,7 +1,7 @@
# frozen_string_literal: true
RSpec.describe SiteSetting::Update do
- describe described_class::Contract, type: :model do
+ describe SiteSetting::Update::Contract, type: :model do
it { is_expected.to validate_presence_of :settings }
end
@@ -31,6 +31,28 @@ RSpec.describe SiteSetting::Update do
it { is_expected.to fail_a_policy(:current_user_is_admin) }
end
+ context "when changing a setting where the value already matches the current value" do
+ let(:setting_name) { :title }
+ let(:new_value) { SiteSetting.title }
+
+ it { is_expected.to run_successfully }
+
+ it "does not create a staff action log" do
+ expect { result }.to not_change { UserHistory.count }
+ end
+
+ context "when the site setting is a boolean and the new value has not been coerced to a boolean" do
+ let(:setting_name) { :allow_user_locale }
+ let(:new_value) { "false" }
+
+ before { SiteSetting.allow_user_locale = false }
+
+ it "does not create a staff action log" do
+ expect { result }.to not_change { UserHistory.count }
+ end
+ end
+ end
+
context "when trying to change a deprecated setting" do
let(:hard_deprecated_setting) { ["suggested_topics", "new_suggested_topics", false, "3.3"] }
diff --git a/spec/system/page_objects/components/category_type_card.rb b/spec/system/page_objects/components/category_type_card.rb
new file mode 100644
index 00000000000..f3fc86fb638
--- /dev/null
+++ b/spec/system/page_objects/components/category_type_card.rb
@@ -0,0 +1,13 @@
+# frozen_string_literal: true
+
+module PageObjects
+ module Components
+ class CategoryTypeCard < PageObjects::Components::Base
+ CATEGORY_TYPE_CARD_SELECTOR = ".category-type-cards__card"
+
+ def find_type_card(type_id)
+ find("#{CATEGORY_TYPE_CARD_SELECTOR}.--category-type-#{type_id}")
+ end
+ end
+ end
+end