out of time
-
Spork, Mongoid 2, Rails 3
In one of my (to date) signal accomplishments of 2011, I recently upgraded the Clique application to Rails 3. One thing I couldn’t get working right away, though, was the excellent Spork library, which lets you run RSpec without having to reload your application dependencies on every run. In particular, I couldn’t get Spork to pick up changes to my model classes. Not a deal-breaker, but TDD is pretty painful on an underpowered netbook when you have to load 50 gems each time you want to run a spec.
After some digging, I determined that Mongoid 2 has a railtie that eagerly loads the contents of the
app/modelsdirectory when the application loads. Game over, I thought — the railtie isn’t configurable, and I couldn’t even monkey-patch the method, because no application is run between loading Mongoid and running the railtie.But after a few painful days and some more digging, I noticed that Mongoid’s code uses
require_dependency, an ActiveSupport method that requires a file but makes the modules and classes defined therein reloadable (assuming you’re following the usual naming conventions for source files). Which means the problem was solved very simply by adding the following to myspec_helper.rb:Spork.each_run do ActiveSupport::Dependencies.clear endAnd that’s it. Usually this isn’t necessary because Spork’s master process never loads your application code, so it doesn’t have to unload it, but in this case it’s needed and it works.