You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

56 lines
1.1 KiB

  1. # frozen_string_literal: true
  2. # == Schema Information
  3. #
  4. # Table name: session_activations
  5. #
  6. # id :integer not null, primary key
  7. # user_id :integer not null
  8. # session_id :string not null
  9. # created_at :datetime not null
  10. # updated_at :datetime not null
  11. # user_agent :string default(""), not null
  12. # ip :inet
  13. #
  14. class SessionActivation < ApplicationRecord
  15. def detection
  16. @detection ||= Browser.new(user_agent)
  17. end
  18. def browser
  19. detection.id
  20. end
  21. def platform
  22. detection.platform.id
  23. end
  24. before_save do
  25. self.user_agent = '' if user_agent.nil?
  26. end
  27. class << self
  28. def active?(id)
  29. id && where(session_id: id).exists?
  30. end
  31. def activate(options = {})
  32. activation = create!(options)
  33. purge_old
  34. activation
  35. end
  36. def deactivate(id)
  37. return unless id
  38. where(session_id: id).destroy_all
  39. end
  40. def purge_old
  41. order('created_at desc').offset(Rails.configuration.x.max_session_activations).destroy_all
  42. end
  43. def exclusive(id)
  44. where('session_id != ?', id).destroy_all
  45. end
  46. end
  47. end