diff options
author | Sean Griffin <sean@thoughtbot.com> | 2014-12-27 19:46:36 -0700 |
---|---|---|
committer | Sean Griffin <sean@thoughtbot.com> | 2014-12-27 19:49:09 -0700 |
commit | 307ec3db0fe26cbd1811d34a27e6637726ce23ce (patch) | |
tree | 93fee6a30b053e6ad1892ddbadf63a3b429431e3 /activerecord/lib/active_record/coders | |
parent | cd84b27ca45aa6f18f38202fdbc0ecc0ce9f98ef (diff) | |
download | rails-307ec3db0fe26cbd1811d34a27e6637726ce23ce.tar.gz rails-307ec3db0fe26cbd1811d34a27e6637726ce23ce.tar.bz2 rails-307ec3db0fe26cbd1811d34a27e6637726ce23ce.zip |
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
Diffstat (limited to 'activerecord/lib/active_record/coders')
-rw-r--r-- | activerecord/lib/active_record/coders/yaml_column.rb | 11 |
1 files changed, 11 insertions, 0 deletions
diff --git a/activerecord/lib/active_record/coders/yaml_column.rb b/activerecord/lib/active_record/coders/yaml_column.rb index d3d7396c91..9ea22ed798 100644 --- a/activerecord/lib/active_record/coders/yaml_column.rb +++ b/activerecord/lib/active_record/coders/yaml_column.rb @@ -8,6 +8,7 @@ module ActiveRecord def initialize(object_class = Object) @object_class = object_class + check_arity_of_constructor end def dump(obj) @@ -33,6 +34,16 @@ module ActiveRecord 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 |