2
0
Fork 0
mirror of https://github.com/discourse/discourse.git synced 2026-03-03 23:54:20 +08:00
discourse/spec/lib/theme_site_setting_resolver_spec.rb
Martin Brennan 19af83d39e
FEATURE: Themeable site settings (#32233)
This commit introduces the concept of themeable site settings,
which is a new tool for theme authors that lives alongside theme
modifiers and theme settings. Here is a quick summary:

* Theme settings - These are custom settings used to control UI and functionality within your theme or component and provide configuration options. These cannot change core Discourse functionality.
* Theme modifiers - Allows a theme or a component to modify selected server-side functionality of core Discourse as an alternative to building a plugin.
* Themeable site settings (new) - Allows a theme (not components) to override a small subset of core site settings, which generally control parts of the UI and other minor functionality. This allows themes to have a greater control over the full site experience.

Themeable site settings will be shown for all themes, whether the theme
changes
the value or not, and have a similar UI to custom theme settings.

We are also introducing a new page at
`/admin/config/theme-site-settings` that
allows admins to see all possible themeable site settings, and which
themes
are changing the value from the default.

### Configuration

Theme authors can configure initial values themeable site settings using
a section in the `about.json` file like so:

```json
"theme_site_settings": {
  "search_experience": "search_field"
}
```

These values will not change when the theme updates, because we cannot
know if admins have manually changed them.

### Limitations

Themeable site settings are only really intended to control elements of
the UI, and when retrieving their value we require a theme ID, so these
limitations apply:

- Themeable site settings cannot be used in Sidekiq jobs
- Themeable site settings cannot be used in markdown rules
- Themeable site settings will be cached separately to client site
settings using theme ID as a key
- Themeable site settings will override keys on the `siteSettings`
service on the client using the application preloader
- `SiteSetting.client_settings_json` will not include themeable site
settings, instead you can call `SiteSetting.theme_site_settings_json`
with a theme ID

### Initial settings

There are only two site settings that will be themeable to begin with:

* `enable_welcome_banner`
* `search_experience`

And our new Horizon theme will take advantage of both. Over time, more
settings that control elements of the UI will be exposed this way.
2025-07-16 11:00:21 +10:00

71 lines
2.4 KiB
Ruby

# frozen_string_literal: true
RSpec.describe ThemeSiteSettingResolver do
fab!(:theme)
subject(:resolver) { described_class.new(theme: theme) }
describe "#resolved_theme_site_settings" do
let(:themeable_setting) { :enable_welcome_banner }
let(:default_value) { SiteSetting.defaults[themeable_setting] }
it "returns themeable settings" do
result = resolver.resolved_theme_site_settings
expect(result.map { |s| s[:setting] }).to match_array(SiteSetting.themeable_site_settings)
end
it "returns settings in alphabetical order" do
result = resolver.resolved_theme_site_settings
expect(result.map { |r| r[:setting] }).to eq(SiteSetting.themeable_site_settings.sort)
end
it "includes metadata for each setting" do
result = resolver.resolved_theme_site_settings.first
expect(result).to include(
setting: themeable_setting,
default: default_value,
description: I18n.t("site_settings.#{themeable_setting}"),
type: "bool",
)
end
context "when theme has not overridden any settings" do
it "uses the default site setting value" do
result = resolver.resolved_theme_site_settings.find { |s| s[:setting] == themeable_setting }
expect(result[:value]).to eq(default_value)
expect(result[:default]).to eq(default_value)
end
end
context "when theme has overridden settings" do
let(:overridden_value) { false }
before do
# Create a theme site setting override with a different value than default
Fabricate(
:theme_site_setting,
theme: theme,
name: themeable_setting.to_s,
value: overridden_value,
data_type: SiteSetting.types[:enum],
)
end
it "uses the overridden value" do
result = resolver.resolved_theme_site_settings.find { |s| s[:setting] == themeable_setting }
expect(result[:value]).to eq(overridden_value)
expect(result[:default]).to eq(default_value)
end
end
context "with enum type settings" do
let(:themeable_setting) { :search_experience }
it "includes valid_values and translate_names" do
result = resolver.resolved_theme_site_settings.find { |s| s[:setting] == themeable_setting }
expect(result[:valid_values]).to be_an(Array)
expect(result).to include(:translate_names)
end
end
end
end