Refactoring: Migrated UserDeviceLogJob from Delayed::Job to Active Job.

This commit is contained in:
Jens Pfeifer 2018-12-12 13:51:29 +01:00 committed by Thorsten Eckel
parent 6854ad0903
commit d7401b6a57
4 changed files with 39 additions and 27 deletions

View file

@ -59,14 +59,12 @@ module ApplicationController::HandlesDevices
# add device if needed
http_user_agent = ENV['HTTP_USER_AGENT'] || request.env['HTTP_USER_AGENT']
Delayed::Job.enqueue(
Observer::UserDeviceLogJob.new(
http_user_agent,
remote_ip,
user.id,
session[:user_device_fingerprint],
type,
)
UserDeviceLogJob.perform_later(
http_user_agent,
remote_ip,
user.id,
session[:user_device_fingerprint],
type,
)
end
end

View file

@ -0,0 +1,11 @@
class UserDeviceLogJob < ApplicationJob
def perform(http_user_agent, remote_ip, user_id, fingerprint, type)
UserDevice.add(
http_user_agent,
remote_ip,
user_id,
fingerprint,
type,
)
end
end

View file

@ -1,19 +0,0 @@
class Observer::UserDeviceLogJob
def initialize(http_user_agent, remote_ip, user_id, fingerprint, type)
@http_user_agent = http_user_agent
@remote_ip = remote_ip
@user_id = user_id
@fingerprint = fingerprint
@type = type
end
def perform
UserDevice.add(
@http_user_agent,
@remote_ip,
@user_id,
@fingerprint,
@type,
)
end
end

View file

@ -0,0 +1,22 @@
require 'rails_helper'
RSpec.describe UserDeviceLogJob, type: :job do
let!(:user) { create(:user) }
it 'executes user device log job' do
expect do
described_class.perform_now(
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.107 Safari/537.36',
'172.0.0.1',
user.id,
'fingerprintABC123',
'session',
)
end.to change {
UserDevice.where(
user_id: user.id,
).count
}.by(1)
end
end