2013-02-05 13:16:51 -06:00
|
|
|
class DraftSequence < ActiveRecord::Base
|
|
|
|
def self.next!(user,key)
|
|
|
|
user_id = user
|
|
|
|
user_id = user.id unless user.class == Fixnum
|
2013-02-28 12:54:12 -06:00
|
|
|
h = { user_id: user_id, draft_key: key }
|
2014-05-06 08:41:59 -05:00
|
|
|
c = DraftSequence.find_by(h)
|
2013-02-05 13:16:51 -06:00
|
|
|
c ||= DraftSequence.new(h)
|
|
|
|
c.sequence ||= 0
|
|
|
|
c.sequence += 1
|
2013-02-07 09:45:24 -06:00
|
|
|
c.save
|
2013-02-05 13:16:51 -06: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
|
|
|
|
|
2013-02-28 12:54:12 -06:00
|
|
|
r.length.zero? ? 0 : r[0][0].to_i
|
2013-02-05 13:16:51 -06:00
|
|
|
end
|
|
|
|
end
|
2013-05-23 21:48:32 -05:00
|
|
|
|
|
|
|
# == Schema Information
|
|
|
|
#
|
|
|
|
# Table name: draft_sequences
|
|
|
|
#
|
|
|
|
# id :integer not null, primary key
|
|
|
|
# user_id :integer not null
|
|
|
|
# draft_key :string(255) not null
|
|
|
|
# sequence :integer not null
|
|
|
|
#
|
|
|
|
# Indexes
|
|
|
|
#
|
|
|
|
# index_draft_sequences_on_user_id_and_draft_key (user_id,draft_key) UNIQUE
|
|
|
|
#
|