Module: ScheduleFu::CalendarHelper

Defined in:
lib/schedule_fu/calendar_helper.rb

Instance Method Summary collapse

Instance Method Details

#calendar(options = {}, &block) ⇒ Object

Returns an HTML calendar. In its simplest form, this method generates a plain calendar (which can then be customized using CSS) for a given month and year. However, this may be customized in a variety of ways -- changing the default CSS classes, generating the individual day entries yourself, and so on.

The following options are required: :year # The year number to show the calendar for. :month # The month number to show the calendar for.

The following are optional, available for customizing the default behaviour:

:table_class       => "calendar"        # The class for the  tag.
:month_name_class  => "monthName"       # The class for the name of the month, at the top of the table.
:other_month_class => "otherMonth" # Not implemented yet.
:day_name_class    => "dayName"         # The class is for the names of the weekdays, at the top.
:day_class         => "day"             # The class for the individual day number cells.
                                        This may or may not be used if you specify a block (see below).
:abbrev            => (0..2)            # 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 => 0                 # Renders calendar starting on Sunday. Use 1 for Monday, and so on.
:accessible        => true              # Turns on accessibility mode. This suffixes dates within the
                                      # calendar that are outside the range defined in the 
with # # Defaults to false. # You'll need to define an appropriate style in order to make this disappear. # Choose your own method of hiding content appropriately. :show_today => false # Highlights today on the calendar using the CSS class 'today'. # Defaults to true. :previous_month_text => nil # Displayed left of the month name if set :next_month_text => nil # Displayed right of the month name if set :month_name_array => Date::MONTHNAMES # Array of months :display_year => options[:year] # Year to display

For more customization, you can pass a code block to this method, that will get one argument, a Date object, and return a values for the individual table cells. The block can return an array, [cell_text, cell_attrs], cell_text being the text that is displayed and cell_attrs a hash containing the attributes for the

tag (this can be used to change the 's class for customization with CSS). This block can also return the cell_text only, in which case the 's class defaults to the value given in :day_class. If the block returns nil, the default options are used.

Example usage:

calendar(:year => 2005, :month => 6) # This generates the simplest possible calendar.
calendar({:year => 2005, :month => 6, :table_class => "calendar_helper"}) # This generates a calendar, as
                                                                        # before, but the <table>'s class
                                                                        # is set to "calendar_helper".
calendar(:year => 2005, :month => 6, :abbrev => (0..-1)) # This generates a simple calendar but shows the
                                                       # entire day name ("Sunday", "Monday", etc.) instead
                                                       # of only the first three letters.
calendar(:year => 2005, :month => 5) do |d| # This generates a simple calendar, but gives special days
if listOfSpecialDays.include?(d)          # (days that are in the array listOfSpecialDays) one CSS class,
  [d.mday, {:class => "specialDay"}]      # "specialDay", and gives the rest of the days another CSS class,
else                                      # "normalDay". You can also use this highlight today differently
  [d.mday, {:class => "normalDay"}]       # from the rest of the days, etc.
end
end

An additional 'weekend' class is applied to weekend days.

For consistency with the themes provided in the calendar_styles generator, use "specialDay" as the CSS class for marked days.

Raises:

  • (ArgumentError)


68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/schedule_fu/calendar_helper.rb', line 68

def calendar(options = {}, &block)
  raise(ArgumentError, "No year given")  unless options.has_key?(:year)
  raise(ArgumentError, "No month given") unless options.has_key?(:month)
  
  block ||= Proc.new {|d| nil}
  
  options = defaults(options).merge(options)
  vars = setup_variables(options)
  
  (:table, :class => options[:table_class], :border => 0, 
      :cellspacing => 0, :cellpadding => 0) do
    text = calendar_head(options, vars).html_safe
    text << calendar_body(options, vars, &block).html_safe
  end
end