ActiveRecord::Coders::YAMLColumn#dump should raise an error

closes #2737

Conflicts:

	activerecord/lib/active_record/coders/yaml_column.rb
This commit is contained in:
Guten 2011-08-29 23:29:11 +08:00 committed by Piotr Sarnacki
parent 2e8129e87a
commit 9637de6b47
2 changed files with 14 additions and 1 deletions

View File

@ -15,7 +15,13 @@ module ActiveRecord
end
def dump(obj)
YAML.dump(obj) unless obj.nil?
return if obj.nil?
unless obj.is_a?(object_class)
raise SerializationTypeMismatch,
"Attribute was supposed to be a #{object_class}, but was a #{obj.class}. -- #{obj.inspect}"
end
YAML.dump obj
end
def load(yaml)

View File

@ -9,6 +9,13 @@ module ActiveRecord
assert_equal Object, coder.object_class
end
def test_type_mismatch_on_different_classes_on_dump
coder = YAMLColumn.new(Array)
assert_raises(SerializationTypeMismatch) do
coder.dump("a")
end
end
def test_type_mismatch_on_different_classes
coder = YAMLColumn.new(Array)
assert_raises(SerializationTypeMismatch) do