From 2df1540143f1a57ab7aae8ff926266cc233292c5 Mon Sep 17 00:00:00 2001 From: Sean Griffin Date: Fri, 13 Jun 2014 19:37:10 -0600 Subject: [PATCH] Deprecate `serialized_attributes` without replacement We've stopped using it internally, in favor of polymorphism. So should you! --- activerecord/CHANGELOG.md | 5 +++ .../attribute_methods/serialization.rb | 33 ++++++++++--------- .../test/cases/serialized_attribute_test.rb | 6 ++-- 3 files changed, 26 insertions(+), 18 deletions(-) diff --git a/activerecord/CHANGELOG.md b/activerecord/CHANGELOG.md index f15bd6261b..6124518523 100644 --- a/activerecord/CHANGELOG.md +++ b/activerecord/CHANGELOG.md @@ -1,3 +1,8 @@ +* Deprecate `serialized_attributes` without replacement. You can access its + behavior by going through the column's type object. + + *Sean Griffin* + * Correctly extract IPv6 addresses from `DATABASE_URI`: the square brackets are part of the URI structure, not the actual host. diff --git a/activerecord/lib/active_record/attribute_methods/serialization.rb b/activerecord/lib/active_record/attribute_methods/serialization.rb index cec50f62a3..d06d5bcd92 100644 --- a/activerecord/lib/active_record/attribute_methods/serialization.rb +++ b/activerecord/lib/active_record/attribute_methods/serialization.rb @@ -3,20 +3,7 @@ module ActiveRecord module Serialization extend ActiveSupport::Concern - included do - # Returns a hash of all the attributes that have been specified for - # serialization as keys and their class restriction as values. - class_attribute :serialized_attributes, instance_accessor: false - self.serialized_attributes = {} - end - module ClassMethods - ## - # :method: serialized_attributes - # - # Returns a hash of all the attributes that have been specified for - # serialization as keys and their class restriction as values. - # If you have an attribute that needs to be saved to the database as an # object, and retrieved as the same object, then specify the name of that # attribute using this method and it will be handled automatically. The @@ -59,10 +46,24 @@ module ActiveRecord decorate_attribute_type(attr_name, :serialize) do |type| Type::Serialized.new(type, coder) end + end - # merge new serialized attribute and create new hash to ensure that each class in inheritance hierarchy - # has its own hash of own serialized attributes - self.serialized_attributes = serialized_attributes.merge(attr_name.to_s => coder) + def serialized_attributes + ActiveSupport::Deprecation.warn(<<-WARNING.strip_heredoc) + `serialized_attributes` is deprecated, and will be removed in Rails 5.0. + If you need to access the serialization behavior, you can do: + + #{self.class.name}.column_for_attribute('foo').type_cast_for_database(value) + + or + + #{self.class.name}.column_for_attribute('foo').type_cast_from_database(value) + WARNING + @serialized_attributes ||= Hash[ + columns.select { |t| t.cast_type.is_a?(Type::Serialized) }.map { |c| + [c.name, c.cast_type.coder] + } + ] end end end diff --git a/activerecord/test/cases/serialized_attribute_test.rb b/activerecord/test/cases/serialized_attribute_test.rb index 82df0a5f08..186a1a2ade 100644 --- a/activerecord/test/cases/serialized_attribute_test.rb +++ b/activerecord/test/cases/serialized_attribute_test.rb @@ -22,7 +22,9 @@ class SerializedAttributeTest < ActiveRecord::TestCase end def test_list_of_serialized_attributes - assert_equal %w(content), Topic.serialized_attributes.keys + assert_deprecated do + assert_equal %w(content), Topic.serialized_attributes.keys + end end def test_serialized_attribute @@ -207,7 +209,7 @@ class SerializedAttributeTest < ActiveRecord::TestCase t = Topic.create(content: "first") assert_equal("first", t.content) - t.update_column(:content, Topic.serialized_attributes["content"].dump("second")) + t.update_column(:content, Topic.type_for_attribute('content').type_cast_for_database("second")) assert_equal("second", t.content) end