提交 69274ce5 编写于 作者: Y Yves Senn

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
上级 485dab41
......@@ -20,7 +20,7 @@ def type_cast(value)
def type_cast_for_write(value)
return if value.nil?
unless is_default_value?(value)
coder.dump(value)
super coder.dump(value)
end
end
......
......@@ -294,6 +294,41 @@ def test_update_all
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
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册