Merge pull request #639 from gauthier-delacroix/Unsuffixed-default-associations-keys

Allow JSONAPI unsuffixed associations keys
This commit is contained in:
Steve Klabnik
2014-10-08 14:45:46 -04:00
6 changed files with 79 additions and 2 deletions

22
test/fixtures/poro.rb vendored
View File

@@ -148,3 +148,25 @@ module TestNamespace
class ProfileSerializer < ::ProfileSerializer; end
class UserSerializer < ::UserSerializer; end
end
ActiveModel::Serializer.setup do |config|
config.default_key_type = :name
end
class NameKeyUserSerializer < ActiveModel::Serializer
attributes :name, :email
has_one :profile
end
class NameKeyPostSerializer < ActiveModel::Serializer
attributes :title, :body
has_many :comments
end
ActiveModel::Serializer.setup do |config|
config.default_key_type = nil
end

View File

@@ -242,6 +242,24 @@ module ActiveModel
}
}, @post_serializer.as_json)
end
def test_associations_name_key_embedding_ids_serialization_using_serializable_hash
@association = NameKeyPostSerializer._associations[:comments]
@association.embed = :ids
assert_equal({
title: 'Title 1', body: 'Body 1', 'comments' => @post.comments.map { |c| c.object_id }
}, NameKeyPostSerializer.new(@post).serializable_hash)
end
def test_associations_name_key_embedding_ids_serialization_using_as_json
@association = NameKeyPostSerializer._associations[:comments]
@association.embed = :ids
assert_equal({
'name_key_post' => { title: 'Title 1', body: 'Body 1', 'comments' => @post.comments.map { |c| c.object_id } }
}, NameKeyPostSerializer.new(@post).as_json)
end
end
end
end

View File

@@ -216,6 +216,24 @@ module ActiveModel
}
}, @user_serializer.as_json)
end
def test_associations_name_key_embedding_ids_serialization_using_serializable_hash
@association = NameKeyUserSerializer._associations[:profile]
@association.embed = :ids
assert_equal({
name: 'Name 1', email: 'mail@server.com', 'profile' => @user.profile.object_id
}, NameKeyUserSerializer.new(@user).serializable_hash)
end
def test_associations_name_key_embedding_ids_serialization_using_as_json
@association = NameKeyUserSerializer._associations[:profile]
@association.embed = :ids
assert_equal({
'name_key_user' => { name: 'Name 1', email: 'mail@server.com', 'profile' => @user.profile.object_id }
}, NameKeyUserSerializer.new(@user).as_json)
end
end
end
end