From 4e7217027f1933d5860ba459b3d23f454272f709 Mon Sep 17 00:00:00 2001 From: Sean Griffin Date: Tue, 10 Mar 2015 11:54:17 -0600 Subject: Add YAML compatibility for objects from Rails 4.2 As of Ruby 2.2, Psych can handle any object which is marshallable. This was not true on previous versions of Ruby, so our delegator types had to provide their own implementation of `init_with` and `encode_with`. Unfortunately, this doesn't match up with what Psych will do today. Since by the time we hit this layer, the objects will have already been created, I think it makes the most sense to just grab the current type from the class. --- activerecord/test/cases/yaml_serialization_test.rb | 44 +++++++++------------- 1 file changed, 18 insertions(+), 26 deletions(-) (limited to 'activerecord/test/cases/yaml_serialization_test.rb') diff --git a/activerecord/test/cases/yaml_serialization_test.rb b/activerecord/test/cases/yaml_serialization_test.rb index 65cd54e380..56909a8630 100644 --- a/activerecord/test/cases/yaml_serialization_test.rb +++ b/activerecord/test/cases/yaml_serialization_test.rb @@ -92,31 +92,7 @@ class YamlSerializationTest < ActiveRecord::TestCase end def test_deserializing_rails_41_yaml - yaml = <<-YAML.strip_heredoc - --- !ruby/object:Topic - attributes: - id: - title: The First Topic - author_name: David - author_email_address: david@loudthinking.com - written_on: 2003-07-16 14:28:11.223300000 Z - bonus_time: 2000-01-01 14:28:00.000000000 Z - last_read: 2004-04-15 - content: | - --- - :omg: :lol - important: - approved: false - replies_count: 1 - unique_replies_count: 0 - parent_id: - parent_title: - type: - group: - created_at: 2015-03-10 17:05:42.000000000 Z - updated_at: 2015-03-10 17:05:42.000000000 Z - YAML - topic = YAML.load(yaml) + topic = YAML.load(yaml_fixture("rails_4_1")) assert topic.new_record? assert_equal nil, topic.id @@ -124,6 +100,22 @@ class YamlSerializationTest < ActiveRecord::TestCase assert_equal({ omg: :lol }, topic.content) end - def test_deserializing_rails_42_yaml + def test_deserializing_rails_4_2_0_yaml + topic = YAML.load(yaml_fixture("rails_4_2_0")) + + assert_not topic.new_record? + assert_equal 1, topic.id + assert_equal "The First Topic", topic.title + assert_equal("Have a nice day", topic.content) + end + + private + + def yaml_fixture(file_name) + path = File.expand_path( + "../../support/yaml_compatibility_fixtures/#{file_name}.yml", + __FILE__ + ) + File.read(path) end end -- cgit v1.2.3