mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-22 22:06:50 +00:00
Merge pull request #1607 from Hirtenknogger/merge-multiple-nested-associations
Merge multiple nested associations
This commit is contained in:
commit
ae3cf18d0f
1
.gitignore
vendored
1
.gitignore
vendored
@ -17,3 +17,4 @@ test/version_tmp
|
|||||||
tmp
|
tmp
|
||||||
*.swp
|
*.swp
|
||||||
.ruby-version
|
.ruby-version
|
||||||
|
vendor/bundle
|
||||||
|
|||||||
13
.travis.yml
13
.travis.yml
@ -11,17 +11,18 @@ rvm:
|
|||||||
|
|
||||||
sudo: false
|
sudo: false
|
||||||
|
|
||||||
install:
|
install: bundle install --path=vendor/bundle --retry=3 --jobs=3
|
||||||
- bundle install --retry=3
|
cache:
|
||||||
|
directories:
|
||||||
|
- vendor/bundle
|
||||||
|
|
||||||
env:
|
env:
|
||||||
- "RAILS_VERSION=3.2.17"
|
- "RAILS_VERSION=4.0"
|
||||||
- "RAILS_VERSION=4.0.3"
|
- "RAILS_VERSION=4.1"
|
||||||
- "RAILS_VERSION=4.1.0"
|
- "RAILS_VERSION=4.2"
|
||||||
- "RAILS_VERSION=master"
|
- "RAILS_VERSION=master"
|
||||||
|
|
||||||
matrix:
|
matrix:
|
||||||
allow_failures:
|
allow_failures:
|
||||||
- rvm: ruby-head
|
- rvm: ruby-head
|
||||||
- env: "RAILS_VERSION=master"
|
- env: "RAILS_VERSION=master"
|
||||||
|
|
||||||
|
|||||||
54
Gemfile
54
Gemfile
@ -2,25 +2,47 @@ source 'https://rubygems.org'
|
|||||||
|
|
||||||
gemspec
|
gemspec
|
||||||
|
|
||||||
platforms :ruby do
|
version = ENV["RAILS_VERSION"] || "4.2"
|
||||||
# sqlite3 1.3.9 does not work with rubinius 2.2.5:
|
|
||||||
# https://github.com/sparklemotion/sqlite3-ruby/issues/122
|
if version == 'master'
|
||||||
gem 'sqlite3', '1.3.8'
|
gem 'rack', github: 'rack/rack'
|
||||||
|
git 'https://github.com/rails/rails.git' do
|
||||||
|
gem 'railties'
|
||||||
|
gem 'activesupport'
|
||||||
|
gem 'activemodel'
|
||||||
|
gem 'actionpack'
|
||||||
|
# Rails 5
|
||||||
|
gem 'actionview'
|
||||||
|
end
|
||||||
|
# Rails 5
|
||||||
|
gem 'rails-controller-testing', github: 'rails/rails-controller-testing'
|
||||||
|
else
|
||||||
|
gem_version = "~> #{version}.0"
|
||||||
|
gem 'railties', gem_version
|
||||||
|
gem 'activesupport', gem_version
|
||||||
|
gem 'activemodel', gem_version
|
||||||
|
gem 'actionpack', gem_version
|
||||||
end
|
end
|
||||||
|
|
||||||
platforms :jruby do
|
if RUBY_VERSION < '2'
|
||||||
gem 'activerecord-jdbcsqlite3-adapter'
|
gem 'mime-types', [ '>= 2.6.2', '< 3' ]
|
||||||
end
|
end
|
||||||
|
|
||||||
version = ENV["RAILS_VERSION"] || "4.0.2"
|
# https://github.com/bundler/bundler/blob/89a8778c19269561926cea172acdcda241d26d23/lib/bundler/dependency.rb#L30-L54
|
||||||
rails = case version
|
@windows_platforms = [:mswin, :mingw, :x64_mingw]
|
||||||
when "master"
|
|
||||||
{:github => "rails/rails"}
|
|
||||||
else
|
|
||||||
"~> #{version}"
|
|
||||||
end
|
|
||||||
gem "rails", rails
|
|
||||||
|
|
||||||
if version < "4"
|
# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
|
||||||
gem "minitest", "~> 4.7.5"
|
gem 'tzinfo-data', platforms: (@windows_platforms + [:jruby])
|
||||||
|
|
||||||
|
group :test do
|
||||||
|
gem 'activerecord'
|
||||||
|
gem 'sqlite3', platform: (@windows_platforms + [:ruby])
|
||||||
|
gem 'activerecord-jdbcsqlite3-adapter', platform: :jruby
|
||||||
|
|
||||||
|
gem 'codeclimate-test-reporter', require: false
|
||||||
|
gem 'simplecov', '~> 0.10', require: false, group: :development
|
||||||
|
end
|
||||||
|
|
||||||
|
group :development, :test do
|
||||||
|
gem 'rubocop', '~> 0.34.0', require: false
|
||||||
end
|
end
|
||||||
|
|||||||
27
appveyor.yml
Normal file
27
appveyor.yml
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
version: '{build}'
|
||||||
|
|
||||||
|
skip_tags: true
|
||||||
|
|
||||||
|
environment:
|
||||||
|
matrix:
|
||||||
|
- ruby_version: "200"
|
||||||
|
- ruby_version: "200-x64"
|
||||||
|
- ruby_version: "21"
|
||||||
|
- ruby_version: "21-x64"
|
||||||
|
|
||||||
|
cache:
|
||||||
|
- vendor/bundle
|
||||||
|
|
||||||
|
install:
|
||||||
|
- SET PATH=C:\Ruby%ruby_version%\bin;%PATH%
|
||||||
|
- ruby --version
|
||||||
|
- gem --version
|
||||||
|
- gem install bundler
|
||||||
|
- bundler --version
|
||||||
|
- bundle platform
|
||||||
|
- bundle install --path=vendor/bundle --retry=3 --jobs=3
|
||||||
|
|
||||||
|
test_script:
|
||||||
|
- bundle exec rake test
|
||||||
|
|
||||||
|
build: off
|
||||||
@ -205,7 +205,11 @@ end
|
|||||||
# we must do this always because even if the current association is not
|
# we must do this always because even if the current association is not
|
||||||
# embeded in root, it might have its own associations that are embeded in root
|
# embeded in root, it might have its own associations that are embeded in root
|
||||||
hash.merge!(association_serializer.embedded_in_root_associations) do |key, oldval, newval|
|
hash.merge!(association_serializer.embedded_in_root_associations) do |key, oldval, newval|
|
||||||
oldval.merge(newval) { |_, oldval, newval| [oldval, newval].flatten.uniq }
|
if oldval.respond_to?(:to_ary)
|
||||||
|
[oldval, newval].flatten.uniq
|
||||||
|
else
|
||||||
|
oldval.merge(newval) { |_, oldval, newval| [oldval, newval].flatten.uniq }
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
if association.embed_in_root?
|
if association.embed_in_root?
|
||||||
|
|||||||
38
test/fixtures/poro.rb
vendored
38
test/fixtures/poro.rb
vendored
@ -53,6 +53,26 @@ class SpecialPost < Post
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
class Type < Model
|
||||||
|
end
|
||||||
|
|
||||||
|
class SelfReferencingUser < Model
|
||||||
|
def type
|
||||||
|
@type ||= Type.new(name: 'N1')
|
||||||
|
end
|
||||||
|
def parent
|
||||||
|
@parent ||= SelfReferencingUserParent.new(name: 'N1')
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
class SelfReferencingUserParent < Model
|
||||||
|
def type
|
||||||
|
@type ||= Type.new(name: 'N2')
|
||||||
|
end
|
||||||
|
def parent
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
class Comment < Model
|
class Comment < Model
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -87,6 +107,22 @@ class UserSerializer < ActiveModel::Serializer
|
|||||||
has_one :profile
|
has_one :profile
|
||||||
end
|
end
|
||||||
|
|
||||||
|
class TypeSerializer < ActiveModel::Serializer
|
||||||
|
attributes :name
|
||||||
|
end
|
||||||
|
|
||||||
|
class SelfReferencingUserParentSerializer < ActiveModel::Serializer
|
||||||
|
attributes :name
|
||||||
|
has_one :type, serializer: TypeSerializer, embed: :ids, include: true
|
||||||
|
end
|
||||||
|
|
||||||
|
class SelfReferencingUserSerializer < ActiveModel::Serializer
|
||||||
|
attributes :name
|
||||||
|
|
||||||
|
has_one :type, serializer: TypeSerializer, embed: :ids, include: true
|
||||||
|
has_one :parent, serializer: SelfReferencingUserSerializer, embed: :ids, include: true
|
||||||
|
end
|
||||||
|
|
||||||
class UserInfoSerializer < ActiveModel::Serializer
|
class UserInfoSerializer < ActiveModel::Serializer
|
||||||
has_one :user, serializer: UserSerializer
|
has_one :user, serializer: UserSerializer
|
||||||
end
|
end
|
||||||
@ -176,7 +212,7 @@ end
|
|||||||
|
|
||||||
class NameKeyPostSerializer < ActiveModel::Serializer
|
class NameKeyPostSerializer < ActiveModel::Serializer
|
||||||
attributes :title, :body
|
attributes :title, :body
|
||||||
|
|
||||||
has_many :comments
|
has_many :comments
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@ -14,6 +14,36 @@ module ActiveModel
|
|||||||
assert_equal([:comments],
|
assert_equal([:comments],
|
||||||
another_inherited_serializer_klass._associations.keys)
|
another_inherited_serializer_klass._associations.keys)
|
||||||
end
|
end
|
||||||
|
def test_multiple_nested_associations
|
||||||
|
parent = SelfReferencingUserParent.new(name: "The Parent")
|
||||||
|
child = SelfReferencingUser.new(name: "The child", parent: parent)
|
||||||
|
self_referencing_user_serializer = SelfReferencingUserSerializer.new(child)
|
||||||
|
result = self_referencing_user_serializer.as_json
|
||||||
|
expected_result = {
|
||||||
|
"self_referencing_user"=>{
|
||||||
|
:name=>"The child",
|
||||||
|
"type_id"=>child.type.object_id,
|
||||||
|
"parent_id"=>child.parent.object_id
|
||||||
|
|
||||||
|
},
|
||||||
|
"types"=>[
|
||||||
|
{
|
||||||
|
:name=>"N1",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
:name=>"N2",
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"parents"=>[
|
||||||
|
{
|
||||||
|
:name=>"N1",
|
||||||
|
"type_id"=>child.parent.type.object_id,
|
||||||
|
"parent_id"=>nil
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
assert_equal(expected_result, result)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user