diff options
author | Kasper Timm Hansen <kaspth@gmail.com> | 2017-02-06 09:27:23 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-02-06 09:27:23 +0100 |
commit | 2bbea8b0996a45f9bdfafe3b63f123cca4614e10 (patch) | |
tree | 213ae3bed9a500a11b32a37c00abe673d75de793 /activerecord | |
parent | b9ae7481fd5653fc6430148f06e0fca27317829e (diff) | |
parent | 1c1aba775826589b5038959120ea57e050e38123 (diff) | |
download | rails-2bbea8b0996a45f9bdfafe3b63f123cca4614e10.tar.gz rails-2bbea8b0996a45f9bdfafe3b63f123cca4614e10.tar.bz2 rails-2bbea8b0996a45f9bdfafe3b63f123cca4614e10.zip |
Merge pull request #27902 from kirs/yaml-column-error
Indicate action that failed in YamlColumn
Diffstat (limited to 'activerecord')
4 files changed, 10 insertions, 10 deletions
diff --git a/activerecord/lib/active_record/coders/yaml_column.rb b/activerecord/lib/active_record/coders/yaml_column.rb index 2136da43fe..9c52a31b95 100644 --- a/activerecord/lib/active_record/coders/yaml_column.rb +++ b/activerecord/lib/active_record/coders/yaml_column.rb @@ -14,7 +14,7 @@ module ActiveRecord def dump(obj) return if obj.nil? - assert_valid_value(obj) + assert_valid_value(obj, action: "dump") YAML.dump obj end @@ -23,16 +23,16 @@ module ActiveRecord return yaml unless yaml.is_a?(String) && /^---/.match?(yaml) obj = YAML.load(yaml) - assert_valid_value(obj) + assert_valid_value(obj, action: "load") obj ||= object_class.new if object_class != Object obj end - def assert_valid_value(obj) + def assert_valid_value(obj, action:) unless obj.nil? || obj.is_a?(object_class) raise SerializationTypeMismatch, - "Attribute `#{@attr_name}` was supposed to be a #{object_class}, but was a #{obj.class}. -- #{obj.inspect}" + "can't #{action} `#{@attr_name}`: was supposed to be a #{object_class}, but was a #{obj.class}. -- #{obj.inspect}" end end diff --git a/activerecord/lib/active_record/type/serialized.rb b/activerecord/lib/active_record/type/serialized.rb index ac9134bfcb..6af05c1860 100644 --- a/activerecord/lib/active_record/type/serialized.rb +++ b/activerecord/lib/active_record/type/serialized.rb @@ -43,7 +43,7 @@ module ActiveRecord def assert_valid_value(value) if coder.respond_to?(:assert_valid_value) - coder.assert_valid_value(value) + coder.assert_valid_value(value, action: "serialize") end end diff --git a/activerecord/test/cases/coders/yaml_column_test.rb b/activerecord/test/cases/coders/yaml_column_test.rb index 1e0b4580af..59ef389326 100644 --- a/activerecord/test/cases/coders/yaml_column_test.rb +++ b/activerecord/test/cases/coders/yaml_column_test.rb @@ -10,19 +10,19 @@ module ActiveRecord end def test_type_mismatch_on_different_classes_on_dump - coder = YAMLColumn.new("attr_name", Array) + coder = YAMLColumn.new("tags", Array) error = assert_raises(SerializationTypeMismatch) do coder.dump("a") end - assert_equal %{Attribute `attr_name` was supposed to be a Array, but was a String. -- "a"}, error.to_s + assert_equal %{can't dump `tags`: was supposed to be a Array, but was a String. -- "a"}, error.to_s end def test_type_mismatch_on_different_classes - coder = YAMLColumn.new("attr_name", Array) + coder = YAMLColumn.new("tags", Array) error = assert_raises(SerializationTypeMismatch) do coder.load "--- foo" end - assert_equal %{Attribute `attr_name` was supposed to be a Array, but was a String. -- "foo"}, error.to_s + assert_equal %{can't load `tags`: was supposed to be a Array, but was a String. -- "foo"}, error.to_s end def test_nil_is_ok diff --git a/activerecord/test/cases/serialized_attribute_test.rb b/activerecord/test/cases/serialized_attribute_test.rb index 24b83691f6..673392b4c4 100644 --- a/activerecord/test/cases/serialized_attribute_test.rb +++ b/activerecord/test/cases/serialized_attribute_test.rb @@ -250,7 +250,7 @@ class SerializedAttributeTest < ActiveRecord::TestCase error = assert_raise(ActiveRecord::SerializationTypeMismatch) do topic.content end - expected = "Attribute `content` was supposed to be a Array, but was a Hash. -- {:zomg=>true}" + expected = "can't load `content`: was supposed to be a Array, but was a Hash. -- {:zomg=>true}" assert_equal expected, error.to_s end |