Commits

Anonymous committed 7866ee9

Completely disable the Rails cache for tests

Settings extension to handle cache behaviour by Gregor Schmidt.

Comments (0)

Files changed (2)

app/models/setting.rb

 
   # 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)
       "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)

test/test_helper.rb

   # Add more helper methods to be used by all tests here...
   def setup
     super
+    Setting.use_caching = false
     Rails.cache.clear
   end