2013-02-05 14:16:51 -05:00
|
|
|
class DraftSequence < ActiveRecord::Base
|
|
|
|
def self.next!(user,key)
|
|
|
|
user_id = user
|
|
|
|
user_id = user.id unless user.class == Fixnum
|
2013-02-07 10:45:24 -05:00
|
|
|
h = {user_id: user_id, draft_key: key}
|
2013-02-05 14:16:51 -05:00
|
|
|
c = DraftSequence.where(h).first
|
|
|
|
c ||= DraftSequence.new(h)
|
|
|
|
c.sequence ||= 0
|
|
|
|
c.sequence += 1
|
2013-02-07 10:45:24 -05:00
|
|
|
c.save
|
2013-02-05 14:16:51 -05:00
|
|
|
c.sequence
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.current(user, key)
|
|
|
|
return nil unless user
|
|
|
|
|
|
|
|
user_id = user
|
|
|
|
user_id = user.id unless user.class == Fixnum
|
|
|
|
|
|
|
|
# perf critical path
|
|
|
|
r = exec_sql('select sequence from draft_sequences where user_id = ? and draft_key = ?', user_id, key).values
|
|
|
|
|
|
|
|
if r.length == 0
|
|
|
|
0
|
2013-02-07 10:45:24 -05:00
|
|
|
else
|
2013-02-05 14:16:51 -05:00
|
|
|
r[0][0].to_i
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|