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

Wednesday, April 05, 2006

Tiiiiiiiiime is on my side! Yes it is...

The first Rails project I completed for KCKCC has a lot of time and date functionality. Early on, I was quite disappointed with the flexibility of the time/date support in Rails. Specifically,

  1. Times can only be in 24-hour format.

  2. There's no time_select helper method, only datetime_select and date_select. Such a method would be helpful for those fields (DB-dependent) that are not complete DATETIMEs but only TIMEs. If you try to create it yourself using :discard_xxx options, the year persists.

  3. Not all of the helper methods let you specify :order.

  4. Specifying :prefix to the select_xxx methods doesn't work the way I expected it to.

  5. Parameters from self-constructed selects couldn't be parsed automagically by Rails. That is, you can't have a single parameter be a hash and expect it to construct the proper datatype. You have to parse it first.


The first problem was mostly solved by the 12_hour_time plugin, but there were a few bugs that prevented the multi-parameter attributes from being assigned properly, and the order was still not configurable.

Because this functionality was important to the users and I wanted to have a guarantee that the incoming data was at least CLOSE to correct, I ended up writing helper methods and some model methods that overcame these difficulties for me. One thing that bugged me from the beginning, however, was that this method was hardly DRY.

Thus, I'm in the process of writing a plugin (for public dissemination, of course) that will allow you to do the things that I had complained about above. Right now it's called "flex_times" but I'll take any suggestions on that front. Here's what you'll be able to do in your code:
# model
datetime_column [:start_time, :end_time]

# view
<%= time_select "object", "start_time",
:order => [:hour, :minute, :second, :ampm],
:twelve_hour => true %>

<%= datetime_select "object", "end_time",
:order => [:month, :day, :year, :hour] %>
The datetime_column declaration in the model will create assignment methods to parse the incoming data automatically, so you can stay DRY. The view helpers will be extremely configurable and I intend to include textual separators like :dash, :mdash, :comma, :slash, :period or allow String elements in the :order.

If it weren't for runtime extension, this would not be possible! I guess we can thank DHH and everyone else on the core team for plugins.

~conducts choir in "Halleluiah Chorus"~