S.E.A.N.I.C.U.S.

Friday, May 19, 2006

Crisis averted and future plugins(?)

This morning I completed uninstalling and reinstalling Ruby, RadRails, MySQL and all the gems I need.

I checked the project back out and discovered I had the same problem as yesterday. I tried my Ruby file-sanitizing script again to no avail.

Finally, on a hunch that it had was RadRails that corrupted my files, I copied their contents to an external editor and retyped about four different partials.

Success! I committed the non-corrupted versions to the repository.

---------------------------------------------------

It seems as I write my Rails projects, I discover more programming tasks that could be factored out into plugins because they'd be useful to others (and myself in other projects). I have potentially two more up-and-coming.

The first is one that I sort of completed this afternoon, after my file-corruption woes were placated. If you've used Basecamp (or many other AJAX-ed apps), you're aware of checkboxes and radio buttons that automatically save when changed. I wanted to make some for the project I'm working on that would make use of in_place_edit_for just as I had for my text-based fields.

After reading some documentation on Prototype, and peeking at the Rails code for ideas, I sat down and wrote a helper that creates AJAX'd radio buttons for you. On Monday, I intend to create one for check boxes, and then factor it out into a plugin. It works somewhat like a cross between in_place_editor_field and select_tag. Here's a sample:
<%= in_place_radio_buttons :post, :can_comment, 
:choices => ["all", "registered", "none"] %>

This would create a bank of radio buttons, separated by spaces, with labels, and they would have onClick events (although I'll change this to onChange) that fire off an AJAX request to the 'set_post_can_comment' in your controller, like any good in_place_editor would.

I did hit one snag in implementing this, however, and it has to do with Boolean fields in the AR model. The default in_place_edit_for produces a method that responds with a render :text => value_of_the_field (for more details about particulars look at the code embedded in the documentation). For a Boolean field when false, this produces render :text => false which makes AV look for a template to render, and produces a "Template missing" error. For the specific field I was using, I decided to rewrite the method and make that last line read render :text => value_of_the_field.to_s -- thus converting the value to a string before rendering it. When I factor out the plugin, I'll create a special AR helper/declaration for boolean-type fields.

My other potential plugin is a file upload handler, but since there are so many of those out there (file_column, acts_as_attachment, and the list goes on), I'm not sure whether I'll release it. It started as some really simple home-brewed file upload in another project, but grew and generalized for this one because I had to do it for multiple classes, with potentially multiple fields being uploaded into. Quite frankly, I think the code is a little ugly, but to my credit, with file upload you have to not only deal with the file itself, but also with the object lifecycle, especially if you're creating a new record at the same time as the upload.