FIX: Recalculate settings when dependent settings change

FIX: Recalculate settings when dependent settings change

diff --git a/app/models/site_setting.rb b/app/models/site_setting.rb
index fa57bbd..f0be63c 100644
--- a/app/models/site_setting.rb
+++ b/app/models/site_setting.rb
@@ -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
 
diff --git a/config/initializers/014-track-setting-changes.rb b/config/initializers/014-track-setting-changes.rb
index bf7d998..00150af 100644
--- a/config/initializers/014-track-setting-changes.rb
+++ b/config/initializers/014-track-setting-changes.rb
@@ -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
diff --git a/spec/models/site_setting_spec.rb b/spec/models/site_setting_spec.rb
index 0eb553e..ce0a53c 100644
--- a/spec/models/site_setting_spec.rb
+++ b/spec/models/site_setting_spec.rb
@@ -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

GitHub sha: 8f892545

2 Likes