1. ActiveCalendar - Javascript Calendar on Rails

    Background

    When I first started with Rails I was amazed at how quickly you can get a project up and running. Coming from a Java background I was used to configuring either Hibernate, then Spring or spending my days tweaking EJB interfaces. The scaffolding in Rails blew me away. However, the basic date and date time renderer, as I’m sure you will agree, left a little something to be desired. It’s easy to change them after the scaffold was created, but that gets old quick. To solve these problems and others, I created ActiveCalendar.

    Development

    The first part of the process entailed choosing the look of the date renderer. I’ve always liked the DHTML / JavaScript Calendar from Dynarch. Since it is licensed under the LGPL, it was a suitable choice.

    Next, I had to write the plugin. I didn’t want a generator because this plugin is designed to be a drop in replacement for the date renderer. It’s not something that has to be generated per scaffold, or configured per field, so I used rails to generate a basic plugin.

    Next, I added a public directory where I stored the necessary images and JavaScript and removed all the unused directories and files. In the end I had the following directory structure:

    After creating the directory structure, it was time to open the hood and have a look at how rails renders fields. After a little digging, I found the FormHelper module in the following file:

    This quickly led me to the DateHelper in:

    Inside of the DateHelper module are methods called date_select and datetime_select, which correspond the methods used in the generated new.rhtml and edit.rhtml files. It should be noted that in the first iteration of the plugin, this is where the digging stopped. I overloaded the methods above with my own that rendered the Dynarch calendar. It worked wonderfully, until I discovered ActiveScaffold.

    ActiveScaffold is a great plugin that replaces the standard rails CRUD pages with a set of sexy AJAX powered pages. However, it uses a different method to render dates, so it was back to drop downs for me. Since I wanted a drop in replacement that would work with both standard Rails and ActiveScaffold, I was forced to dig deeper into the DateHelper.

    It turns out that the DateHelper, like the other FormHelpers, delegates calls to the InstanceTag class. In the final version of the plugin, I overloaded the to_date_select_tag and to_datetime_select_tag methods in InstanceTag and had them call my own DepotDateHelper class. ActiveScaffold must use the same InstanceTag class because once I did that, the calendar began rendering in ActiveScaffold as well.

    For good measure, I overloaded the date_select and datetime_select methods of DateHelper and continued to delegate to InstanceTag, but I added a couple of my own options.

    Installation

    You can install the plugin with the following command:

    Next add the appropriate javascript and stylesheets to your layout:

    That’s it, your dates should now render as JavaScript calendars.

    Screen Shot