Move SQLite3 blob encoding to ActiveModel

This commit is contained in:
Ole Friis Østergaard 2023-03-02 15:03:28 +00:00
parent d624a8e260
commit f4242739aa
3 changed files with 27 additions and 6 deletions

View File

@ -46,6 +46,7 @@ module ActiveModel
def serialize(value)
case value
when ::Numeric, ::Symbol, ActiveSupport::Duration then value.to_s
when ::String then serialize_cast_value(value)
when true then @true
when false then @false
else super
@ -53,6 +54,12 @@ module ActiveModel
end
def serialize_cast_value(value) # :nodoc:
if value&.encoding == Encoding::BINARY
# If we can treat the bytes as UTF-8 without changing them, then use UTF-8 as encoding
new_value = value.dup.force_encoding(Encoding::UTF_8)
return new_value if new_value.valid_encoding?
end
value
end

View File

@ -17,6 +17,26 @@ module ActiveModel
assert_same s, type.cast(s)
assert_same s, type.deserialize(s)
end
test "leaves validly encoded strings untouched" do
s = "string with àccénts".encode(Encoding::ISO_8859_1)
type = Type::ImmutableString.new
assert_same s, type.serialize(s)
end
test "serializes valid, binary-encoded strings to UTF-8" do
s = "string with àccénts".b
type = Type::ImmutableString.new
serialized = type.serialize(s)
assert_equal Encoding::UTF_8, serialized.encoding
assert_equal s.bytes, serialized.bytes
end
test "leaves true binary data untouched" do
binary_data = "\xEE\x49\xC7".b
type = Type::ImmutableString.new
assert_same binary_data, type.serialize(binary_data)
end
end
end
end

View File

@ -79,12 +79,6 @@ module ActiveRecord
case value
when BigDecimal
value.to_f
when String
if value.encoding == Encoding::ASCII_8BIT
super(value.encode(Encoding::UTF_8))
else
super
end
else
super
end