From c847e17300f4cacfcabf79bea746700697ce57ff Mon Sep 17 00:00:00 2001 From: Ryuta Kamizono Date: Sun, 13 Aug 2017 22:09:28 +0900 Subject: Allow `serialize` with a custom coder on `json` and `array` columns We already have a test case for `serialize` with a custom coder in `PostgresqlHstoreTest`. https://github.com/rails/rails/blob/v5.1.3/activerecord/test/cases/adapters/postgresql/hstore_test.rb#L316-L335 --- activerecord/test/cases/json_shared_test_cases.rb | 26 ++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) (limited to 'activerecord/test/cases/json_shared_test_cases.rb') diff --git a/activerecord/test/cases/json_shared_test_cases.rb b/activerecord/test/cases/json_shared_test_cases.rb index 952194c6dc..56ec8c8a82 100644 --- a/activerecord/test/cases/json_shared_test_cases.rb +++ b/activerecord/test/cases/json_shared_test_cases.rb @@ -216,7 +216,7 @@ module JSONSharedTestCases assert_equal true, json.payload end - def test_not_compatible_with_serialize_macro + def test_not_compatible_with_serialize_json new_klass = Class.new(klass) do serialize :payload, JSON end @@ -225,6 +225,30 @@ module JSONSharedTestCases end end + class MySettings + def initialize(hash); @hash = hash end + def to_hash; @hash end + def self.load(hash); new(hash) end + def self.dump(object); object.to_hash end + end + + def test_json_with_serialized_attributes + new_klass = Class.new(klass) do + serialize :settings, MySettings + end + + new_klass.create!(settings: MySettings.new("one" => "two")) + record = new_klass.first + + assert_instance_of MySettings, record.settings + assert_equal({ "one" => "two" }, record.settings.to_hash) + + record.settings = MySettings.new("three" => "four") + record.save! + + assert_equal({ "three" => "four" }, record.reload.settings.to_hash) + end + private def klass JsonDataType -- cgit v1.2.3