Skip to content

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

DEV: Use themeable site settings for Horizon (#33645)

Martin Brennan57fea290ef15
diff --git a/app/assets/javascripts/discourse/app/components/welcome-banner.gjs b/app/assets/javascripts/discourse/app/components/welcome-banner.gjs
index bca739978d4..78c0b303a9d 100644
--- a/app/assets/javascripts/discourse/app/components/welcome-banner.gjs
+++ b/app/assets/javascripts/discourse/app/components/welcome-banner.gjs
@@ -9,7 +9,6 @@ import SearchMenu from "discourse/components/search-menu";
import bodyClass from "discourse/helpers/body-class";
import concatClass from "discourse/helpers/concat-class";
import { prioritizeNameFallback } from "discourse/lib/settings";
-import { applyValueTransformer } from "discourse/lib/transformer";
import I18n, { i18n } from "discourse-i18n";
export default class WelcomeBanner extends Component {
@@ -80,16 +79,7 @@ export default class WelcomeBanner extends Component {
}
get shouldDisplay() {
- const enabled = applyValueTransformer(
- "site-setting-enable-welcome-banner",
- this.siteSettings.enable_welcome_banner
- );
-
- if (!enabled) {
- return false;
- }
-
- return this.displayForRoute;
+ return this.siteSettings.enable_welcome_banner && this.displayForRoute;
}
get bodyClasses() {
diff --git a/app/assets/javascripts/discourse/app/lib/transformer/registry.js b/app/assets/javascripts/discourse/app/lib/transformer/registry.js
index 7e461464460..1ccd9fe708a 100644
--- a/app/assets/javascripts/discourse/app/lib/transformer/registry.js
+++ b/app/assets/javascripts/discourse/app/lib/transformer/registry.js
@@ -61,8 +61,6 @@ export const VALUE_TRANSFORMERS = Object.freeze([
"post-small-action-custom-component",
"post-small-action-icon",
"poster-name-class",
- "site-setting-enable-welcome-banner",
- "site-setting-search-experience",
"small-user-attrs",
"tag-separator",
"topic-list-class",
diff --git a/app/assets/javascripts/discourse/app/services/search.js b/app/assets/javascripts/discourse/app/services/search.js
index 7a0261e6099..0e8b90c8880 100644
--- a/app/assets/javascripts/discourse/app/services/search.js
+++ b/app/assets/javascripts/discourse/app/services/search.js
@@ -2,7 +2,6 @@ import { tracked } from "@glimmer/tracking";
import { action } from "@ember/object";
import Service, { service } from "@ember/service";
import { disableImplicitInjections } from "discourse/lib/implicit-injections";
-import { applyValueTransformer } from "discourse/lib/transformer";
@disableImplicitInjections
export default class Search extends Service {
@@ -32,10 +31,7 @@ export default class Search extends Service {
}
get searchExperience() {
- return applyValueTransformer(
- "site-setting-search-experience",
- this.siteSettings.search_experience
- );
+ return this.siteSettings.search_experience;
}
focusSearchInput() {
diff --git a/app/assets/javascripts/discourse/tests/integration/components/header-contents-test.gjs b/app/assets/javascripts/discourse/tests/integration/components/header-contents-test.gjs
index af964333f4a..e89d1051c54 100644
--- a/app/assets/javascripts/discourse/tests/integration/components/header-contents-test.gjs
+++ b/app/assets/javascripts/discourse/tests/integration/components/header-contents-test.gjs
@@ -3,7 +3,6 @@ import { render } from "@ember/test-helpers";
import { module, test } from "qunit";
import sinon from "sinon";
import Contents from "discourse/components/header/contents";
-import { withPluginApi } from "discourse/lib/plugin-api";
import { setupRenderingTest } from "discourse/tests/helpers/component-test";
module("Integration | Component | Header | Contents", function (hooks) {
@@ -28,29 +27,5 @@ module("Integration | Component | Header | Contents", function (hooks) {
assert
.dom(".floating-search-input-wrapper")
.doesNotExist("it does not display when the site is in mobile view");
-
- sinon.stub(site, "mobileView").value(false);
-
- withPluginApi("1.37.1", (api) => {
- api.registerValueTransformer("site-setting-search-experience", () => {
- return "search_icon";
- });
- });
-
- await render(
- <template>
- <Contents
- @sidebarEnabled={{true}}
- @toggleNavigationMenu={{toggleNavigationMenu}}
- @showSidebar={{true}}
- >test</Contents>
- </template>
- );
-
- assert
- .dom(".floating-search-input-wrapper")
- .doesNotExist(
- "it does not display when the value transformer is not search_field"
- );
});
});
diff --git a/app/assets/javascripts/discourse/tests/integration/components/header-icons-test.gjs b/app/assets/javascripts/discourse/tests/integration/components/header-icons-test.gjs
index 2946a43e15b..acc11ec1d19 100644
--- a/app/assets/javascripts/discourse/tests/integration/components/header-icons-test.gjs
+++ b/app/assets/javascripts/discourse/tests/integration/components/header-icons-test.gjs
@@ -4,7 +4,6 @@ import { module, test } from "qunit";
import sinon from "sinon";
import { SEARCH_BUTTON_ID } from "discourse/components/header";
import Icons from "discourse/components/header/icons";
-import { withPluginApi } from "discourse/lib/plugin-api";
import { setupRenderingTest } from "discourse/tests/helpers/component-test";
module("Integration | Component | Header | Icons", function (hooks) {
@@ -73,32 +72,5 @@ module("Integration | Component | Header | Icons", function (hooks) {
.exists(
"it does display when the site is in mobile view even if search_experience setting is search_field"
);
-
- sinon.stub(site, "mobileView").value(false);
- this.siteSettings.search_experience = "search_icon";
-
- withPluginApi("1.37.1", (api) => {
- api.registerValueTransformer("site-setting-search-experience", () => {
- return "search_field";
- });
- });
-
- await render(
- <template>
- <Icons
- @sidebarEnabled={{true}}
- @toggleSearchMenu={{noop}}
- @toggleNavigationMenu={{noop}}
- @toggleUserMenu={{noop}}
- @searchButtonId={{SEARCH_BUTTON_ID}}
- />
- </template>
- );
-
- assert
- .dom(".search-dropdown")
- .doesNotExist(
- "it does not display when the value transformer is not search_icon"
- );
});
});
diff --git a/app/serializers/theme_serializer.rb b/app/serializers/theme_serializer.rb
index 9587a94b9d7..985f1a161db 100644
--- a/app/serializers/theme_serializer.rb
+++ b/app/serializers/theme_serializer.rb
@@ -78,7 +78,12 @@ class ThemeSerializer < BasicThemeSerializer
# Components always return an empty array here
def themeable_site_settings
- object.themeable_site_settings
+ # UI for editing settings always expects the value + default to be a string
+ # to compare whether the setting has been changed or not.
+ object.themeable_site_settings.each do |tss|
+ tss[:default] = tss[:default].to_s
+ tss[:value] = tss[:value].to_s
+ end
end
def include_themeable_site_settings?
diff --git a/db/migrate/20250724012518_drop_horizon_setting_field.rb b/db/migrate/20250724012518_drop_horizon_setting_field.rb
new file mode 100644
index 00000000000..cbb8325e104
--- /dev/null
+++ b/db/migrate/20250724012518_drop_horizon_setting_field.rb
@@ -0,0 +1,27 @@
+# frozen_string_literal: true
+class DropHorizonSettingField < ActiveRecord::Migration[8.0]
+ def up
+ old_setting_field_id = DB.query_single(<<~SQL).first
+ SELECT id FROM theme_fields
+ WHERE theme_id = -2 AND name = 'yaml' AND target_id = 3
+ SQL
+
+ return if old_setting_field_id.nil?
+
+ # target_id 3 is Theme.targets[:settings]
+ # ID -2 is always the Horizon theme since it's a core theme
+ DB.exec(<<~SQL)
+ DELETE FROM theme_fields
+ WHERE id = #{old_setting_field_id}
+ SQL
+
+ DB.exec(<<~SQL)
+ DELETE FROM javascript_caches
+ WHERE theme_field_id = #{old_setting_field_id}
+ SQL
+ end
+
+ def down
+ raise ActiveRecord::IrreversibleMigration
+ end
+end
diff --git a/lib/system_themes_manager.rb b/lib/system_themes_manager.rb
index 1a09cf823ba..881c6ff1933 100644
--- a/lib/system_themes_manager.rb
+++ b/lib/system_themes_manager.rb
@@ -29,4 +29,17 @@ class SystemThemesManager
remote_theme.update_column(:enabled, true)
Stylesheet::Manager.clear_theme_cache!
end
+
+ # Don't want user history created from theme site setting changes
+ # from system themes polluting specs.
+ def self.clear_system_theme_user_history!
+ return if !Rails.env.test?
+
+ Theme::CORE_THEMES.each_key do |theme_name|
+ UserHistory
+ .where(action: UserHistory.actions[:change_theme_site_setting])
+ .where("subject ILIKE :theme_name", theme_name: "#{theme_name}:%")
+ .destroy_all
+ end
+ end
end
diff --git a/spec/rails_helper.rb b/spec/rails_helper.rb
index b79bdc5d4ac..eaf0837cd2e 100644
--- a/spec/rails_helper.rb
+++ b/spec/rails_helper.rb
@@ -332,6 +332,7 @@ RSpec.configure do |config|
Discourse.current_user_provider = TestCurrentUserProvider
Discourse::Application.load_tasks
+ SystemThemesManager.clear_system_theme_user_history!
ThemeField.delete_all
JavascriptCache.delete_all
ThemeSiteSetting.delete_all
diff --git a/spec/serializers/theme_serializer_spec.rb b/spec/serializers/theme_serializer_spec.rb
index c3f1e7330a4..18320274f80 100644
--- a/spec/serializers/theme_serializer_spec.rb
+++ b/spec/serializers/theme_serializer_spec.rb
@@ -1,7 +1,7 @@
# frozen_string_literal: true
RSpec.describe ThemeSerializer do
- describe "load theme settings" do
+ describe ".settings" do
fab!(:theme)
it "should add error message when settings format is invalid" do
@@ -64,4 +64,34 @@ RSpec.describe ThemeSerializer do
expect(serialized[:screenshot_url]).to eq(first_upload.url)
end
end
+
+ describe ".themeable_site_settings" do
+ fab!(:theme)
+
+ it "returns the themeable_site_settings with defaults and values converted to string" do
+ serialized = ThemeSerializer.new(theme).as_json[:theme]
+
+ SiteSetting.themeable_site_settings.each do |setting|
+ found_tss = serialized[:themeable_site_settings].find { |tss| tss[:setting] == setting }
+ expect(found_tss[:value]).to eq(SiteSetting.defaults[setting.name].to_s)
+ expect(found_tss[:default]).to eq(SiteSetting.defaults[setting.name].to_s)
+ end
+ end
+
+ it "returns the correct value for overridden theme site settings" do
+ Fabricate(
+ :theme_site_setting_with_service,
+ name: "enable_welcome_banner",
+ value: false,
+ theme: theme,
+ )
+
+ serialized = ThemeSerializer.new(theme).as_json[:theme]
+
+ found_tss =
+ serialized[:themeable_site_settings].find { |tss| tss[:setting] == :enable_welcome_banner }
+ expect(found_tss[:value]).to eq("false")
+ expect(found_tss[:default]).to eq(SiteSetting.defaults[:enable_welcome_banner].to_s)
+ end
+ end
end
diff --git a/themes/horizon/about.json b/themes/horizon/about.json
index d24a5992ded..a78c7ba79a2 100644
--- a/themes/horizon/about.json
+++ b/themes/horizon/about.json
@@ -131,5 +131,9 @@
"hover": "584B3E"
}
},
- "screenshots": ["screenshots/light.png", "screenshots/dark.png"]
+ "screenshots": ["screenshots/light.png", "screenshots/dark.png"],
+ "theme_site_settings": {
+ "enable_welcome_banner": true,
+ "search_experience": "search_field"
+ }
}
diff --git a/themes/horizon/javascripts/discourse/api-initializers/horizon.gjs b/themes/horizon/javascripts/discourse/api-initializers/horizon.gjs
index 36342d80b08..20db3c600a8 100644
--- a/themes/horizon/javascripts/discourse/api-initializers/horizon.gjs
+++ b/themes/horizon/javascripts/discourse/api-initializers/horizon.gjs
@@ -5,12 +5,4 @@ import UserColorPaletteSelector from "../components/user-color-palette-selector"
export default apiInitializer("1.8.0", (api) => {
api.renderInOutlet("above-main-container", ExperimentalScreen);
api.renderInOutlet("sidebar-footer-actions", UserColorPaletteSelector);
-
- api.registerValueTransformer("site-setting-enable-welcome-banner", () => {
- return settings.enable_welcome_banner;
- });
-
- api.registerValueTransformer("site-setting-search-experience", () => {
- return settings.search_experience;
- });
});
diff --git a/themes/horizon/settings.yml b/themes/horizon/settings.yml
deleted file mode 100644
index 343fd3bb0e9..00000000000
--- a/themes/horizon/settings.yml
+++ /dev/null
@@ -1,11 +0,0 @@
-enable_welcome_banner:
- default: true
- description: "Overrides the core `enable welcome banner` site setting"
-
-search_experience:
- type: enum
- default: search_field
- choices:
- - search_field
- - search_icon
- description: "Overrides the core `search experience` site setting"