Prevent ApplicationSetting to cache nil value

This commit is contained in:
Kamil Trzcinski 2017-10-19 15:07:18 +03:00
parent 1090514ab4
commit 64fd9814fd
3 changed files with 24 additions and 1 deletions

View File

@ -203,7 +203,10 @@ class ApplicationSetting < ActiveRecord::Base
ensure_cache_setup
Rails.cache.fetch(CACHE_KEY) do
ApplicationSetting.last
ApplicationSetting.last.tap do |settings|
# do not cache nils
raise 'missing settings' unless settings
end
end
rescue
# Fall back to an uncached value if there are any problems (e.g. redis down)

View File

@ -0,0 +1,5 @@
---
title: Fix application setting to cache nil object
merge_request:
author:
type: fixed

View File

@ -220,6 +220,21 @@ describe ApplicationSetting do
expect(described_class.current).to eq(:last)
end
end
context 'when an ApplicationSetting is not yet present' do
it 'does not cache nil object' do
# when missing settings a nil object is returned, but not cached
allow(described_class).to receive(:last).and_return(nil).twice
expect(described_class.current).to be_nil
# when the settings are set the method returns a valid object
allow(described_class).to receive(:last).and_return(:last)
expect(described_class.current).to eq(:last)
# subsequent calls get everything from cache
expect(described_class.current).to eq(:last)
end
end
end
context 'restrict creating duplicates' do