FIX: Recalculate settings when dependent settings change

This commit is contained in:
Gerhard Schlager 2019-07-12 20:42:43 +02:00
parent fb2df0b43b
commit 8f89254554
3 changed files with 28 additions and 1 deletions

View File

@ -111,6 +111,19 @@ class SiteSetting < ActiveRecord::Base
SiteSetting.manual_polling_enabled? || SiteSetting.pop3_polling_enabled?
end
WATCHED_SETTINGS ||= [
:attachment_content_type_blacklist,
:attachment_filename_blacklist,
:unicode_username_character_whitelist,
:markdown_typographer_quotation_marks
]
def self.reset_cached_settings!
@attachment_content_type_blacklist_regex = nil
@attachment_filename_blacklist_regex = nil
@unicode_username_whitelist_regex = nil
end
def self.attachment_content_type_blacklist_regex
@attachment_content_type_blacklist_regex ||= Regexp.union(SiteSetting.attachment_content_type_blacklist.split("|"))
end
@ -120,7 +133,7 @@ class SiteSetting < ActiveRecord::Base
end
def self.unicode_username_character_whitelist_regex
@unicode_username_whitelist_regex = SiteSetting.unicode_username_character_whitelist.present? \
@unicode_username_whitelist_regex ||= SiteSetting.unicode_username_character_whitelist.present? \
? Regexp.new(SiteSetting.unicode_username_character_whitelist) : nil
end

View File

@ -41,4 +41,8 @@ DiscourseEvent.on(:site_setting_changed) do |name, old_value, new_value|
if SiteIconManager::WATCHED_SETTINGS.include?(name)
SiteIconManager.ensure_optimized!
end
if SiteSetting::WATCHED_SETTINGS.include?(name)
SiteSetting.reset_cached_settings!
end
end

View File

@ -196,4 +196,14 @@ describe SiteSetting do
end
end
end
describe 'cached settings' do
it 'should recalcualte cached setting when dependent settings are changed' do
SiteSetting.attachment_filename_blacklist = 'foo'
expect(SiteSetting.attachment_filename_blacklist_regex).to eq(/foo/)
SiteSetting.attachment_filename_blacklist = 'foo|bar'
expect(SiteSetting.attachment_filename_blacklist_regex).to eq(/foo|bar/)
end
end
end