aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib
diff options
context:
space:
mode:
authorSean Griffin <sean@thoughtbot.com>2015-03-10 11:54:17 -0600
committerSean Griffin <sean@thoughtbot.com>2015-03-10 11:56:45 -0600
commit4e7217027f1933d5860ba459b3d23f454272f709 (patch)
treeaf7c4945c79c3abf42099d4bcec012b01280dc6f /activerecord/lib
parentafc124c3b417b5327ed4d85dc34393f3d19bfbcf (diff)
downloadrails-4e7217027f1933d5860ba459b3d23f454272f709.tar.gz
rails-4e7217027f1933d5860ba459b3d23f454272f709.tar.bz2
rails-4e7217027f1933d5860ba459b3d23f454272f709.zip
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.
Diffstat (limited to 'activerecord/lib')
-rw-r--r--activerecord/lib/active_record/legacy_yaml_adapter.rb18
1 files changed, 17 insertions, 1 deletions
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