From 0ac5126a781656f714ed18bda0d1d1f9f5e17faa Mon Sep 17 00:00:00 2001 From: Guo Xiang Tan Date: Fri, 16 Nov 2018 11:02:51 +0800 Subject: [PATCH] FIX: Clear uploads cache on `SiteSetting.refresh!`. This fixes a bug where the return value of uploads site settings may defer between processes even though we trigger a refresh via MessageBus. --- lib/site_setting_extension.rb | 22 +++++++++++++++---- .../components/site_setting_extension_spec.rb | 18 +++++++++++++++ 2 files changed, 36 insertions(+), 4 deletions(-) diff --git a/lib/site_setting_extension.rb b/lib/site_setting_extension.rb index 22cd16adeb5..07bd5c4708e 100644 --- a/lib/site_setting_extension.rb +++ b/lib/site_setting_extension.rb @@ -269,8 +269,16 @@ module SiteSettingExtension shadowed_settings.each { |ss| new_hash[ss] = GlobalSetting.send(ss) } changes, deletions = diff_hash(new_hash, current) - changes.each { |name, val| current[name] = val } - deletions.each { |name, _| current[name] = defaults_view[name] } + + changes.each do |name, val| + current[name] = val + clear_uploads_cache(name) + end + + deletions.each do |name, _| + current[name] = defaults_view[name] + clear_uploads_cache(name) + end clear_cache! end @@ -318,7 +326,7 @@ module SiteSettingExtension def remove_override!(name) provider.destroy(name) current[name] = defaults.get(name, default_locale) - uploads.delete(name) + clear_uploads_cache(name) clear_cache! end @@ -326,7 +334,7 @@ module SiteSettingExtension val, type = type_supervisor.to_db_value(name, val) provider.save(name, val, type) current[name] = type_supervisor.to_rb_value(name, val) - uploads.delete(name) + clear_uploads_cache(name) notify_clients!(name) if client_settings.include? name clear_cache! end @@ -480,6 +488,12 @@ module SiteSettingExtension @uploads[provider.current_site] ||= {} end + def clear_uploads_cache(name) + if type_supervisor.get_type(name) == :upload && uploads.has_key?(name) + uploads.delete(name) + end + end + def logger Rails.logger end diff --git a/spec/components/site_setting_extension_spec.rb b/spec/components/site_setting_extension_spec.rb index 1b76de978e3..858a5011dae 100644 --- a/spec/components/site_setting_extension_spec.rb +++ b/spec/components/site_setting_extension_spec.rb @@ -133,6 +133,24 @@ describe SiteSettingExtension do settings.foo = "baz" expect(settings.foo).to eq("baz") end + + it "clears the cache for site setting uploads" do + settings.setting(:upload_type, "", type: :upload) + upload = Fabricate(:upload) + settings.upload_type = upload + + expect(settings.upload_type).to eq(upload) + expect(settings.send(:uploads)[:upload_type]).to eq(upload) + + upload2 = Fabricate(:upload) + settings.provider.save(:upload_type, upload2.id, SiteSetting.types[:upload]) + + expect do + settings.refresh! + end.to change { settings.send(:uploads)[:upload_type] }.from(upload).to(nil) + + expect(settings.upload_type).to eq(upload2) + end end describe "multisite" do