Permalink
Browse files

Merge pull request #15704 from sgrif/sg-deprecate-serialized

Deprecate `serialized_attributes` without replacement
  • Loading branch information...
2 parents 64220a1 + 2df1540 commit 69807afdd0e39025230c7c74b09db01f0cc160d6 @senny senny committed Jun 14, 2014
@@ -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.
@@ -3,20 +3,7 @@ module AttributeMethods
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 @@ def serialize(attr_name, class_name_or_coder = Object)
decorate_attribute_type(attr_name, :serialize) do |type|
Type::Serialized.new(type, coder)
end
+ end
+
+ 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
- # 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)
+ #{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
@@ -22,7 +22,9 @@ def test_serialize_does_not_eagerly_load_columns
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 @@ def test_serialized_column_should_unserialize_after_update_column
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

0 comments on commit 69807af

Please sign in to comment.