Skip to content

Commit a040493

Browse files
committed
workaround deserialize on save - rails/rails#44317
1 parent dc42c57 commit a040493

File tree

2 files changed

+31
-5
lines changed
  • lib/active_record/type/oracle_enhanced
  • spec/active_record/oracle_enhanced/type

2 files changed

+31
-5
lines changed

lib/active_record/type/oracle_enhanced/raw.rb

+10-2
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,21 @@ def type
1010
:raw
1111
end
1212

13+
# see ActiveModel::ActiveModel#forget_attribute_assignments
14+
# workaround for https://github.com/rails/rails/issues/44317
15+
def deserialize(value)
16+
value.respond_to?(:enhanced_deserialized) ? value.enhanced_deserialized : super
17+
end
18+
1319
def serialize(value)
1420
# Encode a string or byte array as string of hex codes
1521
if value.nil?
1622
super
1723
else
18-
value = value.unpack("C*")
19-
value.map { |x| "%02X" % x }.join
24+
value.unpack("C*").map { |x| "%02X" % x }.join.tap do |serialized|
25+
serialized.singleton_class.attr_accessor :enhanced_deserialized
26+
serialized.enhanced_deserialized = value
27+
end
2028
end
2129
end
2230
end

spec/active_record/oracle_enhanced/type/raw_spec.rb

+21-3
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
t.raw :binary_data, limit: 1024
1313
end
1414
end
15-
@binary_data = "\0\1\2\3\4\5\6\7\8\9" * 100
15+
@binary_data = ("\0\1\2\3\4\5\6\7\8\9" * 100).freeze
1616
@binary_data2 = "\1\2\3\4\5\6\7\8\9\0" * 100
1717
end
1818

@@ -38,6 +38,7 @@ class ::TestEmployee < ActiveRecord::Base
3838
last_name: "Last",
3939
binary_data: @binary_data
4040
)
41+
expect(@employee.binary_data).to eq(@binary_data)
4142
@employee.reload
4243
expect(@employee.binary_data).to eq(@binary_data)
4344
end
@@ -51,6 +52,7 @@ class ::TestEmployee < ActiveRecord::Base
5152
expect(@employee.binary_data).to be_nil
5253
@employee.binary_data = @binary_data
5354
@employee.save!
55+
expect(@employee.binary_data).to eq(@binary_data)
5456
@employee.reload
5557
expect(@employee.binary_data).to eq(@binary_data)
5658
end
@@ -77,6 +79,7 @@ class ::TestEmployee < ActiveRecord::Base
7779
@employee.reload
7880
@employee.binary_data = @binary_data2
7981
@employee.save!
82+
expect(@employee.binary_data).to eq(@binary_data2)
8083
@employee.reload
8184
expect(@employee.binary_data).to eq(@binary_data2)
8285
end
@@ -116,13 +119,14 @@ class ::TestEmployee < ActiveRecord::Base
116119
@employee.reload
117120
@employee.binary_data = @binary_data
118121
@employee.save!
122+
expect(@employee.binary_data).to eq(@binary_data)
119123
@employee.reload
120124
expect(@employee.binary_data).to eq(@binary_data)
121125
end
122126

123127
it "should allow equality on select" do
124128
TestEmployee.delete_all
125-
TestEmployee.create!(
129+
employee = TestEmployee.create!(
126130
first_name: "First",
127131
last_name: "Last",
128132
binary_data: @binary_data,
@@ -132,6 +136,20 @@ class ::TestEmployee < ActiveRecord::Base
132136
last_name: "Last1",
133137
binary_data: @binary_data2,
134138
)
135-
expect(TestEmployee.where(binary_data: @binary_data)).to have_attributes(count: 1)
139+
expect(TestEmployee.where(binary_data: @binary_data).to_a).to eq([employee])
140+
end
141+
142+
it "should allow equality on select with NULL value" do
143+
TestEmployee.delete_all
144+
employee = TestEmployee.create!(
145+
first_name: "First",
146+
last_name: "Last",
147+
)
148+
TestEmployee.create!(
149+
first_name: "First1",
150+
last_name: "Last1",
151+
binary_data: @binary_data2,
152+
)
153+
expect(TestEmployee.where(binary_data: nil).to_a).to eq([employee])
136154
end
137155
end

0 commit comments

Comments
 (0)