Add `BrowserDetection` model concern (#29513)

This commit is contained in:
Matt Jankowski 2024-03-19 11:39:14 -04:00 committed by GitHub
parent d7ab5655ef
commit 62e266fbd6
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 31 additions and 29 deletions

View File

@ -0,0 +1,27 @@
# frozen_string_literal: true
module BrowserDetection
extend ActiveSupport::Concern
included do
before_save :assign_user_agent
end
def detection
@detection ||= Browser.new(user_agent)
end
def browser
detection.id
end
def platform
detection.platform.id
end
private
def assign_user_agent
self.user_agent ||= ''
end
end

View File

@ -16,21 +16,11 @@
# #
class LoginActivity < ApplicationRecord class LoginActivity < ApplicationRecord
include BrowserDetection
enum :authentication_method, { password: 'password', otp: 'otp', webauthn: 'webauthn', sign_in_token: 'sign_in_token', omniauth: 'omniauth' } enum :authentication_method, { password: 'password', otp: 'otp', webauthn: 'webauthn', sign_in_token: 'sign_in_token', omniauth: 'omniauth' }
belongs_to :user belongs_to :user
validates :authentication_method, inclusion: { in: authentication_methods.keys } validates :authentication_method, inclusion: { in: authentication_methods.keys }
def detection
@detection ||= Browser.new(user_agent)
end
def browser
detection.id
end
def platform
detection.platform.id
end
end end

View File

@ -16,6 +16,8 @@
# #
class SessionActivation < ApplicationRecord class SessionActivation < ApplicationRecord
include BrowserDetection
belongs_to :user, inverse_of: :session_activations belongs_to :user, inverse_of: :session_activations
belongs_to :access_token, class_name: 'Doorkeeper::AccessToken', dependent: :destroy, optional: true belongs_to :access_token, class_name: 'Doorkeeper::AccessToken', dependent: :destroy, optional: true
belongs_to :web_push_subscription, class_name: 'Web::PushSubscription', dependent: :destroy, optional: true belongs_to :web_push_subscription, class_name: 'Web::PushSubscription', dependent: :destroy, optional: true
@ -24,19 +26,6 @@ class SessionActivation < ApplicationRecord
to: :access_token, to: :access_token,
allow_nil: true allow_nil: true
def detection
@detection ||= Browser.new(user_agent)
end
def browser
detection.id
end
def platform
detection.platform.id
end
before_save :assign_user_agent
before_create :assign_access_token before_create :assign_access_token
class << self class << self
@ -67,10 +56,6 @@ class SessionActivation < ApplicationRecord
private private
def assign_user_agent
self.user_agent = '' if user_agent.nil?
end
def assign_access_token def assign_access_token
self.access_token = Doorkeeper::AccessToken.create!(access_token_attributes) self.access_token = Doorkeeper::AccessToken.create!(access_token_attributes)
end end