I always have a problem parsing Rails‘ named routes in my head. It usually takes me a lot of time and effort to decide which controller action or view file is responsible for given path. Of course, it’s easy to tell when you look at:
that you probably should go to
users_controller.rb and find
edit action and
views/users/edit.html.haml is the right view file. But when nested routes, prefixes and non-RESTful actions all come to play, you might end up with a monster like:
Don’t laugh, this is real. So, what’s going on here? What is the action name? Are those “employer” things prefixes or nested routes? Where do I find the view? To answer all these questions, the Rails Routes TextMate bundle was born.
What does it do?
When you install it, you can press
Cmd-P on any named route expression (almost anything that ends with
_url) to go to appropriate action or view file. There are also
Ctrl-Cmd-P (go to action) and
Opt-Cmd-P (go to view) available.
Another problem that always bothered me a lot (and is finally solved thanks to Rails Routes bundle) is when you want to edit a specific view file, press
Cmd-T, start typing
edit.html and are presented with lots (22 in my current project) of
edit.html.haml files that live in different directories and there is no way to narrow the search further, so your eyes must do the scanning for the right file. With Rails Routes you just jump from the route to the view without any hassles.
To be honest, I created this bundle mainly because I wanted to see if it was that difficult. But now
Cmd-P is one of the most frequently used commands for me. It’s like an addiction and I hope they won’t make it illegal. I’m also thinking about adding it to my TextMate shortcuts you should be using article.
How does it work?
Thanks to Rails introspective architecture it’s quite easy to find the route definition. Just load
routes.rb file and then you can access all the named routes in a big, hash-like
ActionController::Routing::Routes.named_routes object. To access two most important things, controller and action, you just do something like:
path = :edit_user # no _path/_url, no params, must be a symbol route = ActionController::Routing::Routes.named_routes[path] controller = defaults[:controller] action = defaults[:action]
And that’s the core of it. All the rest of the bundle is trivial: find the files, locate the action, show some nice menus etc.
You may notice that the route parsing part is a little bit slow: on my computer it takes about a second (I can live with that) for the “Loading routes.rb” message to disappear. But I have plans for making it faster so stay tuned. Also, be sure to check the “Troubleshooting” section on the github page if it takes much longer than a second.
Stay tuned for updates because there I will be adding more features soon (the TODO list starts overflowing the post-it card). And I’m always open for suggestions, feature requests, bugfixes etc.