REFACTOR: Use UserAuthTokenLogSerializer instead of conditionals.

This commit is contained in:
Guo Xiang Tan 2018-09-03 10:44:22 +08:00
parent 45f092a49d
commit 7256b3bf68
4 changed files with 99 additions and 87 deletions

View File

@ -0,0 +1,71 @@
module UserAuthTokensMixin
extend ActiveSupport::Concern
included do
attributes :id,
:client_ip,
:os,
:device_name,
:icon,
:created_at
end
def client_ip
object.client_ip.to_s
end
def os
case object.user_agent
when /Android/i
'Android'
when /iPhone|iPad|iPod/i
'iOS'
when /Macintosh/i
'macOS'
when /Linux/i
'Linux'
when /Windows/i
'Windows'
else
I18n.t('staff_action_logs.unknown')
end
end
def device_name
case object.user_agent
when /Android/i
I18n.t('user_auth_tokens.devices.android')
when /iPad/i
I18n.t('user_auth_tokens.devices.ipad')
when /iPhone/i
I18n.t('user_auth_tokens.devices.iphone')
when /iPod/i
I18n.t('user_auth_tokens.devices.ipod')
when /Mobile/i
I18n.t('user_auth_tokens.devices.mobile')
when /Macintosh/i
I18n.t('user_auth_tokens.devices.mac')
when /Linux/i
I18n.t('user_auth_tokens.devices.linux')
when /Windows/i
I18n.t('user_auth_tokens.devices.windows')
else
I18n.t('user_auth_tokens.devices.unknown')
end
end
def icon
case os
when 'Android'
'android'
when 'macOS', 'iOS'
'apple'
when 'Linux'
'linux'
when 'Windows'
'windows'
else
'question'
end
end
end

View File

@ -0,0 +1,16 @@
class UserAuthTokenLogSerializer < ApplicationSerializer
include UserAuthTokensMixin
attributes :action
def action
case object.action
when 'generate'
I18n.t('log_in')
when 'destroy'
I18n.t('unsubscribe.log_out')
else
I18n.t('staff_action_logs.unknown')
end
end
end

View File

@ -1,88 +1,5 @@
class UserAuthTokenSerializer < ApplicationSerializer class UserAuthTokenSerializer < ApplicationSerializer
attributes :id, include UserAuthTokensMixin
:action,
:client_ip,
:created_at,
:seen_at,
:os,
:device_name,
:icon
def action attributes :seen_at
case object.action
when 'generate'
I18n.t('log_in')
when 'destroy'
I18n.t('unsubscribe.log_out')
else
I18n.t('staff_action_logs.unknown')
end
end
def include_action?
object.has_attribute?(:action)
end
def client_ip
object.client_ip.to_s
end
def include_seen_at?
object.has_attribute?(:seen_at)
end
def os
case object.user_agent
when /Android/i
'Android'
when /iPhone|iPad|iPod/i
'iOS'
when /Macintosh/i
'macOS'
when /Linux/i
'Linux'
when /Windows/i
'Windows'
else
I18n.t('staff_action_logs.unknown')
end
end
def device_name
case object.user_agent
when /Android/i
I18n.t('user_auth_tokens.devices.android')
when /iPad/i
I18n.t('user_auth_tokens.devices.ipad')
when /iPhone/i
I18n.t('user_auth_tokens.devices.iphone')
when /iPod/i
I18n.t('user_auth_tokens.devices.ipod')
when /Mobile/i
I18n.t('user_auth_tokens.devices.mobile')
when /Macintosh/i
I18n.t('user_auth_tokens.devices.mac')
when /Linux/i
I18n.t('user_auth_tokens.devices.linux')
when /Windows/i
I18n.t('user_auth_tokens.devices.windows')
else
I18n.t('user_auth_tokens.devices.unknown')
end
end
def icon
case os
when 'Android'
'android'
when 'macOS', 'iOS'
'apple'
when 'Linux'
'linux'
when 'Windows'
'windows'
else
'question'
end
end
end end

View File

@ -196,11 +196,19 @@ class UserSerializer < BasicUserSerializer
end end
def user_auth_tokens def user_auth_tokens
ActiveModel::ArraySerializer.new(object.user_auth_tokens.order(:seen_at).reverse_order, each_serializer: UserAuthTokenSerializer) ActiveModel::ArraySerializer.new(
object.user_auth_tokens.order(:seen_at).reverse_order,
each_serializer: UserAuthTokenSerializer
)
end end
def user_auth_token_logs def user_auth_token_logs
ActiveModel::ArraySerializer.new(object.user_auth_token_logs.where(action: UserAuthToken::USER_ACTIONS).order(:created_at).reverse_order, each_serializer: UserAuthTokenSerializer) ActiveModel::ArraySerializer.new(
object.user_auth_token_logs.where(
action: UserAuthToken::USER_ACTIONS
).order(:created_at).reverse_order,
each_serializer: UserAuthTokenLogSerializer
)
end end
def bio_raw def bio_raw