Saturday, December 30, 2006

Smalltalk Cleverness in Rails, Revisited

Pat Maddox saw my earlier post and came back with another solution.

(The other solution is a link due to some formatting issues in Blogger.)

What's cool about Pat's solution is that it solves a more general problem. The more general problem is, how do you use auto_complete_for and text_field_with_auto_complete in your controller and views if you're not going directly to an ActiveRecord model? The answer is, the autocomplete methods only require a particular method from ActiveRecord, namely find, and they don't actually require the ActiveRecord version -- just any method named find.

Anyway, going back to the specific functionality, the downside is, it's more code than my original solution, but the upside is, it isolates all the wizardly stuff in a pair of objects that can quite happily live in the /lib directory. This means the controller and view retain the usual helper methods, which make them a lot more maintainable. Unfortunately, as Pat's own blog points out, it doesn't require an expert to use Rails, which means that if you're using expert techniques within Rails, isolating them in the /lib directory is a good idea.

Imagine a world filled with dragons, elves, etc. Putting the wizardly code in /lib is kind of the same thing as making wizards live in towers, instead of smack dab in the middle of downtown. If you've got a bustling city filled with dwarves and orcs arguing about the price of ale, the last thing in the world you want is some irresponsible wizard turning entire city blocks into antimatter during the course of some experiment gone wrong. Perhaps this analogy is a bit of a stretch, but it's the best party weekend of the year and a blizzard has me snowed in. I tried to dig my car out earlier today, I couldn't even figure out where it was, and my driveway is only about ten feet long. Anyway, this post seems to have a bunch of tangents, but long story short, Pat's solution is pretty cool.

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.