1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Deprecate serialized_attributes without replacement

We've stopped using it internally, in favor of polymorphism. So should
you!
This commit is contained in:
Sean Griffin 2014-06-13 19:37:10 -06:00
parent 34221cdcab
commit 2df1540143
3 changed files with 26 additions and 18 deletions

View file

@ -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.

View file

@ -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

View file

@ -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