aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/attribute_methods/serialization.rb
diff options
context:
space:
mode:
authorSean Griffin <sean@thoughtbot.com>2014-05-28 08:24:59 -0700
committerSean Griffin <sean@thoughtbot.com>2014-05-29 12:12:44 -0700
commit2eb547a4d9cfb4ed1c92397921402048162f5fc8 (patch)
tree4deededa75b1e55f6bcb0c8156334c51aab560b3 /activerecord/lib/active_record/attribute_methods/serialization.rb
parent4a3f71b6fb07f5bbf9e43b259a7429c96752e00b (diff)
downloadrails-2eb547a4d9cfb4ed1c92397921402048162f5fc8.tar.gz
rails-2eb547a4d9cfb4ed1c92397921402048162f5fc8.tar.bz2
rails-2eb547a4d9cfb4ed1c92397921402048162f5fc8.zip
Refactor serialized types to be partially defined as custom properties
Many of the methods defined in `AttributeMethods::Serialization` can be refactored onto this type as well, but this is a reasonable small step. Removes the `Type` class, and the need for `decorate_columns` to handle serialized types.
Diffstat (limited to 'activerecord/lib/active_record/attribute_methods/serialization.rb')
-rw-r--r--activerecord/lib/active_record/attribute_methods/serialization.rb26
1 files changed, 6 insertions, 20 deletions
diff --git a/activerecord/lib/active_record/attribute_methods/serialization.rb b/activerecord/lib/active_record/attribute_methods/serialization.rb
index 47c6f94ba7..e8c27cb8b8 100644
--- a/activerecord/lib/active_record/attribute_methods/serialization.rb
+++ b/activerecord/lib/active_record/attribute_methods/serialization.rb
@@ -58,32 +58,18 @@ module ActiveRecord
Coders::YAMLColumn.new(class_name_or_coder)
end
+ type = columns_hash[attr_name.to_s].cast_type
+ if type.serialized?
+ type = type.subtype
+ end
+ property attr_name, ActiveRecord::Type::Serialized.new(type)
+
# 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)
end
end
- class Type # :nodoc:
- delegate :type, :type_cast_for_database, to: :@column
-
- def initialize(column)
- @column = column
- end
-
- def type_cast(value)
- if value.state == :serialized
- value.unserialized_value @column.type_cast value.value
- else
- value.unserialized_value
- end
- end
-
- def accessor
- ActiveRecord::Store::IndifferentHashAccessor
- end
- end
-
class Attribute < Struct.new(:coder, :value, :state) # :nodoc:
def unserialized_value(v = value)
state == :serialized ? unserialize(v) : value