I have a little interactive script for doing a bunch of things with my Rails app. Generally I run this with the cache_classes config option disabled so that I can modify and reload views without having to restart it however certain tests will only work with cache_classes enabled. In particular integration tests exhibit very strange behaviour when cache_classes is disabled.
Ideally what I would like to do is to be able to turn cache_classes on and off from within the script without restarting the script but whilst I can update the value of this variable, it doesn’t have any effect.
Does anyone know of a way to dynamically update cache_classes?
Alternatively, if there is a way of telling rails about view changes whilst cache_classes is on, that would be even better as then I could leave cache_classes permanently on.
Many thanks
For the benefit of anyone else who may have this issue, there is a relatively straight forward way of using a custom method to determine whether views should be cached.
First we need to override a method in the ActionView::Resolver class as follows
Then we can use our custom method to turn the view caching on and off depending on whatever criteria we want.
I’ve posted this in the hope of saving someone else a big dig through the Rails source code but there are a few caveats:
a) I have only checked this on Rails 3.0.14 as that is the current version that is applicable to me now. It may or may not work on other versions.
b) If you put a slow method in here you will absolutely kill your performance as this method will get called a lot of times per request. I am happy to do this since in my case the code is only being used in my local test environment and I have to explicitly include it when I want it, but personally, I probably wouldn’t have the balls/stomach to put something like this in production code.
Hope this helps someone.