Module: Calagator::EventsHelper

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

Constant Summary collapse

GOOGLE_EVENT_SUBSCRIBE_BASE =
'https://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.



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

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 - %i[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.



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

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



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

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)
    first_event_of_day = index if new_day
    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.



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

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

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



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

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

#format_event_date(date) ⇒ Object

Cast date to_date unless date is undefined



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

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

#format_event_time(date) ⇒ Object

Cast date to time unless date is undefined



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

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

Returns a Google Calendar subscription URL.



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

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

#google_maps_url(address) ⇒ Object



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

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

Returns an iCalendar export URL.



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

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

Returns an iCalendar subscription URL.



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

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

#shareable_event_url(event) ⇒ Object



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

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.



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

def sorting_label_for(sorting_key = nil, is_searching_by_tag = false)
  sorting_key = sorting_key.to_s
  if sorting_key.present? && SORTING_LABELS.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



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

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



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

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"
  if event.venue
    result << "@ #{truncate(event.venue.title, length: lengths[:venue])}"
  end

  result.join(' ')
end