2017-06-24 01:50:53 +09:00
|
|
|
# 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
|
2017-06-25 23:54:30 +09:00
|
|
|
# user_agent :string default(""), not null
|
|
|
|
# ip :inet
|
2017-06-24 01:50:53 +09:00
|
|
|
#
|
|
|
|
|
|
|
|
class SessionActivation < ApplicationRecord
|
2017-06-25 23:54:30 +09:00
|
|
|
def detection
|
|
|
|
@detection ||= Browser.new(user_agent)
|
2017-06-24 01:50:53 +09:00
|
|
|
end
|
|
|
|
|
2017-06-25 23:54:30 +09:00
|
|
|
def browser
|
|
|
|
detection.id
|
2017-06-24 01:50:53 +09:00
|
|
|
end
|
|
|
|
|
2017-06-25 23:54:30 +09:00
|
|
|
def platform
|
|
|
|
detection.platform.id
|
2017-06-24 01:50:53 +09:00
|
|
|
end
|
|
|
|
|
2017-06-25 23:54:30 +09:00
|
|
|
before_save do
|
|
|
|
self.user_agent = '' if user_agent.nil?
|
2017-06-24 01:50:53 +09:00
|
|
|
end
|
|
|
|
|
2017-06-25 23:54:30 +09:00
|
|
|
class << self
|
|
|
|
def active?(id)
|
|
|
|
id && where(session_id: id).exists?
|
|
|
|
end
|
|
|
|
|
|
|
|
def activate(options = {})
|
|
|
|
activation = create!(options)
|
|
|
|
purge_old
|
|
|
|
activation
|
|
|
|
end
|
|
|
|
|
|
|
|
def deactivate(id)
|
|
|
|
return unless id
|
|
|
|
where(session_id: id).destroy_all
|
|
|
|
end
|
|
|
|
|
|
|
|
def purge_old
|
|
|
|
order('created_at desc').offset(Rails.configuration.x.max_session_activations).destroy_all
|
|
|
|
end
|
|
|
|
|
|
|
|
def exclusive(id)
|
|
|
|
where('session_id != ?', id).destroy_all
|
|
|
|
end
|
2017-06-24 01:50:53 +09:00
|
|
|
end
|
|
|
|
end
|