提交 47f1c10c 编写于 作者: S Sean Griffin

Keep the types of virtual columns after yaml serialization

On MySQL and PostgreSQL, the adapter does not type cast virtual columns
for us.
上级 bfb8b139
......@@ -13,7 +13,7 @@ module Format
ISO_DATETIME = /\A(\d{4})-(\d\d)-(\d\d) (\d\d):(\d\d):(\d\d)(\.\d+)?\z/
end
attr_reader :name, :cast_type, :null, :sql_type, :default_function
attr_reader :name, :cast_type, :sql_type, :default_function
delegate :type, :precision, :scale, :limit, :klass, :accessor,
:text?, :number?, :binary?, :serialized?, :changed?,
......@@ -34,7 +34,7 @@ def initialize(name, default, cast_type, sql_type = nil, null = true)
@name = name
@cast_type = cast_type
@sql_type = sql_type
@null = null
@nullable = null
@original_default = default
@default_function = nil
end
......@@ -61,6 +61,10 @@ def with_type(type)
clone.instance_variable_set('@cast_type', type)
end
end
def null
@nullable
end
end
class NullColumn < Column
......
......@@ -356,6 +356,7 @@ def initialize_dup(other) # :nodoc:
def encode_with(coder)
coder['raw_attributes'] = @raw_attributes
coder['attributes'] = @attributes
coder['column_types'] = @column_types_override
coder['new_record'] = new_record?
end
......
......@@ -36,6 +36,17 @@ def accessor
ActiveRecord::Store::IndifferentHashAccessor
end
def init_with(coder)
@subtype = coder['subtype']
@coder = coder['coder']
__setobj__(@subtype)
end
def encode_with(coder)
coder['subtype'] = @subtype
coder['coder'] = @coder
end
private
def is_default_value?(value)
......
require 'cases/helper'
require 'models/topic'
require 'models/post'
require 'models/author'
class YamlSerializationTest < ActiveRecord::TestCase
fixtures :topics
fixtures :topics, :authors, :posts
def test_to_yaml_with_time_with_zone_should_not_raise_exception
with_timezone_config aware_attributes: true, zone: "Pacific Time (US & Canada)" do
......@@ -69,4 +71,15 @@ def test_new_records_remain_new_after_round_trip
assert_not topic.new_record?, "Loaded records without ID are not new"
assert_not YAML.load(YAML.dump(topic)).new_record?, "Record should not be new after deserialization"
end
def test_types_of_virtual_columns_are_not_changed_on_round_trip
author = Author.select('authors.*, count(posts.id) as posts_count')
.joins(:posts)
.group('authors.id')
.first
dumped = YAML.load(YAML.dump(author))
assert_equal 5, author.posts_count
assert_equal 5, dumped.posts_count
end
end
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册