Load path for a per-app namespace à la Corey Haines

Corey Haines, an exponent of dependency-free testing in Rails apps, has suggested that all custom app logic be moved into its own namespace, and that namespace get its own directory under app/. So a custom class Baz in an app called foobar would be namespaced as Foobar::Baz, and the code would live in app/foobar/baz.rb.

A slightly non-obvious consequence of this is that while Rails picks up all subdirectories of app/ in its class loading, it's expecting that any namespacing will occur inside those directories, not be expressed by those directory names themselves. If we just throw code in app/foobar/baz.rb, it will expect a top-level Baz class to be there, not Foobar::Baz.

One solution is to add a second foobar subdirectory, so the code would now live in app/foobar/foobar/baz.rb. That seems suboptimal.

Instead we can add app/ itself to config.autoload_paths in config/application.rb:

config.autoload_paths += %W( #{config.root}/app )

Then app/foobar becomes eligible to house code in the Foobar namespace.

Maxim Chernyak has a concise cheatsheet on Rails load paths that walks through the various gotchas lurking there.