diff --git a/app/models/setting.rb b/app/models/setting.rb index 101595ad..581766de 100644 --- a/app/models/setting.rb +++ b/app/models/setting.rb @@ -97,7 +97,11 @@ class Setting < ActiveRecord::Base # Returns the value of the setting named name def self.[](name) - Marshal.load(Rails.cache.fetch(self.cache_key(name)) {Marshal.dump(find_or_default(name).value)}) + if use_caching? + Marshal.load(Rails.cache.fetch(self.cache_key(name)) {Marshal.dump(find_or_default(name).value)}) + else + find_or_default(name).value + end end def self.[]=(name, v) @@ -156,6 +160,25 @@ class Setting < ActiveRecord::Base "only, you may use Setting.first.try(:touch)" end + # Temporarily deactivate settings caching in the block scope + def self.uncached + cache_setting = self.use_caching + self.use_caching = false + yield + ensure + self.use_caching = cache_setting + end + + # Check if Setting caching should be performed + def self.use_caching? + !Thread.current['chiliproject/settings/do_not_use_caching'] + end + + # Dis-/En-able Setting caching. This is mainly intended to be used in tests + def self.use_caching=(new_value) + Thread.current['chiliproject/settings/do_not_use_caching'] = !new_value + end + private # Returns the Setting instance for the setting named name # (record found in database or new record with default value) diff --git a/test/test_helper.rb b/test/test_helper.rb index afbc02e2..435cc5ab 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -47,6 +47,7 @@ class ActiveSupport::TestCase # Add more helper methods to be used by all tests here... def setup super + Setting.use_caching = false Rails.cache.clear end