Skip to content

Commit ae3cf18

Browse files
author
Yohan Robert
committed
Merge pull request #1607 from Hirtenknogger/merge-multiple-nested-associations
Merge multiple nested associations
2 parents a105c60 + a30f53d commit ae3cf18

File tree

7 files changed

+146
-25
lines changed

7 files changed

+146
-25
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,4 @@ test/version_tmp
1717
tmp
1818
*.swp
1919
.ruby-version
20+
vendor/bundle

.travis.yml

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,18 @@ rvm:
1111

1212
sudo: false
1313

14-
install:
15-
- bundle install --retry=3
14+
install: bundle install --path=vendor/bundle --retry=3 --jobs=3
15+
cache:
16+
directories:
17+
- vendor/bundle
1618

1719
env:
18-
- "RAILS_VERSION=3.2.17"
19-
- "RAILS_VERSION=4.0.3"
20-
- "RAILS_VERSION=4.1.0"
20+
- "RAILS_VERSION=4.0"
21+
- "RAILS_VERSION=4.1"
22+
- "RAILS_VERSION=4.2"
2123
- "RAILS_VERSION=master"
2224

2325
matrix:
2426
allow_failures:
2527
- rvm: ruby-head
2628
- env: "RAILS_VERSION=master"
27-

Gemfile

Lines changed: 39 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,47 @@ source 'https://rubygems.org'
22

33
gemspec
44

5-
platforms :ruby do
6-
# sqlite3 1.3.9 does not work with rubinius 2.2.5:
7-
# https://github.com/sparklemotion/sqlite3-ruby/issues/122
8-
gem 'sqlite3', '1.3.8'
5+
version = ENV["RAILS_VERSION"] || "4.2"
6+
7+
if version == 'master'
8+
gem 'rack', github: 'rack/rack'
9+
git 'https://github.com/rails/rails.git' do
10+
gem 'railties'
11+
gem 'activesupport'
12+
gem 'activemodel'
13+
gem 'actionpack'
14+
# Rails 5
15+
gem 'actionview'
16+
end
17+
# Rails 5
18+
gem 'rails-controller-testing', github: 'rails/rails-controller-testing'
19+
else
20+
gem_version = "~> #{version}.0"
21+
gem 'railties', gem_version
22+
gem 'activesupport', gem_version
23+
gem 'activemodel', gem_version
24+
gem 'actionpack', gem_version
25+
end
26+
27+
if RUBY_VERSION < '2'
28+
gem 'mime-types', [ '>= 2.6.2', '< 3' ]
929
end
1030

11-
platforms :jruby do
12-
gem 'activerecord-jdbcsqlite3-adapter'
31+
# https://github.com/bundler/bundler/blob/89a8778c19269561926cea172acdcda241d26d23/lib/bundler/dependency.rb#L30-L54
32+
@windows_platforms = [:mswin, :mingw, :x64_mingw]
33+
34+
# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
35+
gem 'tzinfo-data', platforms: (@windows_platforms + [:jruby])
36+
37+
group :test do
38+
gem 'activerecord'
39+
gem 'sqlite3', platform: (@windows_platforms + [:ruby])
40+
gem 'activerecord-jdbcsqlite3-adapter', platform: :jruby
41+
42+
gem 'codeclimate-test-reporter', require: false
43+
gem 'simplecov', '~> 0.10', require: false, group: :development
1344
end
1445

15-
version = ENV["RAILS_VERSION"] || "4.0.2"
16-
rails = case version
17-
when "master"
18-
{:github => "rails/rails"}
19-
else
20-
"~> #{version}"
21-
end
22-
gem "rails", rails
23-
24-
if version < "4"
25-
gem "minitest", "~> 4.7.5"
46+
group :development, :test do
47+
gem 'rubocop', '~> 0.34.0', require: false
2648
end

appveyor.yml

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
version: '{build}'
2+
3+
skip_tags: true
4+
5+
environment:
6+
matrix:
7+
- ruby_version: "200"
8+
- ruby_version: "200-x64"
9+
- ruby_version: "21"
10+
- ruby_version: "21-x64"
11+
12+
cache:
13+
- vendor/bundle
14+
15+
install:
16+
- SET PATH=C:\Ruby%ruby_version%\bin;%PATH%
17+
- ruby --version
18+
- gem --version
19+
- gem install bundler
20+
- bundler --version
21+
- bundle platform
22+
- bundle install --path=vendor/bundle --retry=3 --jobs=3
23+
24+
test_script:
25+
- bundle exec rake test
26+
27+
build: off

lib/active_model/serializer.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,11 @@ def embedded_in_root_associations
205205
# we must do this always because even if the current association is not
206206
# embeded in root, it might have its own associations that are embeded in root
207207
hash.merge!(association_serializer.embedded_in_root_associations) do |key, oldval, newval|
208-
oldval.merge(newval) { |_, oldval, newval| [oldval, newval].flatten.uniq }
208+
if oldval.respond_to?(:to_ary)
209+
[oldval, newval].flatten.uniq
210+
else
211+
oldval.merge(newval) { |_, oldval, newval| [oldval, newval].flatten.uniq }
212+
end
209213
end
210214

211215
if association.embed_in_root?

test/fixtures/poro.rb

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,26 @@ def special_comment
5353
end
5454
end
5555

56+
class Type < Model
57+
end
58+
59+
class SelfReferencingUser < Model
60+
def type
61+
@type ||= Type.new(name: 'N1')
62+
end
63+
def parent
64+
@parent ||= SelfReferencingUserParent.new(name: 'N1')
65+
end
66+
end
67+
68+
class SelfReferencingUserParent < Model
69+
def type
70+
@type ||= Type.new(name: 'N2')
71+
end
72+
def parent
73+
end
74+
end
75+
5676
class Comment < Model
5777
end
5878

@@ -87,6 +107,22 @@ class UserSerializer < ActiveModel::Serializer
87107
has_one :profile
88108
end
89109

110+
class TypeSerializer < ActiveModel::Serializer
111+
attributes :name
112+
end
113+
114+
class SelfReferencingUserParentSerializer < ActiveModel::Serializer
115+
attributes :name
116+
has_one :type, serializer: TypeSerializer, embed: :ids, include: true
117+
end
118+
119+
class SelfReferencingUserSerializer < ActiveModel::Serializer
120+
attributes :name
121+
122+
has_one :type, serializer: TypeSerializer, embed: :ids, include: true
123+
has_one :parent, serializer: SelfReferencingUserSerializer, embed: :ids, include: true
124+
end
125+
90126
class UserInfoSerializer < ActiveModel::Serializer
91127
has_one :user, serializer: UserSerializer
92128
end
@@ -176,7 +212,7 @@ class NameKeyUserSerializer < ActiveModel::Serializer
176212

177213
class NameKeyPostSerializer < ActiveModel::Serializer
178214
attributes :title, :body
179-
215+
180216
has_many :comments
181217
end
182218

test/unit/active_model/serializer/associations_test.rb

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,36 @@ def test_associations_inheritance
1414
assert_equal([:comments],
1515
another_inherited_serializer_klass._associations.keys)
1616
end
17+
def test_multiple_nested_associations
18+
parent = SelfReferencingUserParent.new(name: "The Parent")
19+
child = SelfReferencingUser.new(name: "The child", parent: parent)
20+
self_referencing_user_serializer = SelfReferencingUserSerializer.new(child)
21+
result = self_referencing_user_serializer.as_json
22+
expected_result = {
23+
"self_referencing_user"=>{
24+
:name=>"The child",
25+
"type_id"=>child.type.object_id,
26+
"parent_id"=>child.parent.object_id
27+
28+
},
29+
"types"=>[
30+
{
31+
:name=>"N1",
32+
},
33+
{
34+
:name=>"N2",
35+
}
36+
],
37+
"parents"=>[
38+
{
39+
:name=>"N1",
40+
"type_id"=>child.parent.type.object_id,
41+
"parent_id"=>nil
42+
}
43+
]
44+
}
45+
assert_equal(expected_result, result)
46+
end
1747
end
1848
end
1949
end

0 commit comments

Comments
 (0)