discourse/app/models/draft_sequence.rb

43 lines
1.1 KiB
Ruby
Raw Normal View History

2013-02-06 03:16:51 +08:00
class DraftSequence < ActiveRecord::Base
2017-07-28 09:20:09 +08:00
def self.next!(user, key)
2013-02-06 03:16:51 +08:00
user_id = user
2017-04-15 12:11:02 +08:00
user_id = user.id unless user.is_a?(Integer)
return 0 if user_id < 0
h = { user_id: user_id, draft_key: key }
c = DraftSequence.find_by(h)
2013-02-06 03:16:51 +08:00
c ||= DraftSequence.new(h)
c.sequence ||= 0
c.sequence += 1
2017-09-15 16:44:04 +08:00
c.save!
exec_sql("DELETE FROM drafts WHERE user_id = :user_id AND draft_key = :draft_key AND sequence < :sequence", draft_key: key, user_id: user_id, sequence: c.sequence)
2013-02-06 03:16:51 +08:00
c.sequence
end
def self.current(user, key)
return nil unless user
user_id = user
2017-04-15 12:11:02 +08:00
user_id = user.id unless user.is_a?(Integer)
2013-02-06 03:16:51 +08:00
# perf critical path
r = exec_sql('select sequence from draft_sequences where user_id = ? and draft_key = ?', user_id, key).values
2017-09-15 16:44:04 +08:00
r.length.zero? ? 0 : r[0][0]
2013-02-06 03:16:51 +08:00
end
end
# == Schema Information
#
# Table name: draft_sequences
#
# id :integer not null, primary key
# user_id :integer not null
2017-12-05 23:29:14 +08:00
# 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
#