Skip to content

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

FIX: exclude experimental system theme palettes (#33267)

Krzysztof Kotlarekbbf7a379022d
diff --git a/app/controllers/admin/color_schemes_controller.rb b/app/controllers/admin/color_schemes_controller.rb
index 040c937c45b..64059fa0d95 100644
--- a/app/controllers/admin/color_schemes_controller.rb
+++ b/app/controllers/admin/color_schemes_controller.rb
@@ -4,7 +4,10 @@ class Admin::ColorSchemesController < Admin::AdminController
before_action :fetch_color_scheme, only: %i[update destroy]
def index
- schemes = ColorScheme.without_theme_owned_palettes.order("id ASC")
+ schemes =
+ ColorScheme.without_theme_owned_palettes.with_experimental_system_theme_palettes.order(
+ "color_schemes.id ASC",
+ )
schemes = schemes.where(theme_id: nil) if params[:exclude_theme_owned]
diff --git a/app/controllers/admin/config/customize_controller.rb b/app/controllers/admin/config/customize_controller.rb
index 76736bc28a9..d8ceffeade6 100644
--- a/app/controllers/admin/config/customize_controller.rb
+++ b/app/controllers/admin/config/customize_controller.rb
@@ -8,8 +8,8 @@ class Admin::Config::CustomizeController < Admin::AdminController
Theme
.include_basic_relations
.includes(:theme_fields, color_scheme: [:color_scheme_colors])
+ .with_experimental_system_themes
.where(component: false)
- .where("ID > 0 OR LOWER(name) IN (?)", SiteSetting.experimental_system_themes_map)
.order(:name)
render json: { themes: serialize_data(themes, ThemeIndexSerializer) }
diff --git a/app/controllers/admin/themes_controller.rb b/app/controllers/admin/themes_controller.rb
index 9c43b79a613..b4a1830ee3b 100644
--- a/app/controllers/admin/themes_controller.rb
+++ b/app/controllers/admin/themes_controller.rb
@@ -169,13 +169,14 @@ class Admin::ThemesController < Admin::AdminController
end
def index
- @themes = Theme.strict_loading.include_relations.order(:name)
+ @themes = Theme.with_experimental_system_themes.strict_loading.include_relations.order(:name)
@color_schemes =
ColorScheme
.strict_loading
.all
.without_theme_owned_palettes
+ .with_experimental_system_theme_palettes
.includes(:theme, color_scheme_colors: :color_scheme)
.to_a
diff --git a/app/models/color_scheme.rb b/app/models/color_scheme.rb
index 3f4a384c61e..d01282deb3b 100644
--- a/app/models/color_scheme.rb
+++ b/app/models/color_scheme.rb
@@ -318,6 +318,14 @@ class ColorScheme < ActiveRecord::Base
scope :without_theme_owned_palettes,
-> { where("color_schemes.id NOT IN (SELECT color_scheme_id FROM theme_color_schemes)") }
+ scope :with_experimental_system_theme_palettes,
+ -> do
+ joins("LEFT JOIN themes on themes.id = color_schemes.theme_id").where(
+ "themes.id is NULL OR themes.id > 0 OR themes.id IN (?)",
+ Theme.experimental_system_theme_ids,
+ )
+ end
+
validates_associated :color_scheme_colors
BASE_COLORS_FILE = "#{Rails.root}/app/assets/stylesheets/common/foundation/colors.scss"
diff --git a/app/models/theme.rb b/app/models/theme.rb
index fea0af41a2a..e5d0cf9dbf1 100644
--- a/app/models/theme.rb
+++ b/app/models/theme.rb
@@ -122,6 +122,8 @@ class Theme < ActiveRecord::Base
scope :not_system, -> { where("id > 0") }
scope :system, -> { where("id < 0") }
+ scope :with_experimental_system_themes,
+ -> { where("id > 0 OR id IN (?)", Theme.experimental_system_theme_ids) }
delegate :remote_url, to: :remote_theme, private: true, allow_nil: true
@@ -340,6 +342,12 @@ class Theme < ActiveRecord::Base
end
end
+ def self.experimental_system_theme_ids
+ Theme::CORE_THEMES
+ .select { |k, v| SiteSetting.experimental_system_themes_map.include?(k) }
+ .values
+ end
+
def set_default!
if component
raise Discourse::InvalidParameters.new(I18n.t("themes.errors.component_no_default"))
diff --git a/spec/requests/admin/color_schemes_controller_spec.rb b/spec/requests/admin/color_schemes_controller_spec.rb
index da402aebb10..02d61ab28f6 100644
--- a/spec/requests/admin/color_schemes_controller_spec.rb
+++ b/spec/requests/admin/color_schemes_controller_spec.rb
@@ -32,6 +32,19 @@ RSpec.describe Admin::ColorSchemesController do
expect(scheme_colors[0]["hex"]).to eq(base_scheme_colors[0].hex)
end
+ it "filters colors belonging to experimental system themes" do
+ get "/admin/color_schemes.json"
+ expect(response.status).to eq(200)
+ scheme_names = response.parsed_body.map { |scheme| scheme["name"] }
+ expect(scheme_names).not_to include("Horizon")
+
+ SiteSetting.experimental_system_themes = "horizon"
+ get "/admin/color_schemes.json"
+ expect(response.status).to eq(200)
+ scheme_names = response.parsed_body.map { |scheme| scheme["name"] }
+ expect(scheme_names).to include("Horizon")
+ end
+
it "serializes default colors even when not present in database" do
scheme = ColorScheme.create_from_base({ name: "my color scheme" })
scheme.colors.find_by(name: "primary").destroy!
diff --git a/spec/requests/admin/themes_controller_spec.rb b/spec/requests/admin/themes_controller_spec.rb
index 7c2da398e66..95aecc6ef62 100644
--- a/spec/requests/admin/themes_controller_spec.rb
+++ b/spec/requests/admin/themes_controller_spec.rb
@@ -477,6 +477,25 @@ RSpec.describe Admin::ThemesController do
expect(theme_json["remote_theme"]["remote_version"]).to eq("7")
end
+ it "filters experimental system themes" do
+ get "/admin/themes.json"
+ expect(response.status).to eq(200)
+ theme_names = response.parsed_body["themes"].map { |theme| theme[:name] }
+ color_scheme_names =
+ response.parsed_body["extras"]["color_schemes"].map { |scheme| scheme[:name] }
+ expect(theme_names).not_to include("Horizon")
+ expect(color_scheme_names).not_to include("Horizon")
+
+ SiteSetting.experimental_system_themes = "horizon"
+ get "/admin/themes.json"
+ expect(response.status).to eq(200)
+ theme_names = response.parsed_body["themes"].map { |t| t[:name] }
+ color_scheme_names =
+ response.parsed_body["extras"]["color_schemes"].map { |scheme| scheme[:name] }
+ expect(theme_names).to include("Horizon")
+ expect(color_scheme_names).to include("Horizon")
+ end
+
it "does not result in N+1 queries" do
# warmup
get "/admin/themes.json"