Skip to content

Instantly share code, notes, and snippets.

@eliotsykes
Created September 21, 2022 07:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eliotsykes/c81ff06b948808532943eca5515d9623 to your computer and use it in GitHub Desktop.
Save eliotsykes/c81ff06b948808532943eca5515d9623 to your computer and use it in GitHub Desktop.
ActiveJob compatibility for Rails 5.2 -> 6.0 upgrade
# File: config/initializers/active_job_deserializer_extension.rb
return unless (VERSION::MAJOR == 5 && VERSION::MINOR == 2)
require "active_job/arguments"
module ActiveJobDeserializerExtension
# This copies the hash deserialization from activejob 6.0 so activejob 5.2 is forwards compatible, it can run jobs
# with hash args that were serialized by activejob 6.0. This allows Rails 5.2 and 6.0 to run within the same app
# during the upgrade period.
def deserialize_hash(serialized_hash)
result = serialized_hash.transform_values { |v| deserialize_argument(v) }
if result.delete('_aj_hash_with_indifferent_access')
result = result.with_indifferent_access
elsif symbol_keys = result.delete('_aj_symbol_keys')
result = transform_symbol_keys(result, symbol_keys)
elsif symbol_keys = result.delete('_aj_ruby2_keywords')
# _aj_ruby2_keywords was added in activejob 6.0, this elsif block is missing in activejob 5.2. Adding it here
# makes activejob 5.2 forwards-compatible to run kwarg jobs enqueued by activejob 6.0.
result = transform_symbol_keys(result, symbol_keys)
result = Hash.ruby2_keywords_hash(result)
end
result
end
end
ActiveJob::Arguments.extend ActiveJobDeserializerExtension
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment