Custom routing for plugins and tests

Haven’t had as much time to play lately as I’ve been put onto a new project at work which is taking up a fair bit of my thinking time, but I’m slowly pushing ahead.

Revisions 16 and 17 are in with a nice little feature:

I decided to have a go at updating the rails version to the latest trunk, just to see what would happen, and had most of the functional tests failing with wierd nil errors. The cause of this was the use of named routes combined with misuse of the with_routing function.

When you use ‘with_routing’ in a test, the new set of routes that are used will be used instead of those defined in routes.rb. The radiant test code was assuming that the previously defined named routes were still available. Rails also had a quirk where any named routing method would, instead of throwing a NameError, as it should, instead attempted to look up the old controller/action using the new naming rules. Since the with_routing in radiant was used to add a default route (’:controller/:action’) all the old named routes would map to their instead.

With the rewriting of rail’s routing code, the named routes will now fail with a cryptic error about calling a method on nil. When the routes are replaced, the named route helper methods still remain on controllers, but they will fail instead of looking up the route (they should really get removed, but i don’t see how that could be done safely).

To work around this, I’ve added a mechanism to allow the addition of extra routes through the Radiant::CustomRoutes object. Call Radiant::CustomRoutes.register with a block that accepts an normal rails route mapper, and the Radiant::CustomRoute.activate method will add those routes in during a route reload.

The tests also make use of this to add additional routes for testing purposes by using the “with_radiant_routing” helper.

This stuff should be great for adding radiant plugins that want to include extra controllers in the administration interface (or to anywhere on the site really). Bonus.