diff options
author | Rafael França <rafaelmfranca@gmail.com> | 2017-08-14 17:40:13 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-08-14 17:40:13 -0400 |
commit | efad56a0d93c51d7ebce5007a80df522335430bd (patch) | |
tree | df314458c113bad4ca2e0a8ae3a9caffd36324c0 /activerecord/test/cases/json_shared_test_cases.rb | |
parent | 509dfdca98cf82244c33deae66baf7175279400c (diff) | |
parent | c847e17300f4cacfcabf79bea746700697ce57ff (diff) | |
download | rails-efad56a0d93c51d7ebce5007a80df522335430bd.tar.gz rails-efad56a0d93c51d7ebce5007a80df522335430bd.tar.bz2 rails-efad56a0d93c51d7ebce5007a80df522335430bd.zip |
Merge pull request #30229 from kamipo/allow_serialize_with_custom_coder
Allow `serialize` with a custom coder on `json` and `array` columns
Diffstat (limited to 'activerecord/test/cases/json_shared_test_cases.rb')
-rw-r--r-- | activerecord/test/cases/json_shared_test_cases.rb | 26 |
1 files changed, 25 insertions, 1 deletions
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 |