This repository has been archived by the owner on Feb 27, 2018. It is now read-only.
/
slot.rb
139 lines (108 loc) · 2.76 KB
/
slot.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
class Slot < ActiveRecord::Base
belongs_to :room
belongs_to :timeslot
belongs_to :talk
attr_accessible :talk_id
attr_accessible :room_id, :timeslot_id, :talk_id, :as => :admin
validates :timeslot_id, :presence => true
validates :room_id, :presence => true
after_save :clear_cache
after_save :notify
def start
timeslot.start
end
def end
timeslot.end
end
def self.generate!
Slot.delete_all
Room.all.each do |room|
Timeslot.all.each do |timeslot|
slot = Slot.create({:room_id => room.id, :timeslot_id => timeslot.id}, :without_protection => true)
puts "Slot #{slot.id} created (#{room.name}, #{timeslot.start.to_s})"
end
end
end
# Empty (but not necessarily available)
def self.find_empty
@slots = Array.new()
Slot.all.each do | slot |
if (slot.is_empty?)
@slots << slot
end
end
return @slots
end
# Empty and in the future (but not necessarily available)
def self.find_empty_upcoming
@slots = Array.new()
upcoming_timeslots = Timeslot.upcoming
empty_slots.find_empty.each do | slot |
if (upcoming_timeslots.include?(slot.timeslot))
@slots << slot
end
end
return @slots
end
# Empty and unlocked (but not necessarily in the future)
def self.find_all_available
self.find_available(Slot.all)
end
# Empty and unlocked from set of slots provided (but not necessarily in the future)
def self.find_available(slots_to_check)
@slots = Array.new()
slots_to_check.each do | slot |
if (!slot.locked && slot.is_empty?)
@slots << slot
end
end
return @slots
end
# Available and in the future
def self.find_all_available_upcoming
Slot.find_available_upcoming(Slot.all)
end
# Available and in the future
def self.find_available_upcoming(slots_to_check)
@slots = Array.new()
slots_to_check.each do | slot |
if (!slot.locked && slot.is_empty? && slot.timeslot.is_upcoming?)
@slots << slot
end
end
return @slots
end
def self.find_occupied
Slot.select {|slot| !slot.is_empty?}
end
def is_empty?
if (self.talk.nil?)
return true
else
return false
end
end
def self.by_timeslot(timeslot)
Slot.joins(:timeslot).where('timeslots.id = ?', timeslot)
end
def self.on_now
Timeslot.on_now.slots
end
def self.on_next
Timeslot.on_next.slots
end
private
def clear_cache
$redis.keys("views/slot_#{self.id}*").each do |key|
$redis.del(key)
end
end
def notify
if (defined?(PUBNUB) && ENV["REALTIME"] == 'ON')
PUBNUB.publish({
'channel' => PUBNUB_CHANNEL,
'message' => self
})
end
end
end