Class: EventCalendar

Inherits:
Object
  • Object
show all
Extended by:
ActiveSupport::Memoizable
Defined in:
lib/event_calendar.rb,
lib/event_calendar/week.rb,
lib/event_calendar/event.rb

Overview

Generates HTML calendars

Defined Under Namespace

Classes: Event, Week

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(year = Time.now.year, month = Time.now.month, options = {}) {|_self| ... } ⇒ EventCalendar

Optionally accepts a year as the first argument, a month (integer) as the second argument, and a hash of options as the third argument. It also accepts a block which it passes itself to.

For example:

@event_calendar = EventCalendar.new

@event_calendar = EventCalendar.new(2009, 10, :id => 'calendar', :events => Event.all)

@event_calendar = EventCalendar.new(2009, 10) do |c|
  c.id = 'calendar'
  c.events = Event.all
end

Yields:

  • (_self)

Yield Parameters:

  • _self (EventCalendar)

    the object that the method was called on



82
83
84
85
86
# File 'lib/event_calendar.rb', line 82

def initialize(year = Time.now.year, month = Time.now.month, options = {})
  @year, @month, self.options = year, month, self.class.default_options.merge(options)
  @events = self.options.delete(:events).collect { |event| Event.new(event, self.options) }.sort_by(&:start)
  yield self if block_given?
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args) ⇒ Object

Allows you to read and write options using method notation.

For example:

@event_calendar = EventCalendar.new(2009, 10)
@event_calendar.template = '/path/to/some/other/template.mab'
puts @event_calendar.beginning_of_week


132
133
134
135
136
137
138
139
# File 'lib/event_calendar.rb', line 132

def method_missing(method, *args)
  if method.to_s =~ /^([^=]+)(=?)$/ && options.has_key?($1.to_sym)
    options[$1.to_sym] = args.first unless $2.empty?
    options[$1.to_sym]
  else
    super
  end
end

Instance Attribute Details

#eventsObject

Returns the value of attribute events.



17
18
19
# File 'lib/event_calendar.rb', line 17

def events
  @events
end

#monthObject (readonly)

Returns the value of attribute month.



18
19
20
# File 'lib/event_calendar.rb', line 18

def month
  @month
end

#optionsObject

Returns the value of attribute options.



17
18
19
# File 'lib/event_calendar.rb', line 17

def options
  @options
end

#yearObject (readonly)

Returns the value of attribute year.



18
19
20
# File 'lib/event_calendar.rb', line 18

def year
  @year
end

Class Method Details

.default_optionsObject

The default options used when generating event calendars

:id                  =>  The HTML id of the generated container div for this event calendar. Defaults to 'event_calendar'.
:beginning_of_week   =>  The day number to use as the beginning of the week. For example, 0 is for Sunday, 1 is for Monday, etc.
                         Defaults to 0.
:day_label *         =>  The label to use for each day. Defaults to the day number.
:event_class **      =>  The HTML class to add to each event. Defaults to nil.
:event_id **         =>  The id (object_id or database id) of an event. Defaults to :id.
:event_title **      =>  The title of an event. Defaults to :title.
:event_start **      =>  The start date or datetime of an event. Defaults to :starts_at.
:event_end **        =>  The end date or datetime of an event. Defaults to :ends_at.
:event_url **        =>  The url of an event to use as the HTML href attribute. Defaults to '#'.
:event_output **     =>  The HTML to output for an event. Defaults to a link to the :event_url using the :event_title
                         as its label and title attributes.
:event_fields **     =>  The event fields to output as hidden attributes into the calendar so that they can be accessed with
                         javascript. Defaults to [:id, :title, :start, :end].
:events              =>  An array of events to display in the calendar. Defaults to [].
:header_label *      =>  The label to use as the header of the calendar. Defaults to the month name and year like 'October 2009'.
:header_day_label *  =>  The label to use as the header of each day. Defaults to the abbreviated day name like 'Thu'.
:navigation_label *  =>  The label to use as the inner HTML for the navigation links. Defaults to the full month name like 'November'.
:navigation_url      =>  A proc which returns the url to use as the HTML href attribute for the previous and next month links. 
                         This proc is passed a date object representing the first day of the previous or next months. Defaults to '#'.
:template            =>  A path to the Markaby template file to use when rendering the calendar. Defaults to the 'event_calendar/template.mab'
                         file found in this directory.
  • See the EventCalendar.evaluate_date_format_option method for possible values.

** See the Event#evaluate_option method for possible values.



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/event_calendar.rb', line 47

def self.default_options
  @default_options ||= {
    :id                 => 'event_calendar',
    :beginning_of_week  => 0,
    :day_label          => proc { |date| date.strftime('%d').gsub(/^0/, '') },
    :event_class        => nil,
    :event_id           => :id,
    :event_title        => :title,
    :event_start        => :starts_at,
    :event_end          => :ends_at,
    :event_url          => '#',
    :event_output       => proc { |event| "<a href=\"#{event.url}\" title=\"#{event.title}\">#{event.title}</a>" },
    :event_fields       => [:id, :title, :start, :end],
    :events             => [],
    :header_label       => '%B %Y',
    :header_day_label   => '%a',
    :navigation_label   => '%B',
    :navigation_url     => proc { |date| '#' },
    :template           => File.join(File.dirname(__FILE__), 'event_calendar', 'template.mab')
  }
end

Instance Method Details

#dateObject

Returns a date object representing the first day of this EventCalendar instance’s specified year and month.



89
90
91
# File 'lib/event_calendar.rb', line 89

def date
  Date.civil(year, month, 1)
end

#evaluate_date_format_option(option, *args) ⇒ Object

Looks up the specified option representing a date format and returns the evaluated the result.

This is used inside the Markaby template. For example, we have an option :header_label which represents the content at the top of the calendar that outputs the month name and year by default, like “October 2009”.

Inside the template, we call event_calendar.evaluate_date_format_option(:header_label, event_calendar.date).

If options[:header_label] is a string, it takes event_calendar.date and calls strftime(options[:header_label]) on it.

If it’s a symbol, it takes event_calendar.date and calls send(options[:header_label]) on it.

If it’s a proc, it calls the proc and passes event_calendar.date to it. You can pass any number of args to this method and they’ll passed to the proc.

Any other value for options[:header_label] is simply returned.



111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/event_calendar.rb', line 111

def evaluate_date_format_option(option, *args)
  value = self.send(option)
  case value
    when String
      args.first.strftime(value)
    when Symbol
      args.first.send(value)
    when Proc
      value.call(*args)
    else
      value
  end
end

#to_sObject Also known as: to_html

Returns the HTML representation of this EventCalendar.

aliased as to_html



144
145
146
# File 'lib/event_calendar.rb', line 144

def to_s
  render
end

#weeksObject

Returns an array of week objects which contain date objects for every day that this calendar displays. It may contain a few days from the previous and next months.

The EventCalendar::Week class inherits from Array.

For example:

puts EventCalendar.new(2009, 10).weeks.inspect

# [
#   [Sun, 27 Sep 2009, Mon, 28 Sep 2009, Tue, 29 Sep 2009, Wed, 30 Sep 2009, Thu, 01 Oct 2009, Fri, 02 Oct 2009, Sat, 03 Oct 2009],
#   [Sun, 04 Oct 2009, Mon, 05 Oct 2009, Tue, 06 Oct 2009, Wed, 07 Oct 2009, Thu, 08 Oct 2009, Fri, 09 Oct 2009, Sat, 10 Oct 2009],
#   [Sun, 11 Oct 2009, Mon, 12 Oct 2009, Tue, 13 Oct 2009, Wed, 14 Oct 2009, Thu, 15 Oct 2009, Fri, 16 Oct 2009, Sat, 17 Oct 2009],
#   [Sun, 18 Oct 2009, Mon, 19 Oct 2009, Tue, 20 Oct 2009, Wed, 21 Oct 2009, Thu, 22 Oct 2009, Fri, 23 Oct 2009, Sat, 24 Oct 2009],
#   [Sun, 25 Oct 2009, Mon, 26 Oct 2009, Tue, 27 Oct 2009, Wed, 28 Oct 2009, Thu, 29 Oct 2009, Fri, 30 Oct 2009, Sat, 31 Oct 2009]
# ]


165
166
167
168
169
170
# File 'lib/event_calendar.rb', line 165

def weeks
  days_in_month = Time.days_in_month(month, year)
  starting_day = date.beginning_of_week() -1.day + beginning_of_week.days
  ending_day = (date + days_in_month).end_of_week() -1.day + beginning_of_week.days
  (starting_day..ending_day).to_a.in_groups_of(7).collect { |week| Week.new(week, events) }
end