From 7256b3bf689aa156086a397ca5264188a3d912ed Mon Sep 17 00:00:00 2001 From: Guo Xiang Tan Date: Mon, 3 Sep 2018 10:44:22 +0800 Subject: [PATCH] REFACTOR: Use `UserAuthTokenLogSerializer` instead of conditionals. --- .../concerns/user_auth_tokens_mixin.rb | 71 +++++++++++++++ .../user_auth_token_log_serializer.rb | 16 ++++ app/serializers/user_auth_token_serializer.rb | 87 +------------------ app/serializers/user_serializer.rb | 12 ++- 4 files changed, 99 insertions(+), 87 deletions(-) create mode 100644 app/serializers/concerns/user_auth_tokens_mixin.rb create mode 100644 app/serializers/user_auth_token_log_serializer.rb diff --git a/app/serializers/concerns/user_auth_tokens_mixin.rb b/app/serializers/concerns/user_auth_tokens_mixin.rb new file mode 100644 index 00000000000..e4dac0e2a54 --- /dev/null +++ b/app/serializers/concerns/user_auth_tokens_mixin.rb @@ -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 diff --git a/app/serializers/user_auth_token_log_serializer.rb b/app/serializers/user_auth_token_log_serializer.rb new file mode 100644 index 00000000000..220d9aa477e --- /dev/null +++ b/app/serializers/user_auth_token_log_serializer.rb @@ -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 diff --git a/app/serializers/user_auth_token_serializer.rb b/app/serializers/user_auth_token_serializer.rb index b9168289cde..8a9fa01c992 100644 --- a/app/serializers/user_auth_token_serializer.rb +++ b/app/serializers/user_auth_token_serializer.rb @@ -1,88 +1,5 @@ class UserAuthTokenSerializer < ApplicationSerializer - attributes :id, - :action, - :client_ip, - :created_at, - :seen_at, - :os, - :device_name, - :icon + include UserAuthTokensMixin - 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 - - 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 + attributes :seen_at end diff --git a/app/serializers/user_serializer.rb b/app/serializers/user_serializer.rb index b4bd2426d14..eedadd87bfd 100644 --- a/app/serializers/user_serializer.rb +++ b/app/serializers/user_serializer.rb @@ -196,11 +196,19 @@ class UserSerializer < BasicUserSerializer end 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 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 def bio_raw