diff --git a/lib/patches/active_record/rails7_1/serialization.rb b/lib/patches/active_record/rails7_1/serialization.rb index 96e2615f..0c204fd9 100644 --- a/lib/patches/active_record/rails7_1/serialization.rb +++ b/lib/patches/active_record/rails7_1/serialization.rb @@ -1,15 +1,25 @@ module Globalize module AttributeMethods module Serialization - def serialize(attr_name, type: Object, **options) - super(attr_name, type: type, **options) + def serialize(attr_name, class_name_or_coder = Object, **options) + if class_name_or_coder == ::JSON || [:load, :dump].all? { |x| class_name_or_coder.respond_to?(x) } + options = options.merge(coder: class_name_or_coder, type: Object) + else + options = options.merge(code: default_column_serializer, type: class_name_or_coder) + end - coder = if type == ::JSON + super(attr_name, **options) + + coder = if options[:coder] == ::JSON ::ActiveRecord::Coders::JSON - elsif [:load, :dump].all? { |x| type.respond_to?(x) } - type + elsif options.key?(:coder) + if coder.respond_to?(:new) && !coder.respond_to?(:load) + coder.new(attr_name, type) + else + options[:coder] + end else - ::ActiveRecord::Coders::YAMLColumn.new(attr_name, type) + ::ActiveRecord::Coders::YAMLColumn.new(attr_name, options[:type], **(options.fetch(:yaml, {}))) end self.globalize_serialized_attributes = globalize_serialized_attributes.dup