Skip to content

Commit b717a5a

Browse files
committed
Revert "Merge pull request rails#47352 from basecamp/ar-freeze-cached"
This reverts commit 2c20f90, reversing changes made to 912096d.
1 parent 2c20f90 commit b717a5a

File tree

6 files changed

+7
-43
lines changed

6 files changed

+7
-43
lines changed

activemodel/lib/active_model/attribute.rb

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -115,17 +115,13 @@ def has_been_read?
115115
def ==(other)
116116
self.class == other.class &&
117117
name == other.name &&
118-
identifier_value == other.identifier_value &&
118+
value_before_type_cast == other.value_before_type_cast &&
119119
type == other.type
120120
end
121121
alias eql? ==
122122

123123
def hash
124-
[self.class, name, identifier_value, type].hash
125-
end
126-
127-
def identifier_value
128-
value_before_type_cast
124+
[self.class, name, value_before_type_cast, type].hash
129125
end
130126

131127
def init_with(coder)

activemodel/lib/active_model/type/helpers/mutable.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ module ActiveModel
44
module Type
55
module Helpers # :nodoc: all
66
module Mutable
7+
def immutable_value(value)
8+
value.deep_dup.freeze
9+
end
10+
711
def cast(value)
812
deserialize(serialize(value))
913
end

activerecord/CHANGELOG.md

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
* Fix error when querying serialized Class attributes.
2-
3-
*Jorge Manrubia*
4-
51
* Add support for `Array#intersect?` to `ActiveRecord::Relation`.
62

73
`Array#intersect?` is only available on Ruby 3.1 or later.

activerecord/lib/active_record/relation/predicate_builder.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ def build(attribute, value, operator = nil)
6666

6767
def build_bind_attribute(column_name, value)
6868
type = table.type(column_name)
69-
Relation::QueryAttribute.new(column_name, value, type)
69+
Relation::QueryAttribute.new(column_name, type.immutable_value(value), type)
7070
end
7171

7272
def resolve_arel_attribute(table_name, column_name, &block)

activerecord/lib/active_record/relation/query_attribute.rb

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -36,24 +36,10 @@ def unboundable?
3636
@_unboundable
3737
end
3838

39-
def identifier_value
40-
if unboundable?
41-
super
42-
else
43-
# Override to define equality based on frozen casted values to prevent caching
44-
# issues with mutable database query values.
45-
value_for_database
46-
end
47-
end
48-
4939
private
5040
def infinity?(value)
5141
value.respond_to?(:infinite?) && value.infinite?
5242
end
53-
54-
def _value_for_database
55-
super.deep_dup.freeze
56-
end
5743
end
5844
end
5945
end

activerecord/test/cases/serialized_attribute_test.rb

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,6 @@
99
class SerializedAttributeTest < ActiveRecord::TestCase
1010
def setup
1111
ActiveRecord.use_yaml_unsafe_load = true
12-
@yaml_column_permitted_classes_default = ActiveRecord.yaml_column_permitted_classes
13-
end
14-
15-
def teardown
16-
ActiveRecord.yaml_column_permitted_classes = @yaml_column_permitted_classes_default
1712
end
1813

1914
fixtures :topics, :posts
@@ -28,10 +23,6 @@ class ImportantTopic < Topic
2823
serialize :important, type: Hash
2924
end
3025

31-
class ClassifiedTopic < Topic
32-
serialize :important, type: Class
33-
end
34-
3526
teardown do
3627
Topic.serialize("content")
3728
end
@@ -175,14 +166,6 @@ def test_serialized_string_attribute
175166
assert_equal(myobj, topic.content)
176167
end
177168

178-
def test_serialized_class_attribute
179-
ActiveRecord.yaml_column_permitted_classes += [Class]
180-
181-
topic = ClassifiedTopic.create(important: Symbol).reload
182-
assert_equal(Symbol, topic.important)
183-
assert_not_empty ClassifiedTopic.where(important: Symbol)
184-
end
185-
186169
def test_nil_serialized_attribute_without_class_constraint
187170
topic = Topic.new
188171
assert_nil topic.content
@@ -568,7 +551,6 @@ def test_serialized_attribute_works_under_concurrent_initial_access
568551

569552
class SerializedAttributeTestWithYamlSafeLoad < SerializedAttributeTest
570553
def setup
571-
super
572554
ActiveRecord.use_yaml_unsafe_load = false
573555
end
574556

0 commit comments

Comments
 (0)