DateBocks, the new, intuitive date input selection for your forms

by Nathaniel S. H. Brown

In January, being inspired by Simon Willison, I came up with a combined method which would be the answer to my days of entering in dates through drop down selections .

date_select.gif

We use common terms in english to express how we identify a date coming up, or a date in the past. Such as "Next Tuesday", "Last Month", or even as simple as "Friday". We clearly know which date that identifies, but until now, a computer had no idea, as far as forms were concerned.

We have in the past seen date libraries such as RUNT, and the new Chronic library for Ruby directly, but never has there been a way to enter these expressions through a form.

The solution was to be elegant, and intuitive. To be able to select a date, by entering in a grammatical term such as "Tuesday", "14th of February", or even a regular binary date such as "2006-12-02" or "10-24-2006" and all would be well as far as the input would be concerned. Ideally, the form should be able to be a universal translator to dates. Allowing the input of any expression, and outputting whatever the database expects.

example_expressions.gif

To add to the elegance, the JSCalendar was also to be integrated. So when you found yourself looking for a specific date, one which no expression would be able to match (yet), then you directly choose what you want from a handy DHTML calendar.

So, DateBocks was born. The culmination of a DHTML calendar, and most importantly with the ability to enter in gramatical expressions for the date selection.

datebocks_field_expanded.gif

I invite you to try it out on the live demo and see for yourself just how useful this tool really is. And hopefully, you may very well find yourself using it or wishing you were using it - whenever you enter a date.

11 Comments

Lee
2006-09-13 11:09:16


The military date spec (which I learned in the Army) is something like this
9OCT2046


it is unambiguous, easy to read and easy to type. I think that it shold be allowed.

namxam
2006-09-13 11:45:52
This is indeed some nice piece of code. Can't wait to plug it into one of my applications.
Kevin
2006-09-13 12:15:51
Why does it have to be an Engine though? I have a fear engines will taint my apps...
Lori
2006-09-13 12:50:51
Very slick. I have seen many date controls, but that one is definitely "best in class".
Nathaniel
2006-09-13 18:08:04
@Kevin: Engines are da bomb! They do the exact opposite of what you are trying to avoid. They strive to keep your application clean of any 3rd party code, allowing you to customize it through overrides within your own app directory. They keep their own images, javascripts, stylesheets, models, views, controllers, tests, and everything else within their own space.


If you look at some of the alternative implementations such as a plugin with a setup.rb, which would be required to run upon install, they would move all the code mentioned above and pollute your rails tree.


Even more importantly. When I come up with a new version, add/remove code and files, you have to re-run that setup.rb, repollute your rails tree in addition to merging any changes that may have occured. Messss-sy!


I had originally put in the bug tracker at http://dev.toolbocks.com to create a straight-up plugin version for the library, but unless I am able to find a way to keep your rails tree clean from all the code, I simply won't do it. It really is a less elegant way to implement any sort of code that has the full sub-stack of rails code.

Ben
2006-09-21 14:31:38
I just happened by this post and noticed your cool calendar widget. I do have a suggested improvement (AJAX-based). The list of valid phrases that match what you have already typed should dynamically filter in a drop-down as you type, a-la Google Suggest (http://www.google.com/webhp?complete=1&hl=en). That way, you won't type (or mistype) a phrase that the tool can't understand.
johnpg
2006-10-13 10:07:29
The issue I have with Engines is that they are not part of core, and often break when new versions of Rails come out. For that reason I stopped using them. What's more, this is such a simple plugin that Engines seem like overkill.


I haven't looked at the code so I might be missing something, but I think it would be fairly simple to make this a plugin and keep most of the rails tree clean. A simple generator could put the necessary javascripts and such in public/dateblocks, and upgrades could either remove or backup the old version and replace it with the new. You also could do the includes/mixin inside the init.rb and avoid adding anything to your application.rb and helper files so all you'd need to do is install the plugin, run the generator and add the new helper tags in your views. That just seems "better" to me.


Once again I might be missing something since I've not looked over the code yet. But assuming I'm right, this would really make more sense as a standard plugin. I do agree that the Engines idea is potentially really useful and wish a simpler and lighter version (an extension to the plugin system) was built into core. But I also understand the religious issues around these things and am not holding my breath. :-)


In any case, nice work!


Cheers,
John

Onno
2006-10-28 00:58:53

(..) and outputting whatever the database expects

Why not use the "multi-part" hidden form fields as used by e.g. date_select instead? This way you can just do, for example, @my_model.upgrade_attributes(:params[my_model]). The point being that ActiveRecord is a beautiful abstraction from the database nitty-gritty details.

I have also noticed that the date is directly read from the database and put into a text field. For localization / globalization this is really inadequate. Why not parse the date into a Ruby Time object, and then convert it to the proper format? This would require the date format to be set outside of the javascript files of course. Perhaps in one central rb file, together with all translations that are now also in the javascript files?


Overall, this date control is by far the best I've seen on any platform. Tighter integration with Rails and proper localization options would make it rock even more.


Dave
2006-11-05 05:45:26
Looks great! Main website is down - any news on when it will be back?
Nathaniel
2006-11-05 16:46:29
Just noticed it was down too. In the process of getting it back up.
Jens
2007-04-26 13:33:04
How does this deal with internationalization? Is it possible to add new strings in other languages, and also take care of common false friends like "quinze jours" (fifteen days) meaning two weeks in French?