event.rb 2.65 KB
Newer Older
Tyler Lemburg's avatar
Tyler Lemburg committed
1
2
3
4
5
6
7
require 'active_record'

class Event < ActiveRecord::Base
	has_many :event_signups, :dependent => :destroy
	has_one :reservation, :dependent => :destroy
	belongs_to :location
	belongs_to :event_type
Tyler Lemburg's avatar
Tyler Lemburg committed
8
	belongs_to :service_space
Tyler Lemburg's avatar
Tyler Lemburg committed
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
	alias_method :type, :event_type
	alias_method :signups, :event_signups

	scope :in_day, ->(time) {
		today = time.in_time_zone.midnight
		tomorrow = (time.in_time_zone.midnight + 1.day + 1.hour).in_time_zone.midnight
		where('(start_time >= ? AND start_time < ?) OR (end_time >= ? AND end_time < ?)', today.getutc, tomorrow.getutc, today.getutc, tomorrow.getutc)
	}

	scope :in_week, ->(time) {
		last_sunday = time.in_time_zone.week_start
		next_sunday = (time.in_time_zone.week_start + 1.week + 1.hour).in_time_zone.week_start
		where('(start_time >= ? AND start_time < ?) OR (end_time >= ? AND end_time < ?)', last_sunday.getutc, next_sunday.getutc, last_sunday.getutc, next_sunday.getutc)
	}

	# returns length in minutes. If start or end is nil, returns 0
	def length
		if end_time.nil? || start_time.nil?
			return 0
		end
		((end_time - start_time) / 60).to_i
	end

	def info_link
Tyler Lemburg's avatar
Tyler Lemburg committed
33
		"/#{service_space.url_name}/events/#{id}/"
Tyler Lemburg's avatar
Tyler Lemburg committed
34
35
36
	end

	def edit_link
Tyler Lemburg's avatar
Tyler Lemburg committed
37
		"/#{service_space.url_name}/admin/events/#{id}/edit/"
Tyler Lemburg's avatar
Tyler Lemburg committed
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
	end

	def has_reservation
		!self.reservation.nil?
	end

	def image_src
		"/images/#{id}/"
	end

	def set_data(params)
		self.title = params[:title]
		self.description = params[:description]
		self.start_time = calculate_time(params[:start_date], params[:start_time_hour], params[:start_time_minute], params[:start_time_am_pm])
		self.end_time = calculate_time(params[:end_date], params[:end_time_hour], params[:end_time_minute], params[:end_time_am_pm])
		self.event_type_id = params[:type]
		self.location_id = params[:location]
		self.max_signups = params[:limit_signups] == 'on' ? params[:max_signups].to_i : nil
		self.save
	end

	def set_image_data(params)
		if params[:imagedata]
			self.imagemime = params[:imagedata][:type]
			self.imagedata = params[:imagedata][:tempfile].read if params[:imagedata][:tempfile].is_a?(Tempfile)
		end
		self.save
	end

	def remove_image_data
		self.imagemime = nil
		self.imagedata = nil
		self.save
	end
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88

	def to_ics
		calendar = Icalendar::Calendar.new
		ical = Icalendar::Event.new
		ical.dtstart = self.start_time.in_time_zone.strftime("%Y%m%dT%H%M%S")
		ical.dtend = self.end_time.in_time_zone.strftime("%Y%m%dT%H%M%S")
		ical.summary = self.title
		ical.description = self.description
		ical.location = self.location.name
		ical.uid = "unl_resource_scheduler_#{ENV['RACK_ENV']}_event_#{self.id}"
		calendar.add_event(ical)
		calendar.to_ical
	end

	def download_link
		"/#{service_space.url_name}/events/#{id}/event.ics"
	end
Tyler Lemburg's avatar
Tyler Lemburg committed
89
end