Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Backport 7ba3a48 to 5.0 #26344

Merged
merged 1 commit into from
Aug 31, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 7 additions & 0 deletions activerecord/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
* PostgreSQL array columns will now respect the encoding of strings contained
in the array.

Fixes #26326.

*Sean Griffin*

* Inverse association instances will now be set before `after_find` or
`after_initialize` callbacks are run.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,11 @@ def cast(value)

def serialize(value)
if value.is_a?(::Array)
@pg_encoder.encode(type_cast_array(value, :serialize))
result = @pg_encoder.encode(type_cast_array(value, :serialize))
if encoding = determine_encoding_of_strings(value)
result.encode!(encoding)
end
result
else
super
end
Expand Down Expand Up @@ -63,6 +67,13 @@ def type_cast_array(value, method)
@subtype.public_send(method, value)
end
end

def determine_encoding_of_strings(value)
case value
when ::Array then determine_encoding_of_strings(value.first)
when ::String then value.encoding
end
end
end
end
end
Expand Down
6 changes: 6 additions & 0 deletions activerecord/test/cases/adapters/postgresql/array_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,12 @@ def self.model_name; ActiveModel::Name.new(PgArray) end
assert_equal ["has already been taken"], e2.errors[:tags], "Should have uniqueness message for tags"
end

def test_encoding_arrays_of_utf8_strings
string_with_utf8 = "nový"
assert_equal [string_with_utf8], @type.deserialize(@type.serialize([string_with_utf8]))
assert_equal [[string_with_utf8]], @type.deserialize(@type.serialize([[string_with_utf8]]))
end

private
def assert_cycle field, array
# test creation
Expand Down