Completely disable the Rails cache for tests

Settings extension to handle cache behaviour by Gregor Schmidt.
This commit is contained in:
Holger Just 2012-01-16 15:33:42 +01:00
parent a938d582b1
commit 637ca24aed
2 changed files with 25 additions and 1 deletions

View File

@ -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)

View File

@ -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