Commit edb2bd7
Changed files (2)
app
models
spec
models
app/models/user_session.rb
@@ -6,7 +6,11 @@ class UserSession < ApplicationRecord
model.key = SecureRandom.urlsafe_base64(32)
end
- scope :active, ->{ where("accessed_at > ?", 30.minutes.ago).where('created_at > ?', 24.hours.ago).where(revoked_at: nil) }
+ scope :active, -> { where.not(id: revoked).where.not(id: expired) }
+ scope :revoked, -> { where.not(revoked_at: nil) }
+ scope :expired, -> { where(id: idle_timeout).or(where(id: absolute_timeout)) }
+ scope :idle_timeout, -> { where("accessed_at < ?", 30.minutes.ago) }
+ scope :absolute_timeout, -> { where('created_at < ?', 24.hours.ago) }
def self.authenticate(key)
active.find_by(key: key)
spec/models/user_session_spec.rb
@@ -31,6 +31,10 @@ RSpec.describe UserSession do
let!(:revoked_session) { create(:user_session, :revoked) }
specify { expect(UserSession.active).to match_array([active_session]) }
+ specify { expect(UserSession.revoked).to match_array([revoked_session]) }
+ specify { expect(UserSession.expired).to match_array([inactive_session, expired_session]) }
+ specify { expect(UserSession.idle_timeout).to match_array([inactive_session]) }
+ specify { expect(UserSession.absolute_timeout).to match_array([expired_session]) }
end
describe ".authenticate" do