Permalink
Browse files

Merge pull request #28270 from mmangino/dont_ignore_seralization_options

Don't accidentally lose includes in serialization
  • Loading branch information...
kamipo committed Feb 27, 2018
1 parent 03341b2 commit 853054bcc7a043eea78c97e7705a46abb603cc44
@@ -1,3 +1,8 @@
* Do not lose all multiple `:includes` with options in serialization.
*Mike Mangino*
## Rails 5.2.0.rc1 (January 30, 2018) ##
* Models using the attributes API with a proc default can now be marshalled.
@@ -179,7 +179,7 @@ def serializable_add_includes(options = {}) #:nodoc:
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|
@@ -174,4 +174,11 @@ def test_multiple_includes_with_options
{ "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

0 comments on commit 853054b

Please sign in to comment.