Skip to content

Commit 02ee081

Browse files
committed
Merge pull request rails#15435 from sgrif/sg-rm-serialization
Remove most code related to serialized properties
2 parents 260c384 + 90c8be7 commit 02ee081

File tree

12 files changed

+30
-103
lines changed

12 files changed

+30
-103
lines changed

activerecord/lib/active_record/attribute_methods.rb

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -287,11 +287,6 @@ def attributes
287287
}
288288
end
289289

290-
# Placeholder so it can be overriden when needed by serialization
291-
def attributes_for_coder # :nodoc:
292-
attributes_before_type_cast
293-
end
294-
295290
# Returns an <tt>#inspect</tt>-like string for the value of the
296291
# attribute +attr_name+. String attributes are truncated upto 50
297292
# characters, Date and Time attributes are returned in the

activerecord/lib/active_record/attribute_methods/read.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ def #{temp_method}
9494

9595
def cacheable_column?(column)
9696
if attribute_types_cached_by_default == ATTRIBUTE_TYPES_CACHED_BY_DEFAULT
97-
! serialized_attributes.include? column.name
97+
true
9898
else
9999
attribute_types_cached_by_default.include?(column.type)
100100
end

activerecord/lib/active_record/attribute_methods/serialization.rb

Lines changed: 0 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -76,21 +76,6 @@ def serialize(attr_name, class_name_or_coder = Object)
7676
module Behavior # :nodoc:
7777
extend ActiveSupport::Concern
7878

79-
module ClassMethods # :nodoc:
80-
def initialize_attributes(attributes, options = {})
81-
serialized = (options.delete(:serialized) { true }) ? :serialized : :unserialized
82-
super(attributes, options)
83-
84-
serialized_attributes.each do |key, coder|
85-
if attributes.key?(key)
86-
attributes[key] = Type::Serialized::Attribute.new(coder, attributes[key], serialized)
87-
end
88-
end
89-
90-
attributes
91-
end
92-
end
93-
9479
def should_record_timestamps?
9580
super || (self.record_timestamps && (attributes.keys & self.class.serialized_attributes.keys).present?)
9681
end
@@ -106,42 +91,6 @@ def _field_changed?(attr, old, value)
10691
super
10792
end
10893
end
109-
110-
def read_attribute_before_type_cast(attr_name)
111-
if self.class.serialized_attributes.include?(attr_name)
112-
super.unserialized_value
113-
else
114-
super
115-
end
116-
end
117-
118-
def attributes_before_type_cast
119-
super.dup.tap do |attributes|
120-
self.class.serialized_attributes.each_key do |key|
121-
if attributes.key?(key)
122-
attributes[key] = attributes[key].unserialized_value
123-
end
124-
end
125-
end
126-
end
127-
128-
def typecasted_attribute_value(name)
129-
if self.class.serialized_attributes.include?(name)
130-
@raw_attributes[name].serialized_value
131-
else
132-
super
133-
end
134-
end
135-
136-
def attributes_for_coder
137-
attribute_names.each_with_object({}) do |name, attrs|
138-
attrs[name] = if self.class.serialized_attributes.include?(name)
139-
@raw_attributes[name].serialized_value
140-
else
141-
read_attribute_before_type_cast(name)
142-
end
143-
end
144-
end
14594
end
14695
end
14796
end

activerecord/lib/active_record/attribute_methods/write.rb

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,11 @@ def __temp__#{safe_name}=(value)
5353
# specified +value+. Empty strings for fixnum and float columns are
5454
# turned into +nil+.
5555
def write_attribute(attr_name, value)
56-
write_attribute_with_type_cast(attr_name, value, :type_cast_for_write)
56+
write_attribute_with_type_cast(attr_name, value, true)
5757
end
5858

5959
def raw_write_attribute(attr_name, value)
60-
write_attribute_with_type_cast(attr_name, value, :raw_type_cast_for_write)
60+
write_attribute_with_type_cast(attr_name, value, false)
6161
end
6262

6363
private
@@ -66,7 +66,7 @@ def attribute=(attribute_name, value)
6666
write_attribute(attribute_name, value)
6767
end
6868

69-
def write_attribute_with_type_cast(attr_name, value, type_cast_method)
69+
def write_attribute_with_type_cast(attr_name, value, should_type_cast)
7070
attr_name = attr_name.to_s
7171
attr_name = self.class.primary_key if attr_name == 'id' && self.class.primary_key
7272
@attributes.delete(attr_name)
@@ -78,9 +78,9 @@ def write_attribute_with_type_cast(attr_name, value, type_cast_method)
7878
@attributes[attr_name] = value
7979
end
8080

81-
if column
82-
@raw_attributes[attr_name] = column.public_send(type_cast_method, value)
83-
elsif @raw_attributes.has_key?(attr_name)
81+
if column && should_type_cast
82+
@raw_attributes[attr_name] = column.type_cast_for_write(value)
83+
elsif !should_type_cast || @raw_attributes.has_key?(attr_name)
8484
@raw_attributes[attr_name] = value
8585
else
8686
raise ActiveModel::MissingAttributeError, "can't write unknown attribute `#{attr_name}'"

activerecord/lib/active_record/connection_adapters/column.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ module Format
1717

1818
delegate :type, :precision, :scale, :limit, :klass, :accessor,
1919
:text?, :number?, :binary?, :serialized?,
20-
:type_cast, :type_cast_for_write, :raw_type_cast_for_write, :type_cast_for_database,
20+
:type_cast, :type_cast_for_write, :type_cast_for_database,
2121
:type_cast_for_schema,
2222
to: :cast_type
2323

@@ -52,7 +52,7 @@ def human_name
5252
end
5353

5454
def extract_default(default)
55-
type_cast(default)
55+
type_cast_for_write(type_cast(default))
5656
end
5757
end
5858
end

activerecord/lib/active_record/core.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -353,7 +353,7 @@ def initialize_dup(other) # :nodoc:
353353
# Post.new.encode_with(coder)
354354
# coder # => {"attributes" => {"id" => nil, ... }}
355355
def encode_with(coder)
356-
coder['attributes'] = attributes_for_coder
356+
coder['attributes'] = @raw_attributes
357357
end
358358

359359
# Returns true if +comparison_object+ is the same exact object, or +comparison_object+

activerecord/lib/active_record/type/serialized.rb

Lines changed: 11 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,21 @@ def initialize(subtype, coder)
1010
end
1111

1212
def type_cast(value)
13-
if value.respond_to?(:unserialized_value)
14-
value.unserialized_value(super(value.value))
13+
if is_default_value?(value)
14+
value
1515
else
16-
super
16+
coder.load(super)
1717
end
1818
end
1919

2020
def type_cast_for_write(value)
21-
Attribute.new(coder, value, :unserialized)
21+
return if value.nil?
22+
unless is_default_value?(value)
23+
coder.dump(value)
24+
end
2225
end
2326

24-
def raw_type_cast_for_write(value)
25-
Attribute.new(coder, value, :serialized)
26-
end
27+
alias type_cast_for_database type_cast_for_write
2728

2829
def serialized?
2930
true
@@ -33,24 +34,10 @@ def accessor
3334
ActiveRecord::Store::IndifferentHashAccessor
3435
end
3536

36-
class Attribute < Struct.new(:coder, :value, :state) # :nodoc:
37-
def unserialized_value(v = value)
38-
state == :serialized ? unserialize(v) : value
39-
end
40-
41-
def serialized_value
42-
state == :unserialized ? serialize : value
43-
end
44-
45-
def unserialize(v)
46-
self.state = :unserialized
47-
self.value = coder.load(v)
48-
end
37+
private
4938

50-
def serialize
51-
self.state = :serialized
52-
self.value = coder.dump(value)
53-
end
39+
def is_default_value?(value)
40+
value == coder.load(nil)
5441
end
5542
end
5643
end

activerecord/lib/active_record/type/value.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@ def klass # :nodoc:
5454
def type_cast_for_write(value) # :nodoc:
5555
value
5656
end
57-
alias_method :raw_type_cast_for_write, :type_cast_for_write # :internal:
5857

5958
private
6059

activerecord/lib/active_record/validations/uniqueness.rb

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ def validate_each(record, attribute, value)
1414
finder_class = find_finder_class_for(record)
1515
table = finder_class.arel_table
1616
value = map_enum_attribute(finder_class, attribute, value)
17-
value = deserialize_attribute(record, attribute, value)
1817

1918
relation = build_relation(finder_class, table, attribute, value)
2019
relation = relation.and(table[finder_class.primary_key.to_sym].not_eq(record.id)) if record.persisted?
@@ -86,12 +85,6 @@ def scope_relation(record, table, relation)
8685
relation
8786
end
8887

89-
def deserialize_attribute(record, attribute, value)
90-
coder = record.class.serialized_attributes[attribute.to_s]
91-
value = coder.dump value if value && coder
92-
value
93-
end
94-
9588
def map_enum_attribute(klass, attribute, value)
9689
mapping = klass.defined_enums[attribute.to_s]
9790
value = mapping[value] if value && mapping

activerecord/test/cases/adapters/postgresql/composite_test.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ def type_cast(value)
9393
end
9494

9595
def type_cast_for_write(value)
96+
return if value.nil?
9697
"(#{value.city},#{value.street})"
9798
end
9899
end

activerecord/test/cases/attribute_methods_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -870,7 +870,7 @@ def new_topic_like_ar_class(&block)
870870
end
871871

872872
def cached_columns
873-
Topic.columns.map(&:name) - Topic.serialized_attributes.keys
873+
Topic.columns.map(&:name)
874874
end
875875

876876
def time_related_columns_on_topic

activerecord/test/cases/serialized_attribute_test.rb

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,9 @@ def test_serialized_attributes_from_database_on_subclass
5959
def test_serialized_attribute_calling_dup_method
6060
Topic.serialize :content, JSON
6161

62-
t = Topic.new(:content => { :foo => :bar }).dup
63-
assert_equal({ :foo => :bar }, t.content_before_type_cast)
62+
orig = Topic.new(content: { foo: :bar })
63+
clone = orig.dup
64+
assert_equal(orig.content, clone.content)
6465
end
6566

6667
def test_serialized_attribute_declared_in_subclass
@@ -103,8 +104,10 @@ def test_nil_not_serialized_with_class_constraint
103104

104105
def test_serialized_attribute_should_raise_exception_on_save_with_wrong_type
105106
Topic.serialize(:content, Hash)
106-
topic = Topic.new(:content => "string")
107-
assert_raise(ActiveRecord::SerializationTypeMismatch) { topic.save }
107+
assert_raise(ActiveRecord::SerializationTypeMismatch) do
108+
topic = Topic.new(content: 'string')
109+
topic.save
110+
end
108111
end
109112

110113
def test_should_raise_exception_on_serialized_attribute_with_type_mismatch

0 commit comments

Comments
 (0)