mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Don't accidentally lose includes in serialization
This commit is contained in:
parent
76be78c59c
commit
1bb0605dfe
3 changed files with 13 additions and 4 deletions
|
@ -1,3 +1,6 @@
|
|||
* Don't only take the first hash key in serialization :includes. Doing so silently loses options in `includes: [:account, {address: {only: id}, user: {only: id}]`
|
||||
|
||||
*Mike Mangino*
|
||||
## Rails 5.1.0.beta1 (February 23, 2017) ##
|
||||
|
||||
* Remove deprecated behavior that halts callbacks when the return is false.
|
||||
|
|
|
@ -177,7 +177,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|
|
||||
|
|
|
@ -144,6 +144,12 @@ class SerializationTest < ActiveModel::TestCase
|
|||
assert_equal expected, @user.serializable_hash(include: { address: { only: "street" } })
|
||||
end
|
||||
|
||||
def test_multiple_includes_with_options
|
||||
expected = { "email" => "david@example.com", "gender" => "male", "name" => "David",
|
||||
"address" => { "street" => "123 Lane" } }
|
||||
assert_equal expected, @user.serializable_hash(include: { address: { only: "street" } })
|
||||
end
|
||||
|
||||
def test_nested_include
|
||||
@user.friends.first.friends = [@user]
|
||||
expected = { "email" => "david@example.com", "gender" => "male", "name" => "David",
|
||||
|
@ -168,8 +174,8 @@ class SerializationTest < ActiveModel::TestCase
|
|||
def test_multiple_includes_with_options
|
||||
expected = { "email" => "david@example.com", "gender" => "male", "name" => "David",
|
||||
"address" => { "street" => "123 Lane" },
|
||||
"friends" => [{ "name" => "Joe", "email" => "joe@example.com", "gender" => "male" },
|
||||
{ "name" => "Sue", "email" => "sue@example.com", "gender" => "female" }] }
|
||||
assert_equal expected, @user.serializable_hash(include: [{ address: { only: "street" } }, :friends])
|
||||
"friends" => [{ "name" => "Joe" },
|
||||
{ "name" => "Sue" }] }
|
||||
assert_equal expected, @user.serializable_hash(include: [ address: { only: "street" } , friends: {only: "name"}])
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue