Provide a better error message for unsupported classes in serialize

We only support classes which provide a no-args constructor to use as a
default value. We can provide a more helpful error message if we catch
this when `serialize` is called, rather than letting it error when you
try to assign the attribute.

Fixes #18224
This commit is contained in:
Sean Griffin 2014-12-27 19:46:36 -07:00
parent cd84b27ca4
commit 307ec3db0f
3 changed files with 24 additions and 0 deletions

@ -1,3 +1,10 @@
* Provide a more helpful error message when an unsupported class is passed to
`serialize`
Fixes #18224
*Sean Griffin*
* Add bigint primary key support for MySQL.
Example:

@ -8,6 +8,7 @@ class YAMLColumn # :nodoc:
def initialize(object_class = Object)
@object_class = object_class
check_arity_of_constructor
end
def dump(obj)
@ -33,6 +34,16 @@ def load(yaml)
obj
end
private
def check_arity_of_constructor
begin
load(nil)
rescue ArgumentError
raise ArgumentError, "Cannot serialize #{object_class}. Classes passed to `serialize` must have a 0 argument constructor."
end
end
end
end
end

@ -264,4 +264,10 @@ def test_nil_is_not_changed_when_serialized_with_a_class
assert_not topic.content_changed?
end
def test_classes_without_no_arg_constructors_are_not_supported
assert_raises(ArgumentError) do
Topic.serialize(:content, Regexp)
end
end
end