Completely disable the Rails cache for tests
Settings extension to handle cache behaviour by Gregor Schmidt.
This commit is contained in:
parent
a938d582b1
commit
637ca24aed
|
@ -97,7 +97,11 @@ class Setting < ActiveRecord::Base
|
||||||
|
|
||||||
# Returns the value of the setting named name
|
# Returns the value of the setting named name
|
||||||
def self.[](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
|
end
|
||||||
|
|
||||||
def self.[]=(name, v)
|
def self.[]=(name, v)
|
||||||
|
@ -156,6 +160,25 @@ class Setting < ActiveRecord::Base
|
||||||
"only, you may use Setting.first.try(:touch)"
|
"only, you may use Setting.first.try(:touch)"
|
||||||
end
|
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
|
private
|
||||||
# Returns the Setting instance for the setting named name
|
# Returns the Setting instance for the setting named name
|
||||||
# (record found in database or new record with default value)
|
# (record found in database or new record with default value)
|
||||||
|
|
|
@ -47,6 +47,7 @@ class ActiveSupport::TestCase
|
||||||
# Add more helper methods to be used by all tests here...
|
# Add more helper methods to be used by all tests here...
|
||||||
def setup
|
def setup
|
||||||
super
|
super
|
||||||
|
Setting.use_caching = false
|
||||||
Rails.cache.clear
|
Rails.cache.clear
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue