href_to_params

Every now and then I find myself in need of the ability to take a string like “http://www.estately.com/p/WA_Seattle” and figure out the params hash that it would generate – the method to do that in rails isn’t immediately obvious, but after a little digging, I’m happy to say it isn’t too hard.

I admit, I generally treat Routing as a black box: type a url in the browser and I get a params hash – works for me. I thought I was in for some serious digging to figure this one out, but it turns out it already exists, but only for testing under the name assert_recognizes. Adding the ability to recognize query strings, I came up with the following:

This was with about 30 minutes of research, so there very well could be a better way, but this works well for me. If anyone knows a cleaner method I’d love to hear about it.

Update:
I missed it during development, all the tests passed, but there’s something missing from above:

TestRequest can’t be auto-loaded by rails because it’s in a file called test_process.rb, and of course this wasn’t caught with tests as it is already loaded in the testing environment, guess TDD can’t save you from yourself.

Leave a Reply

Subscribe without commenting

startups.each { |startup| startup.code.inspect }