diff options
| author | Rafael França <rafaelmfranca@gmail.com> | 2017-08-14 17:40:13 -0400 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2017-08-14 17:40:13 -0400 |
| commit | efad56a0d93c51d7ebce5007a80df522335430bd (patch) | |
| tree | df314458c113bad4ca2e0a8ae3a9caffd36324c0 /activerecord/test/cases/adapters/postgresql | |
| parent | 509dfdca98cf82244c33deae66baf7175279400c (diff) | |
| parent | c847e17300f4cacfcabf79bea746700697ce57ff (diff) | |
| download | rails-efad56a0d93c51d7ebce5007a80df522335430bd.tar.gz rails-efad56a0d93c51d7ebce5007a80df522335430bd.tar.bz2 rails-efad56a0d93c51d7ebce5007a80df522335430bd.zip | |
Merge pull request #30229 from kamipo/allow_serialize_with_custom_coder
Allow `serialize` with a custom coder on `json` and `array` columns
Diffstat (limited to 'activerecord/test/cases/adapters/postgresql')
| -rw-r--r-- | activerecord/test/cases/adapters/postgresql/array_test.rb | 26 |
1 files changed, 25 insertions, 1 deletions
diff --git a/activerecord/test/cases/adapters/postgresql/array_test.rb b/activerecord/test/cases/adapters/postgresql/array_test.rb index 08b17f37e2..0e9e86f425 100644 --- a/activerecord/test/cases/adapters/postgresql/array_test.rb +++ b/activerecord/test/cases/adapters/postgresql/array_test.rb @@ -47,7 +47,7 @@ class PostgresqlArrayTest < ActiveRecord::PostgreSQLTestCase assert ratings_column.array? end - def test_not_compatible_with_serialize + def test_not_compatible_with_serialize_array new_klass = Class.new(PgArray) do serialize :tags, Array end @@ -56,6 +56,30 @@ class PostgresqlArrayTest < ActiveRecord::PostgreSQLTestCase end end + class MyTags + def initialize(tags); @tags = tags end + def to_a; @tags end + def self.load(tags); new(tags) end + def self.dump(object); object.to_a end + end + + def test_array_with_serialized_attributes + new_klass = Class.new(PgArray) do + serialize :tags, MyTags + end + + new_klass.create!(tags: MyTags.new(["one", "two"])) + record = new_klass.first + + assert_instance_of MyTags, record.tags + assert_equal ["one", "two"], record.tags.to_a + + record.tags = MyTags.new(["three", "four"]) + record.save! + + assert_equal ["three", "four"], record.reload.tags.to_a + end + def test_default @connection.add_column "pg_arrays", "score", :integer, array: true, default: [4, 4, 2] PgArray.reset_column_information |
