diff options
author | Yves Senn <yves.senn@gmail.com> | 2014-06-06 15:08:28 +0200 |
---|---|---|
committer | Yves Senn <yves.senn@gmail.com> | 2014-06-06 15:08:28 +0200 |
commit | 69274ce5163559419cf50ec2524103b93c12e2e9 (patch) | |
tree | 0a82fe6b693e5aa51a94308dea75343fe6eaeec2 /activerecord/test/cases/adapters/postgresql | |
parent | 485dab41c4ff7de392ffba132fbcbff49e6fe9ef (diff) | |
download | rails-69274ce5163559419cf50ec2524103b93c12e2e9.tar.gz rails-69274ce5163559419cf50ec2524103b93c12e2e9.tar.bz2 rails-69274ce5163559419cf50ec2524103b93c12e2e9.zip |
serialized Type should delegate `type_cast_for_write` to underlying Type
This adds a regression test for #14411, which was fixed by #15503.
Closes #14411
Closes #14595
Diffstat (limited to 'activerecord/test/cases/adapters/postgresql')
-rw-r--r-- | activerecord/test/cases/adapters/postgresql/hstore_test.rb | 35 |
1 files changed, 35 insertions, 0 deletions
diff --git a/activerecord/test/cases/adapters/postgresql/hstore_test.rb b/activerecord/test/cases/adapters/postgresql/hstore_test.rb index 78134ff316..a6482786c7 100644 --- a/activerecord/test/cases/adapters/postgresql/hstore_test.rb +++ b/activerecord/test/cases/adapters/postgresql/hstore_test.rb @@ -294,6 +294,41 @@ class PostgresqlHstoreTest < ActiveRecord::TestCase Hstore.update_all tags: { } assert_equal({ }, hstore.reload.tags) end + + # FIXME: remove this lambda once `serialize` no longer issues a db connection. + LAZY_MODELS = lambda do + return if defined?(TagCollection) + + class TagCollection + def initialize(hash); @hash = hash end + def to_hash; @hash end + def self.load(hash); new(hash) end + def self.dump(object); object.to_hash end + end + + class HstoreWithSerialize < Hstore + serialize :tags, TagCollection + end + end + + def test_hstore_with_serialized_attributes + LAZY_MODELS.call + HstoreWithSerialize.create! tags: TagCollection.new({"one" => "two"}) + record = HstoreWithSerialize.first + assert_instance_of TagCollection, record.tags + assert_equal({"one" => "two"}, record.tags.to_hash) + record.tags = TagCollection.new("three" => "four") + record.save! + assert_equal({"three" => "four"}, HstoreWithSerialize.first.tags.to_hash) + end + + def test_clone_hstore_with_serialized_attributes + LAZY_MODELS.call + HstoreWithSerialize.create! tags: TagCollection.new({"one" => "two"}) + record = HstoreWithSerialize.first + dupe = record.dup + assert_equal({"one" => "two"}, dupe.tags.to_hash) + end end private |