This repository has been archived by the owner on Jun 9, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 31
/
suggestion.rb
252 lines (204 loc) · 5.8 KB
/
suggestion.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
# suggestion.rb
#
# Model that contains a title suggestion. These are created from the IRC chat
# bot via !suggest
require 'open-uri'
require 'json'
require 'dm-core'
require 'dm-validations'
require 'dm-timestamps'
require 'dm-aggregates'
class Suggestion
include DataMapper::Resource
property :id, Serial
property :title, String, :length => 40,
:message => I18n.t('messages.models.suggestion')
property :user, String
property :show, String
property :created_at, DateTime, index: true
property :updated_at, DateTime
validates_presence_of :title
validates_with_method :title, :check_title_uniqueness, :if => :new?
# Assocations
has n, :votes
belongs_to :cluster, :required => false
# ------------------
# Before Save
# ------------------
before :save, :fix_title
# Remove quotes from the title before saving
def fix_title
# Remove quotes if the user thought they needed them
self.title = self.title.gsub(/^(?:'|")(.*)(?:'|")$/, '\1')
end
before :create, :check_cluster
def check_cluster
Suggestion.minutes_ago(30).each do |suggestion|
if suggestion.id != self.id and lev_sim(suggestion) > 0.7
if suggestion.in_cluster?
suggestion.cluster.suggestions << self
self.cluster = suggestion.cluster
suggestion.cluster.save
else
cluster = Cluster.create
cluster.suggestions << suggestion
cluster.suggestions << self
self.cluster = cluster
cluster.save
end
return true
end
end
true
end
before :create, :set_live_show
def set_live_show
# Only fetch show from website if it wasn't set previously.
if !self.show
self.show = Shows.fetch_live_show_slug
end
true
end
# after :save, :debug_cluster_id
# def debug_cluster_id
# $stderr.puts "After save, #{self.title}'s cluster_id is #{self.cluster_id}"
# end
# ------------------
# Validations
# ------------------
# Verifies that title hasn't been entered in the last 30 minutes
def check_title_uniqueness
if self.title
Suggestion.minutes_ago(30).each do |suggestion|
if suggestion.title.downcase == self.title.downcase
return [false, "Darn, #{suggestion.user} beat you to \"#{suggestion.title}\"."]
end
end
else
return true
end
return true
end
# ------------------
# Class Methods
# ------------------
def self.recent(days_ago = 1)
from = DateTime.now - days_ago
all(:created_at.gt => from).all(:order => [:created_at.desc])
end
def self.minutes_ago(minutes)
if minutes
time_ago = Time.now - (60 * minutes)
all(:created_at.gt => time_ago).all(:order => [:created_at.desc])
end
end
# Group suggestions by show slug
#
# Returns an array of SuggestionSets (see the bottom of this file)
def self.group_by_show
suggestion_sets = []
last_show = nil
last_time = nil
split_interval = 0.75 # 18 hours - for creating a new set if same show runs 2 days in a row without another in between
all.each do |suggestion|
if suggestion_sets.empty? or last_show != suggestion.show or (last_time - suggestion.created_at) > split_interval
suggestion_sets << SuggestionSet.new(suggestion.show)
end
last_show = suggestion.show
last_time = suggestion.created_at
# Always add the show to the last group
suggestion_sets.last.add suggestion
end
suggestion_sets
end
# ------------------
# Helper Methods
# ------------------
# Voting
# Add a vote to the votes assocation for the user's IP
#
# Returns true if successful and false if the user has already voted.
def vote_up(user_ip)
if user_already_voted?(user_ip)
false
else
if self.votes.create(:user_ip => user_ip)
true
else
false
end
end
end
# Determine if a user has already voted on this suggestion from this IP address.
#
# Returns true if user has not voted on this suggestion.
def user_already_voted?(user_ip)
self.votes.all(:user_ip => user_ip).count > 0
end
def to_s
"#{self.title} by #{self.user}"
end
def update_votes_counter_cache
vote_count = self.votes.count
if vote_count != self.votes_counter
puts "Fixing missmatched count (#{vote_count}/#{self.votes_counter} for #{self})"
self.update(:votes_counter => self.votes.count)
end
end
# Clustering
def lev_sim(other_suggestion)
distance = levenshtein(self.title.downcase,
other_suggestion.title.downcase)
1.0 - distance.to_f / [self.title.length, other_suggestion.title.length].max
end
def in_cluster?
!!self.cluster_id
end
def top_of_cluster?
if self.in_cluster?
self.id == self.cluster.top_suggestion.id
else
true # would be the top if it were in a cluster by itself
end
end
def total_for_cluster
self.in_cluster? ? self.cluster.total_votes : self.votes.count
end
end
# Class to hold sets of suggestions for the group_by_show method of Suggestion
class SuggestionSet
def initialize(slug = nil)
@slug = slug
@suggestions = []
super
end
def add(show)
@suggestions << show
end
def to_s
string = ""
string << "Show #@slug\n"
string << " Titles:\n"
string << @suggestions.map{|s| " #{s.title}"}.join("\n")
string
end
attr_accessor :slug, :suggestions
end
# https://github.com/threedaymonk/text/blob/master/lib/text/levenshtein.rb
def levenshtein(str1, str2)
ar1 = str1.split(//)
ar2 = str2.split(//)
d = (0..ar2.length).to_a
x = nil
ar1.length.times do |i|
e = i + 1
ar2.length.times do |j|
cost = (ar1[i] == ar2[j]) ? 0 : 1;
x = [ d[j+1] + 1, e + 1, d[j] + cost].min
d[j] = e
e = x
end
d[ar2.length] = x
end
x
end