Created
September 21, 2022 07:59
-
-
Save eliotsykes/c81ff06b948808532943eca5515d9623 to your computer and use it in GitHub Desktop.
ActiveJob compatibility for Rails 5.2 -> 6.0 upgrade
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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