From 2e43ea494cb5559c95f28281fad6b58e2b57259d Mon Sep 17 00:00:00 2001 From: ur5us Date: Thu, 15 Feb 2024 10:04:07 +1300 Subject: [PATCH] Fix regression in serialization of nil/NULL values MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Regression introduced in #5012e0d6. This change partially reverts the change such that it works for Rails 6.1/7.0 and 7.1+. In Rails 7.1+ the API for `.serialize` changed. At first using `serialize :parameters, coder: YAML, type: Hash` seems to be backwards compatible with Rails 6.1/7.0 when reading the documentation/code but there’s a subtle difference: when the 2nd positional parameter (denoting the target type) is not provided a default mechanism is used. While the defaults will use `YAML` as the coder the serialization will only occur if the column is not `NULL`. So, without an explicit type parameter `nil`/`NULL` values are not converted to `Hash`. Thus, the explicit type parameter acts as the fallback for the value itself. Fixes #386 #384 #382 --- lib/public_activity/orm/active_record/activity.rb | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/lib/public_activity/orm/active_record/activity.rb b/lib/public_activity/orm/active_record/activity.rb index 42e4451..7ec55a5 100644 --- a/lib/public_activity/orm/active_record/activity.rb +++ b/lib/public_activity/orm/active_record/activity.rb @@ -36,7 +36,13 @@ class Activity < ::ActiveRecord::Base # Serialize parameters Hash begin if table_exists? - serialize :parameters, coder: YAML, type: Hash unless %i[json jsonb hstore].include?(columns_hash['parameters'].type) + unless %i[json jsonb hstore].include?(columns_hash['parameters'].type) + if ::ActiveRecord.version.release < Gem::Version.new('7.1') + serialize :parameters, Hash + else + serialize :parameters, coder: YAML, type: Hash + end + end else warn("[WARN] table #{name} doesn't exist. Skipping PublicActivity::Activity#parameters's serialization") end