Class: Droom::Event

Inherits:
ActiveRecord::Base
  • Object
show all
Includes:
ActionView::Helpers::SanitizeHelper, Concerns::Slugged
Defined in:
app/models/droom/event.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.falling_within(span) ⇒ Object

Chronic::Span



139
140
141
# File 'app/models/droom/event.rb', line 139

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

.futureObject



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

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

.in_month(year, month) ⇒ Object

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



117
118
119
120
121
# File 'app/models/droom/event.rb', line 117

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



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

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.



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

def self.in_the_last(period)           # seconds. eg calendar.occurrences.in_the_last(1.week)
  finish = Time.zone.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)



123
124
125
126
127
# File 'app/models/droom/event.rb', line 123

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)



111
112
113
114
115
# File 'app/models/droom/event.rb', line 111

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)



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

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



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

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

Instance Method Details

#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’.



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

scope :all_private, -> { where("private = 1") }

#as_json(options = {}) ⇒ Object



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

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

#as_search_resultObject



349
350
351
352
353
354
355
356
# File 'app/models/droom/event.rb', line 349

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

#as_suggestionObject



340
341
342
343
344
345
346
347
# File 'app/models/droom/event.rb', line 340

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

#attach(doc) ⇒ Object



157
158
159
# File 'app/models/droom/event.rb', line 157

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

#attended_by?(user) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#categories_for_selectionObject



246
247
248
249
250
# File 'app/models/droom/event.rb', line 246

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

#continuing?Boolean

Returns:

  • (Boolean)


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

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

#detail_visible_to?(user) ⇒ Boolean

Returns:

  • (Boolean)


262
263
264
265
266
267
268
269
# File 'app/models/droom/event.rb', line 262

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

#durationObject



226
227
228
229
230
231
232
# File 'app/models/droom/event.rb', line 226

def duration
  if finish
    finish - start
  else
    0
  end
end

#find_or_create_agenda_category(category) ⇒ Object



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

def find_or_create_agenda_category(category)
  agenda_categories.where(category_id: category.id).first_or_create
end

#finishObject



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

def finish
  tz = timezone || Time.zone
  if finish = read_attribute(:finish)
    finish.in_time_zone(tz)
  end
end

#finish=(value) ⇒ Object



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

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

#finish_dateObject



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

def finish_date
  finish.to_date if finish
end

#finish_timeObject



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

def finish_time
  finish.to_time_of_day if finish
end

#finished?Boolean

Returns:

  • (Boolean)


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

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

#folder_nameObject



358
359
360
# File 'app/models/droom/event.rb', line 358

def folder_name
  "#{name} (#{month_name} #{year})"
end

#has_anyone?Boolean

Returns:

  • (Boolean)


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

def has_anyone?
  invitations.any?
end

#has_documents?Boolean

Returns:

  • (Boolean)


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

def has_documents?
  all_documents.any?
end

#icalendar_eventObject



315
316
317
318
319
320
321
322
323
324
325
326
# File 'app/models/droom/event.rb', line 315

def icalendar_event
  event = Icalendar::Event.new
  event.uid = uuid
  event.summary = name
  event.description = plain_description if description?
  event.dtstart = (all_day? ? start_date : start) if start?
  event.dtend = (all_day? ? finish_date : finish) if finish?
  event.url = url_with_protocol if url?
  event.attendees = invitations.accepted.map{|inv| "mailto:#{inv.user.email}"} if invitations.accepted.any?
  event.location = venue.name if venue
  event
end

#invite(user) ⇒ Object

Instance methods



153
154
155
# File 'app/models/droom/event.rb', line 153

def invite(user)
  self.users << user
end

#monthObject



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

def month
  start.strftime("%m")
end

#month_nameObject



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

def month_name
  start.strftime("%b")
end

#one_day?Boolean

Returns:

  • (Boolean)


279
280
281
# File 'app/models/droom/event.rb', line 279

def one_day?
  all_day? && within_day?
end

#plain_descriptionObject



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

def plain_description
  strip_tags(description)
end

#startObject

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

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



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

def start
  tz = timezone || Time.zone
  if start = read_attribute(:start)
    start.in_time_zone(tz)
  end
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.zone.now + 1.hour


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

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

#start_dateObject



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

def start_date
  start.to_date if start
end

#start_timeObject



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

def start_time
  start.to_time_of_day if start
end

#to_icsObject



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

def to_ics
  cal = Icalendar::Calendar.new
  cal.add_event(icalendar_event)
  cal.to_ical
end

#url_with_protocolObject



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

def url_with_protocol
  if url? && url !~ /^https?:\/\//
    "http://#{url}"
  else
    url
  end
end

#url_without_protocolObject



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

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

#venue_nameObject



234
235
236
# File 'app/models/droom/event.rb', line 234

def venue_name
  venue.name if venue
end

#venue_name=(name) ⇒ Object



238
239
240
# File 'app/models/droom/event.rb', line 238

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

#visible_to?(user) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#within_day?Boolean

Returns:

  • (Boolean)


283
284
285
# File 'app/models/droom/event.rb', line 283

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

#yearObject



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

def year
  start.strftime("%Y")
end