Introducing Rails Routes TextMate bundle

I always have a problem parsing Railsnamed 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:

edit_user_path(@user)

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:

precreate_new_employer_employer_account_path

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 _path or _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.

About these ads

3 responses to “Introducing Rails Routes TextMate bundle

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: