Module: Calagator::EventsHelper

Includes:
TimeRangeHelper
Defined in:
app/helpers/calagator/events_helper.rb

Constant Summary collapse

GOOGLE_EVENT_SUBSCRIBE_BASE =
"http://www.google.com/calendar/render?cid="

Instance Method Summary collapse

Methods included from TimeRangeHelper

#normalize_time

Instance Method Details

#_events_feed_linker(filter = {}, common = {}) ⇒ String

Returns a URL for an events feed.

Parameters:

  • filter (Hash) (defaults to: {})

    Options for filtering. If values are defined, returns a link to all events. If a :query is defined, returns a link to search events’ text by that query. If a :tag is defined, returns a link to search events with that tag.

  • common (Hash) (defaults to: {})

    Options for the URL helper, such as :protocol, :format and such.

Returns:

  • (String)

    URL

Raises:

  • (ArgumentError)

    Raised if given invalid filter options.



80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'app/helpers/calagator/events_helper.rb', line 80

def _events_feed_linker(filter={}, common={})
  # Delete blank filter options because this method is typically called with
  # both a :tag and :query filter, but only one will actually be set.
  filter.delete_if { |key, value| value.blank? }

  if (unknown = filter.keys - [:query, :tag]).present?
    raise ArgumentError, "Unknown option(s): #{unknown.inspect}"
  end

  filter.present? ?
    search_events_url(common.merge(filter)) :
    events_url(common)
end

Returns an ATOM subscription URL.



121
122
123
# File 'app/helpers/calagator/events_helper.rb', line 121

def atom_feed_link(filter={})
  _events_feed_linker(filter, format: "atom")
end

#calculate_rowspans(events) ⇒ Object

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



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'app/helpers/calagator/events_helper.rb', line 27

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

  rowspans
end

#events_sort_label(key) ⇒ Object

Return a human-readable label describing what the sorting key is.



60
61
62
63
64
# File 'app/helpers/calagator/events_helper.rb', line 60

def events_sort_label(key)
  if key.present? or @tag.present?
    sanitize " by <strong>#{sorting_label_for(key, @tag.present?)}.</strong>"
  end
end

Return a link for sorting by key (e.g., “name”).



51
52
53
54
55
56
57
# File 'app/helpers/calagator/events_helper.rb', line 51

def events_sort_link(key)
  if key.present?
    link_to(sorting_label_for(key, @tag.present?), url_for(params.merge(:order => key)))
  else
    link_to('Default', url_for(params.tap { |o| o.delete :order }))
  end
end

#format_event_date(date) ⇒ Object

Cast date to_date unless date is undefined



13
14
15
# File 'app/helpers/calagator/events_helper.rb', line 13

def format_event_date(date)
  date ? date.to_date : ""
end

#format_event_time(date) ⇒ Object

Cast date to time unless date is undefined



18
19
20
# File 'app/helpers/calagator/events_helper.rb', line 18

def format_event_time(date)
  date ? date.strftime('%I:%M %p') : ""
end

Returns a Google Calendar subscription URL.



99
100
101
102
# File 'app/helpers/calagator/events_helper.rb', line 99

def google_events_subscription_link(filter={})
  link = _events_feed_linker(filter, format: "ics")
  "#{GOOGLE_EVENT_SUBSCRIBE_BASE}#{CGI.escape(link)}"
end

#google_maps_url(address) ⇒ Object



44
45
46
# File 'app/helpers/calagator/events_helper.rb', line 44

def google_maps_url(address)
  "http://maps.google.com/maps?q=#{URI.encode(address)}"
end

Returns an iCalendar export URL.



114
115
116
# File 'app/helpers/calagator/events_helper.rb', line 114

def icalendar_export_link(filter={})
  _events_feed_linker(filter, format: "ics")
end

Returns an iCalendar subscription URL.



107
108
109
# File 'app/helpers/calagator/events_helper.rb', line 107

def icalendar_feed_link(filter={})
  _events_feed_linker(filter, protocol: "webcal", format: "ics")
end

#shareable_event_url(event) ⇒ Object



161
162
163
# File 'app/helpers/calagator/events_helper.rb', line 161

def shareable_event_url(event)
  event_url(event) if event.persisted?
end

#sorting_label_for(sorting_key = nil, is_searching_by_tag = false) ⇒ Object

Return the label for the sorting_key (e.g. ‘score’). Optionally set the is_searching_by_tag, to constrain options available for tag searches.



169
170
171
172
173
174
175
176
177
178
# File 'app/helpers/calagator/events_helper.rb', line 169

def sorting_label_for(sorting_key=nil, is_searching_by_tag=false)
  sorting_key = sorting_key.to_s
  if sorting_key.present? and SORTING_LABELS.has_key?(sorting_key)
    SORTING_LABELS[sorting_key]
  elsif is_searching_by_tag
    SORTING_LABELS['date']
  else
    SORTING_LABELS['score']
  end
end

#today_tomorrow_or_weekday(event) ⇒ Object



6
7
8
9
10
# File 'app/helpers/calagator/events_helper.rb', line 6

def today_tomorrow_or_weekday(event)
  output = event.start_time.strftime('%A')
  output = "Started #{output}" if event.ongoing?
  output
end

#tweet_text(event) ⇒ Object

Tweet button text



129
130
131
132
133
134
135
136
137
138
# File 'app/helpers/calagator/events_helper.rb', line 129

def tweet_text(event)
  lengths = tweet_text_sizer(event)

  result = []
  result << "#{truncate(event.title, length: lengths[:title])} -"
  result << event.start_time.strftime("%I:%M%p %m.%d.%Y") # "04:00PM 08.01.2012"
  result << "@ #{truncate(event.venue.title, length: lengths[:venue])}" if event.venue

  result.join(" ")
end