aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/attribute_methods/serialization.rb
diff options
context:
space:
mode:
authorSean Griffin <sean@thoughtbot.com>2014-06-13 19:37:10 -0600
committerSean Griffin <sean@thoughtbot.com>2014-06-14 06:21:28 -0600
commit2df1540143f1a57ab7aae8ff926266cc233292c5 (patch)
tree22bcc8fd9373786a1424f159db1b8e258bd79885 /activerecord/lib/active_record/attribute_methods/serialization.rb
parent34221cdcab7d145d22029174a990b3971616cc80 (diff)
downloadrails-2df1540143f1a57ab7aae8ff926266cc233292c5.tar.gz
rails-2df1540143f1a57ab7aae8ff926266cc233292c5.tar.bz2
rails-2df1540143f1a57ab7aae8ff926266cc233292c5.zip
Deprecate `serialized_attributes` without replacement
We've stopped using it internally, in favor of polymorphism. So should you!
Diffstat (limited to 'activerecord/lib/active_record/attribute_methods/serialization.rb')
-rw-r--r--activerecord/lib/active_record/attribute_methods/serialization.rb33
1 files changed, 17 insertions, 16 deletions
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
+
+ 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