about summary refs log tree commit diff
path: root/app/models/session_activation.rb
blob: 71e9f023c2d35c24b68eb75ecfd9c64faad01d40 (plain) (blame)
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
# frozen_string_literal: true
# == Schema Information
#
# Table name: session_activations
#
#  id         :integer          not null, primary key
#  user_id    :integer          not null
#  session_id :string           not null
#  created_at :datetime         not null
#  updated_at :datetime         not null
#

class SessionActivation < ApplicationRecord
  LIMIT = Rails.configuration.x.max_session_activations

  def self.active?(id)
    id && where(session_id: id).exists?
  end

  def self.activate(id)
    activation = create!(session_id: id)
    purge_old
    activation
  end

  def self.deactivate(id)
    return unless id
    where(session_id: id).destroy_all
  end

  def self.purge_old
    order('created_at desc').offset(LIMIT).destroy_all
  end

  def self.exclusive(id)
    where('session_id != ?', id).destroy_all
  end
end