Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I change config.cache_classes programatically in Rails 3?

I have some iPhone client tests that run against my development rails server. The whole suite runs an order of magnitude faster if I turn on class caching in the Rails config. On the other hand, that slows down development when I'm not actually running the tests.

I want the test suite to hit an action at the beginning to turn on class caching and another action at the end to turn class caching off again.

Is this even possible? If so, how?

like image 697
ilya Avatar asked Nov 14 '22 19:11

ilya


1 Answers

Not without some serious hacking. Rails goes to quite a lot of trouble to make sure your files are reloaded on every request (when cache_classes=false). The value of the cache_classes configuration variable is used by initializers in several places not the least of which being:

  • using require to load ruby files when cache_classes is true (meaning they are no longer reloadable)
  • setting up dispatcher callbacks to reaload the application on every request when cache_classes is false

You do have access to the value of the cache_classes variable, and you can even change it if you like:

Rails.configuration.cache_classes = true

But, this will have no effect on the running rails instance as the initializers where that value is used only run once when the rails app starts up.

What this means is this, unless you're prepared to invest some serious time and hacking effort you can't really avoid a restart of your server. So, what you need to look at is controlling this restart process via your test suite.

For example you can try to restart rails from within rails. This would allow you to define an action that your test suite can hit right before it begins executing (to restart the server in the right mode), and another action which the server can hit after all tests have finished, to restart everything with cache_classes set to what it used to be. You would control the value of cache classes via an environment variable like this post suggests.

It would still require a bit of work to set all of this up and get it to hang together, but this is probably your best bet if you want an 'auto-magical' solution.

like image 182
skorks Avatar answered Dec 09 '22 16:12

skorks