master
1class UserSession < ApplicationRecord
2 has_one :location, as: :locatable
3 belongs_to :user
4 scope :active, -> do
5 where("accessed_at > ?", 2.weeks.ago).where(revoked_at: nil)
6 end
7
8 def revoke!
9 update_attribute(:revoked_at, Time.current)
10 end
11
12 def access(request)
13 self.accessed_at = Time.current
14 self.ip = request.ip
15 self.user_agent = request.user_agent
16 self.location = Location.build_from_ip(request.ip)
17 save ? id : nil
18 end
19
20 class << self
21 def authenticate(id)
22 active.find_by(id: id)
23 end
24 end
25end