mirror of
https://github.com/discourse/discourse.git
synced 2025-02-25 18:55:32 -06:00
PERF: Eradicate N+1 queries from the theme admin page
This commit is contained in:
parent
2909e7fbdf
commit
7feabd9e49
@ -96,8 +96,15 @@ class Admin::ThemesController < Admin::AdminController
|
|||||||
end
|
end
|
||||||
|
|
||||||
def index
|
def index
|
||||||
@themes = Theme.order(:name).includes(:theme_fields, :remote_theme)
|
@themes = Theme.order(:name).includes(:child_themes,
|
||||||
@color_schemes = ColorScheme.all.to_a
|
:remote_theme,
|
||||||
|
:theme_settings,
|
||||||
|
:settings_field,
|
||||||
|
:user,
|
||||||
|
:color_scheme,
|
||||||
|
theme_fields: :upload
|
||||||
|
)
|
||||||
|
@color_schemes = ColorScheme.all.includes(:theme, color_scheme_colors: :color_scheme).to_a
|
||||||
light = ColorScheme.new(name: I18n.t("color_schemes.light"))
|
light = ColorScheme.new(name: I18n.t("color_schemes.light"))
|
||||||
@color_schemes.unshift(light)
|
@color_schemes.unshift(light)
|
||||||
|
|
||||||
|
@ -16,10 +16,12 @@ class Theme < ActiveRecord::Base
|
|||||||
has_many :theme_fields, dependent: :destroy
|
has_many :theme_fields, dependent: :destroy
|
||||||
has_many :theme_settings, dependent: :destroy
|
has_many :theme_settings, dependent: :destroy
|
||||||
has_many :child_theme_relation, class_name: 'ChildTheme', foreign_key: 'parent_theme_id', dependent: :destroy
|
has_many :child_theme_relation, class_name: 'ChildTheme', foreign_key: 'parent_theme_id', dependent: :destroy
|
||||||
has_many :child_themes, through: :child_theme_relation, source: :child_theme
|
has_many :child_themes, -> { order(:name) }, through: :child_theme_relation, source: :child_theme
|
||||||
has_many :color_schemes
|
has_many :color_schemes
|
||||||
belongs_to :remote_theme
|
belongs_to :remote_theme
|
||||||
|
|
||||||
|
has_one :settings_field, -> { where(target_id: Theme.targets[:settings], name: "yaml") }, class_name: 'ThemeField'
|
||||||
|
|
||||||
validate :component_validations
|
validate :component_validations
|
||||||
|
|
||||||
scope :user_selectable, ->() {
|
scope :user_selectable, ->() {
|
||||||
@ -346,7 +348,7 @@ class Theme < ActiveRecord::Base
|
|||||||
end
|
end
|
||||||
|
|
||||||
def settings
|
def settings
|
||||||
field = theme_fields.where(target_id: Theme.targets[:settings], name: "yaml").first
|
field = settings_field
|
||||||
return [] unless field && field.error.nil?
|
return [] unless field && field.error.nil?
|
||||||
|
|
||||||
settings = []
|
settings = []
|
||||||
|
@ -9,6 +9,7 @@ class ThemeSetting < ActiveRecord::Base
|
|||||||
theme.clear_cached_settings!
|
theme.clear_cached_settings!
|
||||||
theme.remove_from_cache!
|
theme.remove_from_cache!
|
||||||
theme.theme_fields.update_all(value_baked: nil)
|
theme.theme_fields.update_all(value_baked: nil)
|
||||||
|
theme.theme_settings.reload
|
||||||
SvgSprite.expire_cache if self.name.to_s.include?("_icon")
|
SvgSprite.expire_cache if self.name.to_s.include?("_icon")
|
||||||
CSP::Extension.clear_theme_extensions_cache! if name.to_s == CSP::Extension::THEME_SETTING
|
CSP::Extension.clear_theme_extensions_cache! if name.to_s == CSP::Extension::THEME_SETTING
|
||||||
end
|
end
|
||||||
|
@ -75,7 +75,7 @@ class ThemeSerializer < ChildThemeSerializer
|
|||||||
end
|
end
|
||||||
|
|
||||||
def child_themes
|
def child_themes
|
||||||
object.child_themes.order(:name)
|
object.child_themes
|
||||||
end
|
end
|
||||||
|
|
||||||
def settings
|
def settings
|
||||||
|
@ -45,7 +45,9 @@ class ThemeSettingsManager
|
|||||||
end
|
end
|
||||||
|
|
||||||
def db_record
|
def db_record
|
||||||
ThemeSetting.where(name: @name, data_type: type, theme: @theme).first
|
# theme.theme_settings will already be preloaded, so it is better to use
|
||||||
|
# `find` on an array, rather than make a round trip to the database
|
||||||
|
theme.theme_settings.to_a.find { |i| i.name.to_s == @name.to_s && i.data_type.to_s == type.to_s }
|
||||||
end
|
end
|
||||||
|
|
||||||
def has_record?
|
def has_record?
|
||||||
|
@ -3,8 +3,8 @@ require 'theme_settings_manager'
|
|||||||
|
|
||||||
describe ThemeSettingsManager do
|
describe ThemeSettingsManager do
|
||||||
|
|
||||||
|
let(:theme) { Fabricate(:theme) }
|
||||||
let(:theme_settings) do
|
let(:theme_settings) do
|
||||||
theme = Fabricate(:theme)
|
|
||||||
yaml = File.read("#{Rails.root}/spec/fixtures/theme_settings/valid_settings.yaml")
|
yaml = File.read("#{Rails.root}/spec/fixtures/theme_settings/valid_settings.yaml")
|
||||||
theme.set_field(target: :settings, name: "yaml", value: yaml)
|
theme.set_field(target: :settings, name: "yaml", value: yaml)
|
||||||
theme.save!
|
theme.save!
|
||||||
@ -37,12 +37,15 @@ describe ThemeSettingsManager do
|
|||||||
expect(bool_setting.value).to eq(true) # default
|
expect(bool_setting.value).to eq(true) # default
|
||||||
|
|
||||||
bool_setting.value = "true"
|
bool_setting.value = "true"
|
||||||
|
theme.reload
|
||||||
expect(bool_setting.value).to eq(true)
|
expect(bool_setting.value).to eq(true)
|
||||||
|
|
||||||
bool_setting.value = "falsse" # intentionally misspelled
|
bool_setting.value = "falsse" # intentionally misspelled
|
||||||
|
theme.reload
|
||||||
expect(bool_setting.value).to eq(false)
|
expect(bool_setting.value).to eq(false)
|
||||||
|
|
||||||
bool_setting.value = true
|
bool_setting.value = true
|
||||||
|
theme.reload
|
||||||
expect(bool_setting.value).to eq(true)
|
expect(bool_setting.value).to eq(true)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@ -51,15 +54,19 @@ describe ThemeSettingsManager do
|
|||||||
it "is always an integer" do
|
it "is always an integer" do
|
||||||
int_setting = find_by_name(:integer_setting)
|
int_setting = find_by_name(:integer_setting)
|
||||||
int_setting.value = 1.6
|
int_setting.value = 1.6
|
||||||
|
theme.reload
|
||||||
expect(int_setting.value).to eq(1)
|
expect(int_setting.value).to eq(1)
|
||||||
|
|
||||||
int_setting.value = "4.3"
|
int_setting.value = "4.3"
|
||||||
|
theme.reload
|
||||||
expect(int_setting.value).to eq(4)
|
expect(int_setting.value).to eq(4)
|
||||||
|
|
||||||
int_setting.value = "10"
|
int_setting.value = "10"
|
||||||
|
theme.reload
|
||||||
expect(int_setting.value).to eq(10)
|
expect(int_setting.value).to eq(10)
|
||||||
|
|
||||||
int_setting.value = "text"
|
int_setting.value = "text"
|
||||||
|
theme.reload
|
||||||
expect(int_setting.value).to eq(0)
|
expect(int_setting.value).to eq(0)
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -69,9 +76,11 @@ describe ThemeSettingsManager do
|
|||||||
expect { int_setting.value = 61 }.to raise_error(Discourse::InvalidParameters)
|
expect { int_setting.value = 61 }.to raise_error(Discourse::InvalidParameters)
|
||||||
|
|
||||||
int_setting.value = 60
|
int_setting.value = 60
|
||||||
|
theme.reload
|
||||||
expect(int_setting.value).to eq(60)
|
expect(int_setting.value).to eq(60)
|
||||||
|
|
||||||
int_setting.value = 1
|
int_setting.value = 1
|
||||||
|
theme.reload
|
||||||
expect(int_setting.value).to eq(1)
|
expect(int_setting.value).to eq(1)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@ -80,12 +89,15 @@ describe ThemeSettingsManager do
|
|||||||
it "is always a float" do
|
it "is always a float" do
|
||||||
float_setting = find_by_name(:float_setting)
|
float_setting = find_by_name(:float_setting)
|
||||||
float_setting.value = 1.615
|
float_setting.value = 1.615
|
||||||
|
theme.reload
|
||||||
expect(float_setting.value).to eq(1.615)
|
expect(float_setting.value).to eq(1.615)
|
||||||
|
|
||||||
float_setting.value = "3.1415"
|
float_setting.value = "3.1415"
|
||||||
|
theme.reload
|
||||||
expect(float_setting.value).to eq(3.1415)
|
expect(float_setting.value).to eq(3.1415)
|
||||||
|
|
||||||
float_setting.value = 10
|
float_setting.value = 10
|
||||||
|
theme.reload
|
||||||
expect(float_setting.value).to eq(10)
|
expect(float_setting.value).to eq(10)
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -96,6 +108,7 @@ describe ThemeSettingsManager do
|
|||||||
expect { float_setting.value = "text" }.to raise_error(Discourse::InvalidParameters)
|
expect { float_setting.value = "text" }.to raise_error(Discourse::InvalidParameters)
|
||||||
|
|
||||||
float_setting.value = 9.521
|
float_setting.value = 9.521
|
||||||
|
theme.reload
|
||||||
expect(float_setting.value).to eq(9.521)
|
expect(float_setting.value).to eq(9.521)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@ -106,9 +119,11 @@ describe ThemeSettingsManager do
|
|||||||
expect { string_setting.value = "a" }.to raise_error(Discourse::InvalidParameters)
|
expect { string_setting.value = "a" }.to raise_error(Discourse::InvalidParameters)
|
||||||
|
|
||||||
string_setting.value = "ab"
|
string_setting.value = "ab"
|
||||||
|
theme.reload
|
||||||
expect(string_setting.value).to eq("ab")
|
expect(string_setting.value).to eq("ab")
|
||||||
|
|
||||||
string_setting.value = "ab" * 10
|
string_setting.value = "ab" * 10
|
||||||
|
theme.reload
|
||||||
expect(string_setting.value).to eq("ab" * 10)
|
expect(string_setting.value).to eq("ab" * 10)
|
||||||
|
|
||||||
expect { string_setting.value = ("a" * 21) }.to raise_error(Discourse::InvalidParameters)
|
expect { string_setting.value = ("a" * 21) }.to raise_error(Discourse::InvalidParameters)
|
||||||
|
Loading…
Reference in New Issue
Block a user