Class: Event

Inherits:
ActiveRecord::Base
  • Object
show all
Includes:
ActionView::Helpers::DateHelper, ActionView::Helpers::TextHelper
Defined in:
app/models/event.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.as_monthsObject



118
119
120
121
122
123
124
125
126
127
128
# File 'app/models/event.rb', line 118

def self.as_months
  stack = {}
  find(:all).each_with_object({}) do |event, stack|
    y = event.start_date.year
    m = (I18n.t 'date.month_names')[event.start_date.month]
    stack[y] ||= {}
    stack[y][m] ||= []
    stack[y][m].push event
  end
  stack
end

.create_from(cal_event) ⇒ Object



369
370
371
372
373
374
375
376
# File 'app/models/event.rb', line 369

def self.create_from(cal_event)
  event = new({
    :uuid => cal_event.uid,
    :created_at => cal_event.dtstamp,
    :status_id => Status[:imported]
  })
  event.update_from(cal_event)
end

.futureObject



110
111
112
# File 'app/models/event.rb', line 110

def self.future
  after(Time.now)
end

.in_month(year, month) ⇒ Object

numbers. eg calendar.occurrences.in_month(2010, 12)



92
93
94
95
96
# File 'app/models/event.rb', line 92

def self.in_month(year, month)          # numbers. eg calendar.occurrences.in_month(2010, 12)
  start = DateTime.civil(year, month)
  finish = start + 1.month
  between(start, finish)
end

.in_the_last(period) ⇒ Object

seconds. eg calendar.occurrences.in_the_last(1.week)



80
81
82
83
84
# File 'app/models/event.rb', line 80

def self.in_the_last(period)           # seconds. eg calendar.occurrences.in_the_last(1.week)
  finish = Time.now
  start = finish - period
  between(start, finish)
end

.in_week(year, week) ⇒ Object

numbers, with a commercial week: eg calendar.occurrences.in_week(2010, 35)



98
99
100
101
102
# File 'app/models/event.rb', line 98

def self.in_week(year, week)            # numbers, with a commercial week: eg calendar.occurrences.in_week(2010, 35)
  start = DateTime.commercial(year, week)
  finish = start + 1.week
  between(start, finish)
end

.in_year(year) ⇒ Object

just a number. eg calendar.occurrences.in_year(2010)



86
87
88
89
90
# File 'app/models/event.rb', line 86

def self.in_year(year)                 # just a number. eg calendar.occurrences.in_year(2010)
  start = DateTime.civil(year)
  finish = start + 1.year
  between(start, finish)
end

.on_day(year, month, day) ⇒ Object

numbers: eg calendar.occurrences.on_day(2010, 12, 12)



104
105
106
107
108
# File 'app/models/event.rb', line 104

def self.on_day (year, month, day)      # numbers: eg calendar.occurrences.on_day(2010, 12, 12)
  start = DateTime.civil(year, month, day)
  finish = start + 1.day
  between(start, finish)
end

.pastObject



114
115
116
# File 'app/models/event.rb', line 114

def self.past
  before(Time.now)
end

Instance Method Details

#add_recurrence(rule) ⇒ Object



348
349
350
# File 'app/models/event.rb', line 348

def add_recurrence(rule)
  self.recurrence_rules << EventRecurrenceRule.from(rule)
end

#addressObject



146
147
148
# File 'app/models/event.rb', line 146

def address
  event_venue.address if event_venue
end

#categoryObject



130
131
132
# File 'app/models/event.rb', line 130

def category
  calendar.category if calendar
end

#continuing?Boolean

Returns:

  • (Boolean)


320
321
322
# File 'app/models/event.rb', line 320

def continuing?
  end_date && start_date < Time.now && end_date > Time.now
end

#dateObject



174
175
176
# File 'app/models/event.rb', line 174

def date
  I18n.l start_date, :format => date_format
end

#dayObject



190
191
192
# File 'app/models/event.rb', line 190

def day
  I18n.l start_date, :format => :calendar_day_name
end

#description_paragraphObject



154
155
156
157
158
159
160
# File 'app/models/event.rb', line 154

def description_paragraph
  if description =~ /\<p/
    description
  else
    "<p>#{description}</p>"
  end
end

#durationObject



222
223
224
225
226
227
228
# File 'app/models/event.rb', line 222

def duration
  if end_date
    end_date - start_date
  else
    0
  end
end

#editable?Boolean

Returns:

  • (Boolean)


328
329
330
# File 'app/models/event.rb', line 328

def editable?
  status != Status[:imported]
end

#end_timeObject



214
215
216
# File 'app/models/event.rb', line 214

def end_time
  (I18n.l end_date, :format => (end_date.min == 0 ? round_time_format : time_format)).downcase if end_date
end

#facebook_urlObject



288
289
290
# File 'app/models/event.rb', line 288

def facebook_url
  %{http://www.facebook.com/event.php?eid=#{facebook_id}} unless facebook_id.blank?
end

#finished?Boolean

Returns:

  • (Boolean)


324
325
326
# File 'app/models/event.rb', line 324

def finished?
  start_date < Time.now && (!end_date || end_date < Time.now)
end

#finishesObject



238
239
240
241
242
243
244
# File 'app/models/event.rb', line 238

def finishes
  if all_day?
    t('event_page.all_day')
  else
    end_time
  end
end

#icalObject



365
366
367
# File 'app/models/event.rb', line 365

def ical
  self.to_ri_cal.to_s
end

#in_this_month?(date) ⇒ Boolean

Returns:

  • (Boolean)


312
313
314
315
316
317
318
# File 'app/models/event.rb', line 312

def in_this_month?(date)
  if end_date
    start_date < date.end_of_month && end_date > date.beginning_of_month
  else
    start_date > date.beginning_of_month && start_date < date.end_of_month
  end
end

#last_dayObject



218
219
220
# File 'app/models/event.rb', line 218

def last_day
  I18n.l end_date, :format => date_format if end_date
end

#locationObject



138
139
140
141
142
143
144
# File 'app/models/event.rb', line 138

def location
  if event_venue
    event_venue.to_s
  else
    read_attribute(:location)
  end
end

#master?Boolean

Returns:

  • (Boolean)


166
167
168
# File 'app/models/event.rb', line 166

def master?
  master.nil?
end

#mdayObject



198
199
200
# File 'app/models/event.rb', line 198

def mday
  I18n.l start_date, :format => :calendar_day_of_month
end

#mday_paddedObject



202
203
204
# File 'app/models/event.rb', line 202

def mday_padded
  mday
end

#monthObject



178
179
180
# File 'app/models/event.rb', line 178

def month
  I18n.l start_date, :format => :calendar_month
end

#occurrence?Boolean

Returns:

  • (Boolean)


170
171
172
# File 'app/models/event.rb', line 170

def occurrence?
  !master?
end

#on_this_day?(date) ⇒ Boolean

sometimes we need to filter an existing list to get the day’s events usually in a radius tag, to avoid going back to the database for each day in a list so we call events.select {|e| e.on_this_day?(day) }

Returns:

  • (Boolean)


304
305
306
307
308
309
310
# File 'app/models/event.rb', line 304

def on_this_day?(date)
  if end_date
    start_date < date.end_of_day && end_date > date.beginning_of_day
  else
    start_date > date.beginning_of_day && start_date < date.end_of_day
  end
end

#one_day?Boolean

Returns:

  • (Boolean)


292
293
294
# File 'app/models/event.rb', line 292

def one_day?
  all_day? && within_day?
end

#postcodeObject



150
151
152
# File 'app/models/event.rb', line 150

def postcode
  event_venue.postcode if event_venue
end

#recurrenceObject



344
345
346
# File 'app/models/event.rb', line 344

def recurrence
  recurrence_rules.first.to_s
end

#recurs?Boolean

Returns:

  • (Boolean)


332
333
334
# File 'app/models/event.rb', line 332

def recurs?
  master || occurrences.any?
end

#short_dateObject



206
207
208
# File 'app/models/event.rb', line 206

def short_date
  I18n.l start_date, :format => short_date_format
end

#short_dayObject



194
195
196
# File 'app/models/event.rb', line 194

def short_day
  I18n.l start_date, :format => :calendar_short_day
end

#short_description(length = 256, ellipsis = "...") ⇒ Object



162
163
164
# File 'app/models/event.rb', line 162

def short_description(length=256, ellipsis="...")
  truncate(description, length, ellipsis)
end

#short_monthObject



182
183
184
# File 'app/models/event.rb', line 182

def short_month
  I18n.l start_date, :format => :calendar_short_month
end

#slugObject



134
135
136
# File 'app/models/event.rb', line 134

def slug
  calendar.slug if calendar
end

#start_timeObject



210
211
212
# File 'app/models/event.rb', line 210

def start_time
  (I18n.l start_date, :format => (start_date.min == 0 ? round_time_format : time_format)).downcase
end

#startsObject



230
231
232
233
234
235
236
# File 'app/models/event.rb', line 230

def starts
  if all_day?
    t('event_page.all_day')
  else
    start_time
  end
end

#statusObject



336
337
338
# File 'app/models/event.rb', line 336

def status
  Status.find(self.status_id)
end

#status=(value) ⇒ Object



340
341
342
# File 'app/models/event.rb', line 340

def status=(value)
  self.status_id = value.id
end

#summarize_periodObject



256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
# File 'app/models/event.rb', line 256

def summarize_period
  period = ""
  if one_day?
    period = (I18n.t 'event_page.summarize_period_one_day', :date => date)
  elsif all_day?
    period = (I18n.t 'event_page.summarize_period_all_day',
                      :from_date => date,
                      :to_date => (I18n.l end_date, :format => date_format))
  elsif within_day?
    if end_time
      period = (I18n.t 'event_page.summarize_period_within_day_with_end_time',
        :start_time => start_time, :end_time => end_time, :date => date)
    else
      period = (I18n.t 'event_page.summarize_period_within_day',
        :start_time => start_time, :date => date)
    end
  else
    period = (I18n.t 'event_page.summarize_period_spanning_days',
      :start_time => start_time, :end_time => end_time, :date => date,
      :end_date => (I18n.l end_date, :format => date_format))
  end
  period
end

#summarize_startObject



246
247
248
249
250
251
252
253
254
# File 'app/models/event.rb', line 246

def summarize_start
  if one_day?
    I18n.t 'event_page.all_day_on', :date => date
  elsif all_day?
    I18n.t 'event_page.from_date', :date => date
  else
    I18n.t 'event_page.on_date', :time => start_time, :date => date
  end
end

#to_ri_calObject



352
353
354
355
356
357
358
359
360
361
362
363
# File 'app/models/event.rb', line 352

def to_ri_cal
  RiCal.Event do |cal_event|
    cal_event.uid = uuid
    cal_event.summary = title
    cal_event.description = description if description
    cal_event.dtstart =  (all_day? ? start_date.to_date : start_date) if start_date
    cal_event.dtend = (all_day? ? end_date.to_date : end_date) if end_date
    cal_event.url = url if url
    cal_event.rrules = recurrence_rules.map(&:to_ical) if recurrence_rules.any?
    cal_event.location = location if location
  end
end

#update_from(cal_event) ⇒ Object



378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
# File 'app/models/event.rb', line 378

def update_from(cal_event)
  #TODO handle and merge local updates
  self.update_attributes({
    :title => cal_event.summary,
    :description => cal_event.description,
    :url => cal_event.url,
    :start_date => cal_event.dtstart,
    :end_date => cal_event.dtend,
    :all_day => !cal_event.dtstart.is_a?(DateTime)
  })
  unless self.event_venue = EventVenue.find_by_title(cal_event.location)
    self.location = cal_event.location
  end
  cal_event.rrule.each { |rule| self.add_recurrence(rule) }
  self.save!
  self
rescue => error
  Rails.logger.error "Event import error: #{error}."
  raise
end

#urlObject



280
281
282
283
284
285
286
# File 'app/models/event.rb', line 280

def url
  if url = read_attribute(:url)
    return nil if url.blank?
    url = "http://#{url}" unless url =~ /^(http:\/){0,1}\//
    url.strip
  end
end

#within_day?Boolean

Returns:

  • (Boolean)


296
297
298
# File 'app/models/event.rb', line 296

def within_day?
  (!end_date || start_date.to_date.jd == end_date.to_date.jd || end_date == start_date + 1.day)
end

#yearObject



186
187
188
# File 'app/models/event.rb', line 186

def year
  start_date.year
end