aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord
diff options
context:
space:
mode:
Diffstat (limited to 'activerecord')
-rw-r--r--activerecord/lib/active_record/attribute_methods/serialization.rb6
-rw-r--r--activerecord/test/cases/attribute_methods/serialization_test.rb28
2 files changed, 33 insertions, 1 deletions
diff --git a/activerecord/lib/active_record/attribute_methods/serialization.rb b/activerecord/lib/active_record/attribute_methods/serialization.rb
index 9caf73e627..1287de0d0d 100644
--- a/activerecord/lib/active_record/attribute_methods/serialization.rb
+++ b/activerecord/lib/active_record/attribute_methods/serialization.rb
@@ -56,7 +56,11 @@ module ActiveRecord
end
def type_cast(value)
- value.unserialized_value @column.type_cast value.value
+ if value.state == :serialized
+ value.unserialized_value @column.type_cast value.value
+ else
+ value.unserialized_value
+ end
end
def type
diff --git a/activerecord/test/cases/attribute_methods/serialization_test.rb b/activerecord/test/cases/attribute_methods/serialization_test.rb
new file mode 100644
index 0000000000..8ce6192096
--- /dev/null
+++ b/activerecord/test/cases/attribute_methods/serialization_test.rb
@@ -0,0 +1,28 @@
+require "cases/helper"
+
+module ActiveRecord
+ module AttributeMethods
+ class SerializationTest < ActiveSupport::TestCase
+ class FakeColumn < Struct.new(:name)
+ def type; :integer; end
+ def type_cast(s); "#{s}!"; end
+ end
+
+ def test_type_cast_serialized_value
+ value = stub(state: :serialized, value: "Hello world")
+ value.expects(:unserialized_value).with("Hello world!")
+
+ type = Serialization::Type.new(FakeColumn.new)
+ type.type_cast(value)
+ end
+
+ def test_type_cast_unserialized_value
+ value = stub(state: :unserialized, value: "Hello world")
+ value.expects(:unserialized_value).with()
+
+ type = Serialization::Type.new(FakeColumn.new)
+ type.type_cast(value)
+ end
+ end
+ end
+end