Module: EventCalendar::ClassMethods
- Defined in:
- lib/event_calendar.rb
Instance Method Summary collapse
- #beginning_of_week(date, start = 0) ⇒ Object
-
#create_event_strips(strip_start, strip_end, events) ⇒ Object
Create the various strips that show events.
- #days_between(first, second) ⇒ Object
-
#event_strips_for_month(shown_date, first_day_of_week = 0, find_options = {}) ⇒ Object
For the given month, find the start and end dates Find all the events within this range, and create event strips for them.
-
#events_for_date_range(start_d, end_d, find_options = {}) ⇒ Object
Get the events overlapping the given start and end dates.
-
#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.
- #has_event_calendar(options = {}) ⇒ Object
- #space_in_current_strips?(event_strips, range) ⇒ Boolean
Instance Method Details
#beginning_of_week(date, start = 0) ⇒ Object
120 121 122 123 |
# File 'lib/event_calendar.rb', line 120 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 events.
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 86 87 88 89 90 |
# File 'lib/event_calendar.rb', line 61 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
112 113 114 115 116 117 118 |
# File 'lib/event_calendar.rb', line 112 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, find_options = {}) ⇒ Object
For the given month, find the start and end dates Find all the events within this range, and create event strips for them
22 23 24 25 26 27 28 29 30 31 |
# File 'lib/event_calendar.rb', line 22 def event_strips_for_month(shown_date, first_day_of_week=0, = {}) if first_day_of_week.is_a?(Hash) .merge!(first_day_of_week) first_day_of_week = 0 end 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, find_options = {}) ⇒ Object
Get the events overlapping the given start and end dates
52 53 54 55 56 57 58 |
# File 'lib/event_calendar.rb', line 52 def events_for_date_range(start_d, end_d, = {}) self.scoped().find( :all, :conditions => [ "(? <= #{self.end_at_field}) AND (#{self.start_at_field}< ?)", start_d.to_time.utc, end_d.to_time.utc ], :order => "#{self.start_at_field} 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
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/event_calendar.rb', line 35 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 |
#has_event_calendar(options = {}) ⇒ Object
10 11 12 13 14 15 16 17 18 |
# File 'lib/event_calendar.rb', line 10 def has_event_calendar(={}) cattr_accessor :start_at_field, :end_at_field self.start_at_field = ( [:start_at_field] ||= :start_at).to_s self.end_at_field = ( [:end_at_field] ||= :end_at ).to_s alias_attribute :start_at, start_at_field unless start_at_field == 'start_at' alias_attribute :end_at, end_at_field unless end_at_field == 'end_at' before_save :adjust_all_day_dates send :include, InstanceMethods end |
#space_in_current_strips?(event_strips, range) ⇒ Boolean
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/event_calendar.rb', line 92 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 |