diff options
author | Rafael Mendonça França <rafaelmfranca@gmail.com> | 2014-06-10 11:47:25 -0300 |
---|---|---|
committer | Rafael Mendonça França <rafaelmfranca@gmail.com> | 2014-06-10 11:47:25 -0300 |
commit | 4bd2a50f882065ab4113df4c5711dd1a156a465b (patch) | |
tree | 1494aae135529e4724b2f18e8bf300e9e043895a | |
parent | d5f480c5324c02d89b23e24a92a7a4ea58de59bc (diff) | |
parent | 47f1c10c8fb7ed9f8999dda5b4a7f42e34afeec6 (diff) | |
download | rails-4bd2a50f882065ab4113df4c5711dd1a156a465b.tar.gz rails-4bd2a50f882065ab4113df4c5711dd1a156a465b.tar.bz2 rails-4bd2a50f882065ab4113df4c5711dd1a156a465b.zip |
Merge pull request #15609 from sgrif/sg-yaml-virtual-columns
Keep the types of virtual columns after yaml serialization
4 files changed, 32 insertions, 3 deletions
diff --git a/activerecord/lib/active_record/connection_adapters/column.rb b/activerecord/lib/active_record/connection_adapters/column.rb index 72c6990ba5..21273364b9 100644 --- a/activerecord/lib/active_record/connection_adapters/column.rb +++ b/activerecord/lib/active_record/connection_adapters/column.rb @@ -13,7 +13,7 @@ module ActiveRecord 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 @@ module ActiveRecord @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 @@ module ActiveRecord clone.instance_variable_set('@cast_type', type) end end + + def null + @nullable + end end class NullColumn < Column diff --git a/activerecord/lib/active_record/core.rb b/activerecord/lib/active_record/core.rb index 79388f53b5..7edaf256c7 100644 --- a/activerecord/lib/active_record/core.rb +++ b/activerecord/lib/active_record/core.rb @@ -356,6 +356,7 @@ module ActiveRecord def encode_with(coder) coder['raw_attributes'] = @raw_attributes coder['attributes'] = @attributes + coder['column_types'] = @column_types_override coder['new_record'] = new_record? end diff --git a/activerecord/lib/active_record/type/serialized.rb b/activerecord/lib/active_record/type/serialized.rb index 9144028576..e6d84eadc0 100644 --- a/activerecord/lib/active_record/type/serialized.rb +++ b/activerecord/lib/active_record/type/serialized.rb @@ -36,6 +36,17 @@ module ActiveRecord 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) diff --git a/activerecord/test/cases/yaml_serialization_test.rb b/activerecord/test/cases/yaml_serialization_test.rb index d4f8ef5b4d..9f1d110ddb 100644 --- a/activerecord/test/cases/yaml_serialization_test.rb +++ b/activerecord/test/cases/yaml_serialization_test.rb @@ -1,8 +1,10 @@ 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 @@ class YamlSerializationTest < ActiveRecord::TestCase 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 |