DEV: Replace site_setting_saved DiscourseEvent with site_setting_changed (#7401)

* DEV: Replace site_setting_saved DiscourseEvent with site_setting_changed

site_setting_saved is confusing for a few reasons:
- It is attached to the after_save of the ActiveRecord model. This is confusing because it only works 'properly' with the db_provider
- It passes the activerecord model as a parameter, which is confusing because you get access to the 'database' version of the setting, rather than the ruby setting. For example, booleans appear as 'y' or 'n' strings.
- When the event is called, the local process cache has not yet been updated. So if you call SiteSetting.setting_name inside the event handler, you will receive the old site setting value

I have deprecated that event, and added a new site_setting_changed event. It passes three parameters:
- Setting name (symbol)
- Old value (in ruby format)
- New value (in ruby format)

It is triggered after the setting has been persisted, and the local process cache has been updated.

This commit also includes a test case which describes the confusing behavior. This can be removed once site_setting_saved is removed.
This commit is contained in:
David Taylor
2019-04-18 16:48:01 +01:00
committed by GitHub
parent 6e46197bc8
commit 7826acc4a7
4 changed files with 86 additions and 10 deletions

View File

@@ -344,19 +344,23 @@ module SiteSettingExtension
end
def remove_override!(name)
old_val = current[name]
provider.destroy(name)
current[name] = defaults.get(name, default_locale)
clear_uploads_cache(name)
clear_cache!
DiscourseEvent.trigger(:site_setting_changed, name, old_val, current[name]) if old_val != current[name]
end
def add_override!(name, val)
old_val = current[name]
val, type = type_supervisor.to_db_value(name, val)
provider.save(name, val, type)
current[name] = type_supervisor.to_rb_value(name, val)
clear_uploads_cache(name)
notify_clients!(name) if client_settings.include? name
clear_cache!
DiscourseEvent.trigger(:site_setting_changed, name, old_val, current[name]) if old_val != current[name]
end
def notify_changed!