mirror of
https://github.com/discourse/discourse.git
synced 2025-02-25 18:55:32 -06:00
DEV: Refactor Theme#settings to return a hash instead of array (#25516)
Why this change? Returning an array makes it hard to immediately retrieve a setting by name and makes the retrieval an O(N) operation. By returning an array, we make it easier for us to lookup a setting by name and retrieval is O(1) as well.
This commit is contained in:
committed by
GitHub
parent
fb469e7e2f
commit
44f8418093
@@ -647,14 +647,15 @@ class Theme < ActiveRecord::Base
|
||||
|
||||
def settings
|
||||
field = settings_field
|
||||
return [] unless field && field.error.nil?
|
||||
settings = []
|
||||
settings = {}
|
||||
|
||||
ThemeSettingsParser
|
||||
.new(field)
|
||||
.load do |name, default, type, opts|
|
||||
settings << ThemeSettingsManager.create(name, default, type, self, opts)
|
||||
end
|
||||
if field && field.error.nil?
|
||||
ThemeSettingsParser
|
||||
.new(field)
|
||||
.load do |name, default, type, opts|
|
||||
settings[name] = ThemeSettingsManager.create(name, default, type, self, opts)
|
||||
end
|
||||
end
|
||||
|
||||
settings
|
||||
end
|
||||
@@ -668,7 +669,7 @@ class Theme < ActiveRecord::Base
|
||||
def cached_default_settings
|
||||
Theme.get_set_cache "default_settings_for_theme_#{self.id}" do
|
||||
settings_hash = {}
|
||||
self.settings.each { |setting| settings_hash[setting.name] = setting.default }
|
||||
self.settings.each { |name, setting| settings_hash[name] = setting.default }
|
||||
|
||||
theme_uploads = build_theme_uploads_hash
|
||||
settings_hash["theme_uploads"] = theme_uploads if theme_uploads.present?
|
||||
@@ -682,7 +683,7 @@ class Theme < ActiveRecord::Base
|
||||
|
||||
def build_settings_hash
|
||||
hash = {}
|
||||
self.settings.each { |setting| hash[setting.name] = setting.value }
|
||||
self.settings.each { |name, setting| hash[name] = setting.value }
|
||||
|
||||
theme_uploads = build_theme_uploads_hash
|
||||
hash["theme_uploads"] = theme_uploads if theme_uploads.present?
|
||||
@@ -724,13 +725,13 @@ class Theme < ActiveRecord::Base
|
||||
# theme.get_setting(:some_string) => "hello"
|
||||
#
|
||||
def get_setting(setting_name)
|
||||
target_setting = settings.find { |setting| setting.name == setting_name.to_sym }
|
||||
target_setting = settings[setting_name.to_sym]
|
||||
raise Discourse::NotFound unless target_setting
|
||||
target_setting.value
|
||||
end
|
||||
|
||||
def update_setting(setting_name, new_value)
|
||||
target_setting = settings.find { |setting| setting.name == setting_name }
|
||||
target_setting = settings[setting_name.to_sym]
|
||||
raise Discourse::NotFound unless target_setting
|
||||
|
||||
target_setting.value = new_value
|
||||
|
||||
Reference in New Issue
Block a user