Class: Droom::Event

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/droom/event.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.falling_within(span) ⇒ Object

Chronic::Span



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

def self.falling_within(span)           # Chronic::Span
  coincident_with(span.begin, span.end)
end

.futureObject



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

def self.future
  unbegun.order('start ASC')
end

.in_month(year, month) ⇒ Object

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



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

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_span(span) ⇒ Object

Chronic::Span



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

def self.in_span(span)                  # Chronic::Span
  between(span.begin, span.end)
end

.in_the_last(period) ⇒ Object

All of these class methods also return scopes.



136
137
138
139
140
# File 'app/models/droom/event.rb', line 136

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)



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

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)



142
143
144
145
146
# File 'app/models/droom/event.rb', line 142

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)



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

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



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

def self.past
  finished.order('start DESC')
end

Instance Method Details

#add_recurrence(rule) ⇒ Object



338
339
340
# File 'app/models/droom/event.rb', line 338

def add_recurrence(rule)
  self.recurrence_rules << Droom::RecurrenceRule.from(rule)
end

#all_privateObject

Event retrieval in various ways

Events differ from other models in that they are visible to all unless marked ‘private’. The documents attached to them are only visible to all if marked ‘public’.



55
# File 'app/models/droom/event.rb', line 55

scope :all_private, where("private = 1")

#as_json(options = {}) ⇒ Object



367
368
369
370
371
# File 'app/models/droom/event.rb', line 367

def as_json(options={})
  json = super
  json[:datestring] = I18n.l start, :format => :natural_with_date
  json
end

#as_search_resultObject



382
383
384
385
386
387
388
389
# File 'app/models/droom/event.rb', line 382

def as_search_result
  {
    :type => 'event',
    :prompt => name,
    :value => name,
    :id => id
  }
end

#as_suggestionObject



373
374
375
376
377
378
379
380
# File 'app/models/droom/event.rb', line 373

def as_suggestion
  {
    :type => 'event',
    :prompt => name,
    :value => name,
    :id => id
  }
end

#attach(doc) ⇒ Object



188
189
190
# File 'app/models/droom/event.rb', line 188

def attach(doc)
  self.documents << doc
end

#attended_by?(person) ⇒ Boolean

Returns:

  • (Boolean)


286
287
288
# File 'app/models/droom/event.rb', line 286

def attended_by?(person)
  person && person.invited_to?(self)
end

#categories_for_selectionObject



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

def categories_for_selection
  cats = categories.map{|c| [c.name, c.id] }
  cats.unshift(['', ''])
  cats
end

#continuing?Boolean

Returns:

  • (Boolean)


322
323
324
# File 'app/models/droom/event.rb', line 322

def continuing?
  finish && start < Time.now && finish > Time.now
end

#detail_visible_to?(user_or_person) ⇒ Boolean

Returns:

  • (Boolean)


296
297
298
299
300
301
302
303
304
# File 'app/models/droom/event.rb', line 296

def detail_visible_to?(user_or_person)
  return true if self.public?
  return false unless user_or_person
  return true if user_or_person.admin?
  return true if user_or_person.privileged?
  return true if user_or_person.person.invited_to?(self)
  return false if self.private?
  return true
end

#durationObject



260
261
262
263
264
265
266
# File 'app/models/droom/event.rb', line 260

def duration
  if finish
    finish - start
  else
    0
  end
end

#find_or_create_agenda_category(category) ⇒ Object



276
277
278
# File 'app/models/droom/event.rb', line 276

def find_or_create_agenda_category(category)
  agenda_categories.find_or_create_by_category_id(category.id)
end

#finish=(value) ⇒ Object



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

def finish=(value)
  write_attribute :finish, parse_date(value)
end

#finish_dateObject



226
227
228
# File 'app/models/droom/event.rb', line 226

def finish_date
  finish.to_date if finish
end

#finish_date=(value) ⇒ Object



256
257
258
# File 'app/models/droom/event.rb', line 256

def finish_date=(value)
  self.finish = parse_date(value).to_date# + finish_time
end

#finish_timeObject



222
223
224
# File 'app/models/droom/event.rb', line 222

def finish_time
  finish.time_of_day if finish
end

#finish_time=(value) ⇒ Object



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

def finish_time=(value)
  if value && !value.blank?
    time_portion = parse_date(value).seconds_since_midnight
    self.finish = (finish_date || start_date || Date.today).to_time + time_portion
  end
end

#finished?Boolean

Returns:

  • (Boolean)


326
327
328
# File 'app/models/droom/event.rb', line 326

def finished?
  start < Time.now && (!finish || finish < Time.now)
end

#has_documents?Boolean

Returns:

  • (Boolean)


310
311
312
# File 'app/models/droom/event.rb', line 310

def has_documents?
  all_documents.any?
end

#has_people?Boolean

Returns:

  • (Boolean)


306
307
308
# File 'app/models/droom/event.rb', line 306

def has_people?
  invitations.any?
end

#invite(person) ⇒ Object

Instance methods



184
185
186
# File 'app/models/droom/event.rb', line 184

def invite(person)
  self.people << person
end

#one_day?Boolean

Returns:

  • (Boolean)


314
315
316
# File 'app/models/droom/event.rb', line 314

def one_day?
  all_day? && within_day?
end

#recurrenceObject



334
335
336
# File 'app/models/droom/event.rb', line 334

def recurrence
  recurrence_rules.first.to_s
end

#recurs?Boolean

Returns:

  • (Boolean)


330
331
332
# File 'app/models/droom/event.rb', line 330

def recurs?
  master || occurrences.any?
end

#start=(value) ⇒ Object

We store the start and end points of the event as a single DateTime value to make comparison simple. The setters for date and time are overridden to pass strings through chronic’s natural language parser and to treat numbers as epoch seconds. These should all work as you’d expect:

event.start = "Tuesday at 11pm"
event.start = "12/12/1969 at 10pm"
event.start = "1347354111"
event.start = Time.now + 1.hour


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

def start=(value)
  write_attribute :start, parse_date(value)
end

#start_dateObject



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

def start_date
  start.to_date if start
end

#start_date=(value) ⇒ Object



245
246
247
# File 'app/models/droom/event.rb', line 245

def start_date=(value)
  self.start = parse_date(value).to_date# + start_time
end

#start_timeObject

For interface purposes we often want to separate date and time parts. These getters will return the corresponding Date or Time object.

The ‘time_of_day` gem makes time handling a bit more intuitive by concealing the date part of a Time object.



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

def start_time
  start.time_of_day if start
end

#start_time=(value) ⇒ Object

And these setters will adjust the current value so that its date or time part corresponds to the given value. The value is passed through the same parsing mechanism as above, so:

event.start = "Tuesday at 11pm"       -> next Tuesday at 11pm
event.start_time = "8pm"              -> next Tuesday at 8pm
event.start_date = "Wednesday"        -> next Wednesday at 8pm
event.start_date = "26 February 2016" -> 26/2/16 at 8pm
event.start_time = "18:00"            -> 26/2/16 at 6pm

If the time is set before the date, we default to that time today. Times default to 00:00 in the usual way.



241
242
243
# File 'app/models/droom/event.rb', line 241

def start_time=(value)
  self.start = (start_date || Date.today).to_time + parse_date(value).seconds_since_midnight
end

#to_icsObject



363
364
365
# File 'app/models/droom/event.rb', line 363

def to_ics
  to_rical.to_s
end

#to_ricalObject



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

def to_rical
  RiCal.Event do |cal_event|
    cal_event.uid = uuid
    cal_event.summary = name
    cal_event.description = description if description
    cal_event.dtstart =  (all_day? ? start_date : start) if start
    cal_event.dtend = (all_day? ? finish_date : finish) if finish
    cal_event.url = url_with_protocol if url
    cal_event.rrules = recurrence_rules.map(&:to_rical) if recurrence_rules.any?
    cal_event.location = venue.name if venue
  end
end

#url_with_protocolObject



342
343
344
# File 'app/models/droom/event.rb', line 342

def url_with_protocol
  url =~ /^https?:\/\// ? url : "http://#{url}"
end

#url_without_protocolObject



346
347
348
# File 'app/models/droom/event.rb', line 346

def url_without_protocol
  url.sub(/^https?:\/\//, '')
end

#venue_nameObject



268
269
270
# File 'app/models/droom/event.rb', line 268

def venue_name
  venue.name if venue
end

#venue_name=(name) ⇒ Object



272
273
274
# File 'app/models/droom/event.rb', line 272

def venue_name=(name)
  self.venue = Droom::Venue.find_or_create_by_name(name)
end

#visible_to?(user_or_person) ⇒ Boolean

Returns:

  • (Boolean)


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

def visible_to?(user_or_person)
  return true if self.public?
  return false if self.private?# || Droom.events_private_by_default?
  return true
end

#within_day?Boolean

Returns:

  • (Boolean)


318
319
320
# File 'app/models/droom/event.rb', line 318

def within_day?
  (!finish || start.to.jd == finish.to.jd || finish == start + 1.day)
end