discourse/lib/system_themes_manager.rb
David Taylor 85cd05a520
DEV: Improve system theme loading (#34954)
Use `set_field` and `theme.save!` when updating the theme. This ensures
all the correct caches are rebuilt, just like when you change a file via
the admin UI. It also sends more precise refresh commands to the browser
so that CSS-only changes apply live, instead of requiring a full page
refresh.
2025-09-24 17:56:41 +01:00

42 lines
1.2 KiB
Ruby

# frozen_string_literal: true
class SystemThemesManager
def self.sync!
Theme::CORE_THEMES.keys.each { |theme_name| sync_theme!(theme_name) }
end
def self.sync_theme!(theme_name)
theme_id = Theme::CORE_THEMES[theme_name]
raise Discourse::InvalidParameters unless theme_id
theme_dir = "#{Rails.root}/themes/#{theme_name}"
is_initial_install = !Theme.exists?(id: theme_id)
theme = RemoteTheme.import_theme_from_directory(theme_dir, theme_id: theme_id)
theme.update_column(:enabled, true)
if is_initial_install
if theme_id == Theme::CORE_THEMES["horizon"]
theme.update!(dark_color_scheme: theme.color_schemes.find_by(name: "Horizon Dark"))
end
end
theme.save!
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