FEATURE: Allow theme settings to request refresh (#15037)

Similar to site settings, adds support for `refresh` option to theme settings.

```yaml
super_feature_enabled:
  type: bool
  default: false
  refresh: true
```
This commit is contained in:
Jarek Radosz 2021-11-22 13:16:56 +01:00 committed by GitHub
parent f43c433d50
commit d91d67a442
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 63 additions and 0 deletions

View File

@ -94,6 +94,13 @@ class Theme < ActiveRecord::Base
clear_cached_settings!
DB.after_commit { ColorScheme.hex_cache.clear }
notify_theme_change(with_scheme: notify_with_scheme)
if theme_setting_requests_refresh
DB.after_commit do
Discourse.request_refresh!
self.theme_setting_requests_refresh = false
end
end
end
def update_child_components
@ -572,6 +579,10 @@ class Theme < ActiveRecord::Base
raise Discourse::NotFound unless target_setting
target_setting.value = new_value
if target_setting.requests_refresh?
self.theme_setting_requests_refresh = true
end
end
def update_translation(translation_key, new_value)
@ -727,6 +738,8 @@ class Theme < ActiveRecord::Base
private
attr_accessor :theme_setting_requests_refresh
def to_scss_variable(name, value)
escaped = SassC::Script::Value::String.quote(value, sass: true)
"$#{name}: unquote(#{escaped});"

View File

@ -37,6 +37,10 @@ class ThemeSettingsManager
@opts[:description] # Old method of specifying description. Is now overridden by locale file
end
def requests_refresh?
@opts[:refresh]
end
def value=(new_value)
ensure_is_valid_value!(new_value)

View File

@ -43,6 +43,8 @@ class ThemeSettingsParser
opts[:textarea] = !!raw_opts[:textarea]
opts[:json_schema] = raw_opts[:json_schema]
opts[:refresh] = !!raw_opts[:refresh]
opts
end

View File

@ -80,3 +80,7 @@ invalid_json_schema_setting:
valid_json_schema_setting:
default: ""
json_schema: '{ "type": "array", "uniqueItems": true, "items": { "type": "object", "properties": { "color": { "type": "string" }, "icon": { "type": "string" } }, "additionalProperties": false } }'
causes_refresh:
default: ""
refresh: true

View File

@ -902,4 +902,44 @@ HTML
expect(new_digest).to eq(digest)
end
end
describe "#update_setting" do
it "requests clients to refresh if `refresh: true`" do
theme.set_field(target: :settings, name: "yaml", value: <<~YAML)
super_feature_enabled:
type: bool
default: false
refresh: true
YAML
ThemeSetting.create!(theme: theme, data_type: ThemeSetting.types[:bool], name: "super_feature_enabled")
theme.save!
messages = MessageBus.track_publish do
theme.update_setting(:super_feature_enabled, true)
theme.save!
end.filter { |m| m.channel == "/global/asset-version" }
expect(messages.count).to eq(1)
end
it "does not request clients to refresh if `refresh: false`" do
theme.set_field(target: :settings, name: "yaml", value: <<~YAML)
super_feature_enabled:
type: bool
default: false
refresh: false
YAML
ThemeSetting.create!(theme: theme, data_type: ThemeSetting.types[:bool], name: "super_feature_enabled")
theme.save!
messages = MessageBus.track_publish do
theme.update_setting(:super_feature_enabled, true)
theme.save!
end.filter { |m| m.channel == "/global/asset-version" }
expect(messages.count).to eq(0)
end
end
end