Semantics, Code and Circumstance
April 10, 2010: This afternoon I recieved a visit from a plushy black cat. I've never seen her before and since she has a collar she may be moved in with her owner in the last days.
Read more about project 365 ...Something that bugged me with its non-existence in the past was a rails plugin that would display a weekly calendar, while being highly configurable. And by saying highly configurable, I mean it should have options for its look, its operation and, beyond that, it should accept any kind of data to display. As there was no such plugin, I did it myself. So today, I proudly present you the weekly_schedule plugin for rails.
This plugin is really easy to install. Just download it from GitHub and place it into the vendor/plugins directory.
The simplest possible call in your code would be
schedule()
which generates a plain calendar (which can then be customized using CSS) for the current week. However, this may be customized in a variety of ways—changing the default CSS classes, populating the individual day entries with appointments, and so on. Generating a calendar that shows the whole christmas week 2009, starting from monday, would require the following call:
schedule(:day => Date.civil(2009, 12, 25), :first_day_of_week => 1)
A more typical use case for schedules is to show what happening today or the very next time. The following code generates a calendar that always starts with today:
schedule(:first_day_of_week => Date.today.cwday)
A more convenient variant of this is the following:
schedule(:first_day_of_week => Date.today.cwday - 3)
This calendar starts three days earlier and therefore keeps today right in the middle of the display
As I said earlier in this post, I wanted this plugin to be as customizable as possible. So it is now packed with different options for the CSS classes:
:table_class
:month_name_class
:day_name_class
:day_class
An additional ‘weekend’ class is applied to weekend days.
The following options are available for customizing the behaviour of the calendar:
:day
This option specifies the day, whose week shall be displayed. Defaults to Date.today
:abbrev
This option specifies how the day names should be abbreviated. Use (0..2) for the first three letters, (0..0) for the first, and (0..-1) for the entire name.
:first_day_of_week
This renders a calendar starting on a specific weekday. Use 1 for Monday up to 7 for Sunday. This option applies only if :only_workdays is set to false.
:show_today
Highlights today on the calendar using the CSS class ‘today’. Defaults to true.
:only_workdays
Turns off weekend display. Defaults to false. When turned on, the value of :first_day_of_week is fixed to 1 (monday). If :day is a saturday or sunday, the following week is shown.
:show
Now, this gives us a hint where to search for info to show and is handed over to details(). Defaults to nil (showing no info).
This function evaluates if something is given in in the :show option. The following should give you a rough idea how this may be useful. Imagine, you’ve given @this_weeks_appointments as an array via the :show option. The value in date is the currently processed day as a Date object.
def details(date, options)
if options[:show]
appointments = options[:show].select{|a| a.date == date}.sort_by{|t| t.time}
appointments.map{|a| "#{a.time} #{a.place}"}
else
'Nothing to do today'
end
end
Feel free to download the plugin from GitHub. If you have some comment or a great idea, please let me know.
Uma,
thanks for your bug report. I removed the useless call for Time.zone. Please re-download the code from github.