Module: EventCalendar::ClassMethods

Defined in:
lib/event_calendar.rb

Overview

class Methods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.setup_event_calendar_on(recipient) ⇒ Object



12
13
14
15
16
17
# File 'lib/event_calendar.rb', line 12

def self.setup_event_calendar_on(recipient)
  recipient.extend ClassMethods
  recipient.class_eval do
    include InstanceMethods
  end
end

Instance Method Details

#beginning_of_week(date, start = 0) ⇒ Object



115
116
117
118
# File 'lib/event_calendar.rb', line 115

def beginning_of_week(date, start = 0)
  days_to_beg = days_between(start, date.wday)
  date - days_to_beg
end

#create_event_strips(strip_start, strip_end, events) ⇒ Object

Create the various strips that show evetns



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/event_calendar.rb', line 56

def create_event_strips(strip_start, strip_end, events)
  # create an inital event strip, with a nil entry for every day of the displayed days
  event_strips = [[nil] * (strip_end - strip_start + 1)]

  events.each do |event|
    cur_date = event.start_at.to_date
    end_date = event.end_at.to_date
    cur_date, end_date = event.clip_range(strip_start, strip_end)
    start_range = (cur_date - strip_start).to_i
    end_range = (end_date - strip_start).to_i
  
    # make sure the event is within our viewing range
    if (start_range <= end_range) and (end_range >= 0) 
      range = start_range..end_range
      
      open_strip = space_in_current_strips?(event_strips, range)
      
      if open_strip.nil?
        # no strips open, make a new one
        new_strip = [nil] * (strip_end - strip_start + 1)
        range.each {|r| new_strip[r] = event}
        event_strips << new_strip
      else
        # found an open strip, add this event to it
        range.each {|r| open_strip[r] = event}
      end
    end
  end
  event_strips
end

#days_between(first, second) ⇒ Object



107
108
109
110
111
112
113
# File 'lib/event_calendar.rb', line 107

def days_between(first, second)
  if first > second
    second + (7 - first)
  else
    second - first
  end
end

#event_strips_for_month(shown_date, first_day_of_week = 0) ⇒ Object

For the given month, find the start and end dates Find all the events within this range, and create event strips for them



21
22
23
24
25
26
# File 'lib/event_calendar.rb', line 21

def event_strips_for_month(shown_date, first_day_of_week=0)
  strip_start, strip_end = get_start_and_end_dates(shown_date, first_day_of_week)
  events = events_for_date_range(strip_start, strip_end)
  event_strips = create_event_strips(strip_start, strip_end, events)
  event_strips
end

#events_for_date_range(start_d, end_d) ⇒ Object

Get the events overlapping the given start and end dates



47
48
49
50
51
52
53
# File 'lib/event_calendar.rb', line 47

def events_for_date_range(start_d, end_d)
  self.find(
    :all,
    :conditions => [ '(? <= end_at) AND (start_at < ?)', start_d.to_time.utc, end_d.to_time.utc ],
    :order => 'start_at ASC'
  )
end

#get_start_and_end_dates(shown_date, first_day_of_week = 0) ⇒ Object

Expand start and end dates to show the previous month and next month’s days, that overlap with the shown months display



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/event_calendar.rb', line 30

def get_start_and_end_dates(shown_date, first_day_of_week=0)
  # start with the first day of the given month
  start_of_month = Date.civil(shown_date.year, shown_date.month, 1)
  # the end of last month
  strip_start = beginning_of_week(start_of_month, first_day_of_week)
  # the beginning of next month, unless this month ended evenly on the last day of the week
  if start_of_month.next_month == beginning_of_week(start_of_month.next_month, first_day_of_week)
    # last day of the month is also the last day of the week
    strip_end = start_of_month.next_month
  else
    # add the extra days from next month
    strip_end = beginning_of_week(start_of_month.next_month + 7, first_day_of_week)
  end
  [strip_start, strip_end]
end

#space_in_current_strips?(event_strips, range) ⇒ Boolean

Returns:

  • (Boolean)


87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/event_calendar.rb', line 87

def space_in_current_strips?(event_strips, range)
  open_strip = nil
  for strip in event_strips
    strip_is_open = true
    range.each do |r|
      # overlapping events on this strip
      if !strip[r].nil?
        strip_is_open = false
        break
      end
    end

    if strip_is_open
      open_strip = strip
      break
    end
  end
  open_strip
end