-
Notifications
You must be signed in to change notification settings - Fork 5
/
meetup.rb
100 lines (82 loc) · 2.26 KB
/
meetup.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
class Meetup < ActiveRecord::Base
belongs_to :first_user, :class_name => 'User'
belongs_to :second_user, :class_name => 'User'
has_many :offers
has_many :dflns
after_save :schedule_jobs
before_validation :set_default_desired_genders
validates_presence_of :first_user_id
def self.newest
order('created_at DESC')
end
# All the unscheduled dates.
def self.unscheduled
where('state = "unscheduled"')
end
# All the scheduled dates.
def self.scheduled
where('state = "scheduled"')
end
def self.proposed
where('state = "proposed"')
end
# All dates that are not yet canceled but are not actively being searched.
def self.retryable
where('state = "retryable"')
end
# All meetups for the given user
def self.for(user)
where('meetups.first_user_id = :id OR meetups.second_user_id = :id', :id => user.id)
end
# All pending offers for this meetup
def pending_offers
offers.pending
end
# In a scheduled date produce the user that is opposite the one passed in.
# In an unscheduled date produce nil.
def for(user)
if scheduled?
if first_user_id == user.id
second_user
elsif second_user_id == user.id
### TODO: untested
first_user
end
end
end
def unschedule!
self.state = "unscheduled"
self.save!
end
### TODO: Use this when the user asks for a new date
def cancel!
self.state = "cancelled"
self.save!
end
def retryable!
self.state = 'retryable'
self.save!
end
def schedule_with!(second_user)
self.state = 'scheduled'
self.second_user = second_user
self.save!
end
# True if the date has two people and a meeting spot.
def scheduled?
state == "scheduled"
end
private
def schedule_jobs
if state_was == "unscheduled" && state == "scheduled"
morning_after = 1.day.from_now.beginning_of_day + 10.hours
QUEUE.enqueue_at(morning_after, MorningAfterCheckerUpper, { :meetup_id => self.id })
end
end
def set_default_desired_genders
self.desires_male = !!first_user.looking_for_male if desires_male.nil?
self.desires_female = !!first_user.looking_for_female if desires_female.nil?
self.desires_other = !!first_user.looking_for_other if desires_other.nil?
true
end
end