forked from calagator/calagator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
events_helper.rb
60 lines (50 loc) · 2.15 KB
/
events_helper.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
module EventsHelper
include TimeRangeHelper # provides normalize_time
GOOGLE_TIME_FORMAT = "%Y%m%dT%H%M%SZ"
FRIENDLY_SORT_LABELS = {'name' => 'Event Name', 'venue' => 'Location', 'score' => 'Relevance'}
def today_tomorrow_or_weekday(record)
# TODO Figure out if there's any need for this method beyond having a way of conditionally displaying the 'Started' information. As far as I can tell, there's no need to display the 'Today' or 'Tomorrow' rather than the weekday because each event already has a header to its left that can say 'Today' or 'Tomorrow'.
# if record.start_time.to_date == Time.today.to_date
# 'Today'
# elsif record.start_time.to_date == (Time.today+1.day).to_date
# 'Tomorrow'
# elsif record.ongoing?
if record.ongoing?
"Started #{record.start_time.strftime('%A')}"
else
record.start_time.strftime('%A')
end
end
# calculate rowspans for an array of events
# argument: array of events
# returns: rowspans, an array in which each entry corresponds to an event in events;
# each entry is number of rows spanned by today_tomorrow_weekday entry, if any, to left of event
# entry will be > 0 for first event of day, 0 for other events
def calculate_rowspans(events)
previous_start_time = nil
rowspans = Array.new(events.size, 0)
first_event_of_day = 0
events.each_with_index do |event, index|
new_day = previous_start_time.nil? || (previous_start_time.to_date != event.start_time.to_date)
if new_day
first_event_of_day = index
end
rowspans[first_event_of_day] += 1
previous_start_time = event.start_time
end
return rowspans
end
def google_maps_url(address)
return "http://maps.google.com/maps?q=#{CGI::escape(address)}"
end
def format_google_timespan( event)
end_time = event.end_time || event.start_time
"#{event.start_time.utc.strftime(GOOGLE_TIME_FORMAT)}/#{end_time.utc.strftime(GOOGLE_TIME_FORMAT)}"
end
def events_sort_link(key)
link_to FRIENDLY_SORT_LABELS[key], url_for(params.merge(:order => key))
end
def events_sort_label(key)
" by <strong>#{(FRIENDLY_SORT_LABELS[key] || "date").downcase}.</strong>"
end
end