0
0
Fork 0
mirror of https://github.com/discourse/discourse.git synced 2026-08-06 10:40:48 +08:00
discourse/app/models/category_scope_site_setting.rb
Natalie Tay 254750692d
DEV: Introduce a category scope enum for flexible category selection (#41505)
This PR adds a shared `CategoryScopeSiteSetting` enum for category-scope
dropdowns.

The enum keeps the stored values short (`all`, `public`, `include`,
`include_strict`, `exclude`, `exclude_strict`) while letting the admin
UI show clearer labels like “Include selected categories (with
subcategories)”. This gives category-list settings a reusable way to
explain include/exclude and strict/non-strict behavior without making
the setting description do all the work.

Example usage:

```yaml
feature_name_category_scope:
    type: enum
    default: "public" # public categories
    enum: "CategoryScopeSiteSetting"
    client: false
    area: "feature-name"
feature_name_categories:
    type: category_list
    default: ""
    client: false
    area: "feature-name"
    depends_on:
      - feature_name_category_scope
    depends_on_values:
      feature_name_category_scope:
        - include
        - include_strict
        - exclude
        - exclude_strict
    depends_behavior: "hidden" # this settting will be hidden based on the depends_on_values
    dependent_setting_display: "inline" # this setting will be displayed inline to the above
```

And suitable descriptions in `server.en.yml`

```yaml
feature_name_category_scope: "Choose which categories this feature can use. Include and exclude options use {{setting:feature_name_categories}}."   
feature_name_categories: "Categories used when the category scope is set to <strong>include</strong> or <strong>exclude</strong>."
```

https://github.com/user-attachments/assets/d5255687-e995-4123-951b-2c784b315c0b
2026-07-08 15:54:10 +08:00

24 lines
668 B
Ruby
Vendored

# frozen_string_literal: true
require "enum_site_setting"
class CategoryScopeSiteSetting < EnumSiteSetting
def self.valid_value?(val)
values.any? { |value| value[:value] == val }
end
def self.values
@values ||= [
{ name: "category_scope.all", value: "all" },
{ name: "category_scope.public", value: "public" },
{ name: "category_scope.include", value: "include" },
{ name: "category_scope.include_strict", value: "include_strict" },
{ name: "category_scope.exclude", value: "exclude" },
{ name: "category_scope.exclude_strict", value: "exclude_strict" },
]
end
def self.translate_names?
true
end
end