diff options
author | Ryuta Kamizono <kamipo@gmail.com> | 2018-02-27 23:42:15 +0900 |
---|---|---|
committer | Ryuta Kamizono <kamipo@gmail.com> | 2018-02-27 23:42:15 +0900 |
commit | 0605f45ab323331b06dde3ed16838f56f141ca3f (patch) | |
tree | 2ad75bb126c177c4e1ddb612aff56a0562b9e434 | |
parent | f1878fa06efb2eaa2c521022fa79f95b6c49f865 (diff) | |
parent | 53789f55cd48626e289f130aadd57618d8a73c8a (diff) | |
download | rails-0605f45ab323331b06dde3ed16838f56f141ca3f.tar.gz rails-0605f45ab323331b06dde3ed16838f56f141ca3f.tar.bz2 rails-0605f45ab323331b06dde3ed16838f56f141ca3f.zip |
Merge pull request #28270 from mmangino/dont_ignore_seralization_options
Don't accidentally lose includes in serialization
-rw-r--r-- | activemodel/CHANGELOG.md | 4 | ||||
-rw-r--r-- | activemodel/lib/active_model/serialization.rb | 2 | ||||
-rw-r--r-- | activemodel/test/cases/serialization_test.rb | 7 |
3 files changed, 12 insertions, 1 deletions
diff --git a/activemodel/CHANGELOG.md b/activemodel/CHANGELOG.md index b28c83e4ed..8a3578229a 100644 --- a/activemodel/CHANGELOG.md +++ b/activemodel/CHANGELOG.md @@ -1,5 +1,9 @@ ## Rails 6.0.0.alpha (Unreleased) ## +* Do not lose all multiple `:includes` with options in serialization. + + *Mike Mangino* + * Rails 6 requires Ruby 2.4.1 or newer. *Jeremy Daer* diff --git a/activemodel/lib/active_model/serialization.rb b/activemodel/lib/active_model/serialization.rb index 47cb81bee5..c4b7b32291 100644 --- a/activemodel/lib/active_model/serialization.rb +++ b/activemodel/lib/active_model/serialization.rb @@ -179,7 +179,7 @@ module ActiveModel return unless includes = options[:include] unless includes.is_a?(Hash) - includes = Hash[Array(includes).map { |n| n.is_a?(Hash) ? n.to_a.first : [n, {}] }] + includes = Hash[Array(includes).flat_map { |n| n.is_a?(Hash) ? n.to_a : [[n, {}]] }] end includes.each do |association, opts| diff --git a/activemodel/test/cases/serialization_test.rb b/activemodel/test/cases/serialization_test.rb index 9002982e7f..6826d2bbd1 100644 --- a/activemodel/test/cases/serialization_test.rb +++ b/activemodel/test/cases/serialization_test.rb @@ -174,4 +174,11 @@ class SerializationTest < ActiveModel::TestCase { "name" => "Sue", "email" => "sue@example.com", "gender" => "female" }] } assert_equal expected, @user.serializable_hash(include: [{ address: { only: "street" } }, :friends]) end + + def test_all_includes_with_options + expected = { "email" => "david@example.com", "gender" => "male", "name" => "David", + "address" => { "street" => "123 Lane" }, + "friends" => [{ "name" => "Joe" }, { "name" => "Sue" }] } + assert_equal expected, @user.serializable_hash(include: [address: { only: "street" }, friends: { only: "name" }]) + end end |