Tickling link_to into submission
What a crappy title. Ho hum.
This bit explains the itch I had. Skip over it if you like.
I recently finished re-structuring some of the code at work, in which I namespaced all of the admin type controllers. At the same time, I attempted to split some of the larger, confused (about their responsibilities) controllers into smaller chunks. This all seems to have worked quite well, but we’ve ended up with some naff (I haven’t used that word in ages) action names.
I like, where possible, to follow the scaffold naming convention (list, show, new, create, edit, update and destroy); and so went ahead and started changing things. I was writing my tests like mum always said I should1 when I realised that there was a bit missing (uh oh). You see, I was ensuring that my controllers responded to certain actions; and I was also ensuring that my views had links to certain controllers and actions. The missing link was a check as to whether the links in my views went to valid controller actions. I could, for example, have been testing for a link to /foo/edit in my view but for an edit_foo action in my foo_controller test. Both tests would pass, but the application would still be broken. Not cool. We do actually have something in place to perform these tests but I’m still a bit scared of selenium and don’t use it as much as I should.
Anyway, i started thinking about bending the link_to helper to my will. I figured that the little fella was a good candidate to not only give me a perfectly formed chunk of html goodness; but to also tell me whether the said chunk of html goodness was actually going to work (i.e. whether my app responded to the generated url). Cool idea huh.
I’m getting tired and a little bored now so I’ll wrap this post up. I’ve created a plugin (svn co http://chrisroos.googlecode.com/svn/trunk/plugins/action_view_helper_url_helper/) that replaces the link_to method to add a class attribute (invalid_href_target or valid_href_target) to the generated anchor tag. You could then use the assert_no_broken_links assertion to ensure there are no anchors with a class of invalid_href_target in your views; and/or you could style it (see resource/style.css as an example) so that you can see any broken links at a glance whilst in development (as shown in the image below).
The code is pretty poor/naive, works with rails 1.0.0 (hence my trying to set-up a rails 1.0.0 app earlier), doesn’t work with rails 1.1.6 (not sure about versions in between) and may or may not work for you. I kinda think it’s a good idea but there’s every chance that a) it’s not or b) there’s already a simple solution to this problem.
Let me know how you get on if you decide to risk the health of your project by trying this plugin.
1 She didn't say that.