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/lib/active_record/legacy_yaml_adapter.rb | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) (limited to 'activerecord/lib') diff --git a/activerecord/lib/active_record/legacy_yaml_adapter.rb b/activerecord/lib/active_record/legacy_yaml_adapter.rb index 0d018d4352..89dee58423 100644 --- a/activerecord/lib/active_record/legacy_yaml_adapter.rb +++ b/activerecord/lib/active_record/legacy_yaml_adapter.rb @@ -7,13 +7,29 @@ module ActiveRecord when 1 then coder else if coder["attributes"].is_a?(AttributeSet) - coder + Rails420.convert(klass, coder) else Rails41.convert(klass, coder) end end end + module Rails420 + def self.convert(klass, coder) + attribute_set = coder["attributes"] + + klass.attribute_names.each do |attr_name| + attribute = attribute_set[attr_name] + if attribute.type.is_a?(Delegator) + type_from_klass = klass.type_for_attribute(attr_name) + attribute_set[attr_name] = attribute.with_type(type_from_klass) + end + end + + coder + end + end + module Rails41 def self.convert(klass, coder) attributes = klass.attributes_builder -- cgit v1.2.3