discourse/config/initializers/014-track-setting-changes.rb
David Taylor 7826acc4a7
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.
2019-04-18 16:48:01 +01:00

35 lines
1.0 KiB
Ruby

DiscourseEvent.on(:site_setting_changed) do |name, old_value, new_value|
# Enabling `must_approve_users` on an existing site is odd, so we assume that the
# existing users are approved.
if name == :must_approve_users && new_value == true
User.where(approved: false).update_all(approved: true)
end
if name == :emoji_set
Emoji.clear_cache
before = "/images/emoji/#{old_value}/"
after = "/images/emoji/#{new_value}/"
Scheduler::Defer.later("Fix Emoji Links") do
DB.exec("UPDATE posts SET cooked = REPLACE(cooked, :before, :after) WHERE cooked LIKE :like",
before: before,
after: after,
like: "%#{before}%"
)
end
end
Report.clear_cache(:storage_stats) if [:backup_location, :s3_backup_bucket].include?(name)
if name == :slug_generation_method
Scheduler::Defer.later("Null topic slug") do
Topic.update_all(slug: nil)
end
end
Jobs.enqueue(:update_s3_inventory) if [:s3_inventory, :s3_upload_bucket].include?(name)
SvgSprite.expire_cache if name.to_s.include?("_icon")
end