twitterlink facebooklink feedlink

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 ...

A weekly calendar plugin for rails

Posted by semantosoph on Jul 29, 2009 | 2 comments

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.

Installing

This plugin is really easy to install. Just download it from GitHub and place it into the vendor/plugins directory.

Usage

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

Options

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).

A word about details()

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.

Gravatar image uma mageshwari wrote on March 26, 2010 I tried with calling in views as schedule() . It showing error like this
  You have a nil object when you didn't expect it!
The error occurred while evaluating nil.now

Extracted source (around line #104):

101:                   </div>
102: 
103:                 </div><%end%><%end%>
104:                 <%= schedule()
105: %>
106: 
107:             <!--sunday specials list end-->

RAILS_ROOT: /home/backup/sayaglify/front
Application Trace | Framework Trace | Full Trace

vendor/plugins/semantosoph-weekly_schedule-1cf7e9f/lib/weekly_schedule.rb:72:in `schedule'
/opt/jruby-1.1.6/lib/ruby/1.8/date.rb:1311:in `step'
/opt/jruby-1.1.6/lib/ruby/1.8/date.rb:1320:in `upto'
vendor/plugins/semantosoph-weekly_schedule-1cf7e9f/lib/weekly_schedule.rb:67:in `schedule'
app/views/preorder/index.html.erb:104
Gravatar image Sven wrote on March 26, 2010

Uma,

thanks for your bug report. I removed the useless call for Time.zone. Please re-download the code from github.

Post your comment