Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge pull request #28270 from mmangino/dont_ignore_seralization_options
Don't accidentally lose includes in serialization
  • Loading branch information
kamipo committed Feb 28, 2018
1 parent 03341b2 commit 853054b
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 1 deletion.
5 changes: 5 additions & 0 deletions activemodel/CHANGELOG.md
@@ -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.
Expand Down
2 changes: 1 addition & 1 deletion activemodel/lib/active_model/serialization.rb
Expand Up @@ -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|
Expand Down
7 changes: 7 additions & 0 deletions activemodel/test/cases/serialization_test.rb
Expand Up @@ -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.