From 860003c26663ceb5421ee635e98e4b3651afac11 Mon Sep 17 00:00:00 2001 From: Alex Dolski Date: Fri, 5 Jan 2024 16:45:36 -0600 Subject: [PATCH] Make RequestContext serializable --- app/models/request_context.rb | 28 ++++++++++++++++++++++++++++ config/initializers/active_job.rb | 2 ++ 2 files changed, 30 insertions(+) create mode 100644 config/initializers/active_job.rb diff --git a/app/models/request_context.rb b/app/models/request_context.rb index d4c2e8d6..8bce5382 100644 --- a/app/models/request_context.rb +++ b/app/models/request_context.rb @@ -9,6 +9,34 @@ # class RequestContext + ## + # N.B.: we've told ActiveJob this class exists using + # `config/initializers/active_job.rb`. + # + class RequestContextSerializer < ActiveJob::Serializers::ObjectSerializer + def serialize?(argument) + argument.kind_of?(RequestContext) + end + + def serialize(request_context) + super( + client_hostname: request_context.client_hostname, + client_ip: request_context.client_ip, + institution: request_context.institution, + user: request_context.user, + role_limit: request_context.role_limit + ) + end + + def deserialize(hash) + RequestContext.new(client_hostname: hash["client_hostname"], + client_ip: hash["client_ip"], + user: hash['user'], + institution: hash['institution'], + role_limit: hash['role_limit']) + end + end + ## # Client hostname. # diff --git a/config/initializers/active_job.rb b/config/initializers/active_job.rb new file mode 100644 index 00000000..eb5b544c --- /dev/null +++ b/config/initializers/active_job.rb @@ -0,0 +1,2 @@ +require "request_context" +Rails.application.config.active_job.custom_serializers << ::RequestContext::RequestContextSerializer