mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-23 06:16:50 +00:00
Append an _id or _ids suffix to associations' keys. Embed objects in root according to their serializers' names.
An `_id` suffix will be appended to the name of HasOne associations, while `_ids` will be appended to the singularized name of HasMany associations. Association keys can still be overridden with the `key` option. Furthermore, objects embedded in the root are now by default named according to their serializer, instead of the key used for their associations.
This commit is contained in:
parent
a74fc6c8db
commit
3b1d2faf51
10
README.md
10
README.md
@ -386,7 +386,7 @@ Now, any associations will be supplied as an Array of IDs:
|
|||||||
"id": 1,
|
"id": 1,
|
||||||
"title": "New post",
|
"title": "New post",
|
||||||
"body": "A body!",
|
"body": "A body!",
|
||||||
"comments": [ 1, 2, 3 ]
|
"comment_ids": [ 1, 2, 3 ]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
@ -413,7 +413,7 @@ The JSON will look like this:
|
|||||||
"comments": [
|
"comments": [
|
||||||
{ "id": 1, "body": "what a dumb post" }
|
{ "id": 1, "body": "what a dumb post" }
|
||||||
],
|
],
|
||||||
"tags": [ 1, 2, 3 ]
|
"tag_ids": [ 1, 2, 3 ]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
@ -444,11 +444,11 @@ this:
|
|||||||
"id": 1,
|
"id": 1,
|
||||||
"title": "New post",
|
"title": "New post",
|
||||||
"body": "A body!",
|
"body": "A body!",
|
||||||
"comments": [ 1, 2 ]
|
"comment_ids": [ 1, 2 ]
|
||||||
},
|
},
|
||||||
"comments": [
|
"comments": [
|
||||||
{ "id": 1, "body": "what a dumb post", "tags": [ 1, 2 ] },
|
{ "id": 1, "body": "what a dumb post", "tag_ids": [ 1, 2 ] },
|
||||||
{ "id": 2, "body": "i liked it", "tags": [ 1, 3 ] },
|
{ "id": 2, "body": "i liked it", "tag_ids": [ 1, 3 ] },
|
||||||
],
|
],
|
||||||
"tags": [
|
"tags": [
|
||||||
{ "id": 1, "name": "short" },
|
{ "id": 1, "name": "short" },
|
||||||
|
|||||||
@ -51,7 +51,7 @@ module ActiveModel
|
|||||||
end
|
end
|
||||||
|
|
||||||
def root
|
def root
|
||||||
option(:root) || plural_key
|
option(:root) || @name
|
||||||
end
|
end
|
||||||
|
|
||||||
def name
|
def name
|
||||||
@ -92,7 +92,15 @@ module ActiveModel
|
|||||||
end
|
end
|
||||||
|
|
||||||
class HasMany < Config #:nodoc:
|
class HasMany < Config #:nodoc:
|
||||||
alias plural_key key
|
def key
|
||||||
|
if key = option(:key)
|
||||||
|
key
|
||||||
|
elsif embed_ids?
|
||||||
|
"#{@name.to_s.singularize}_ids".to_sym
|
||||||
|
else
|
||||||
|
@name
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
def serialize
|
def serialize
|
||||||
associated_object.map do |item|
|
associated_object.map do |item|
|
||||||
@ -132,18 +140,30 @@ module ActiveModel
|
|||||||
option :polymorphic
|
option :polymorphic
|
||||||
end
|
end
|
||||||
|
|
||||||
def polymorphic_key
|
def root
|
||||||
associated_object.class.to_s.demodulize.underscore.to_sym
|
if root = option(:root)
|
||||||
end
|
root
|
||||||
|
elsif polymorphic?
|
||||||
def plural_key
|
|
||||||
if polymorphic?
|
|
||||||
associated_object.class.to_s.pluralize.demodulize.underscore.to_sym
|
associated_object.class.to_s.pluralize.demodulize.underscore.to_sym
|
||||||
else
|
else
|
||||||
key.to_s.pluralize.to_sym
|
@name.to_s.pluralize.to_sym
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def key
|
||||||
|
if key = option(:key)
|
||||||
|
key
|
||||||
|
elsif embed_ids? && !polymorphic?
|
||||||
|
"#{@name}_id".to_sym
|
||||||
|
else
|
||||||
|
@name
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def polymorphic_key
|
||||||
|
associated_object.class.to_s.demodulize.underscore.to_sym
|
||||||
|
end
|
||||||
|
|
||||||
def serialize
|
def serialize
|
||||||
object = associated_object
|
object = associated_object
|
||||||
|
|
||||||
|
|||||||
@ -70,7 +70,7 @@ class AssociationTest < ActiveModel::TestCase
|
|||||||
include! :comments, :value => @post.comments
|
include! :comments, :value => @post.comments
|
||||||
|
|
||||||
assert_equal({
|
assert_equal({
|
||||||
:comments => [ 1 ]
|
:comment_ids => [ 1 ]
|
||||||
}, @hash)
|
}, @hash)
|
||||||
|
|
||||||
assert_equal({
|
assert_equal({
|
||||||
@ -91,7 +91,7 @@ class AssociationTest < ActiveModel::TestCase
|
|||||||
include! :comments, :value => @post.comments, :embed => :ids, :include => false
|
include! :comments, :value => @post.comments, :embed => :ids, :include => false
|
||||||
|
|
||||||
assert_equal({
|
assert_equal({
|
||||||
:comments => [ 1 ]
|
:comment_ids => [ 1 ]
|
||||||
}, @hash)
|
}, @hash)
|
||||||
|
|
||||||
assert_equal({}, @root_hash)
|
assert_equal({}, @root_hash)
|
||||||
@ -101,7 +101,7 @@ class AssociationTest < ActiveModel::TestCase
|
|||||||
include! :comment, :value => @post.comment
|
include! :comment, :value => @post.comment
|
||||||
|
|
||||||
assert_equal({
|
assert_equal({
|
||||||
:comment => 1
|
:comment_id => 1
|
||||||
}, @hash)
|
}, @hash)
|
||||||
|
|
||||||
assert_equal({
|
assert_equal({
|
||||||
@ -119,7 +119,7 @@ class AssociationTest < ActiveModel::TestCase
|
|||||||
include! :comments
|
include! :comments
|
||||||
|
|
||||||
assert_equal({
|
assert_equal({
|
||||||
:comments => [ 1 ]
|
:comment_ids => [ 1 ]
|
||||||
}, @hash)
|
}, @hash)
|
||||||
|
|
||||||
assert_equal({
|
assert_equal({
|
||||||
@ -137,7 +137,7 @@ class AssociationTest < ActiveModel::TestCase
|
|||||||
include! :comment
|
include! :comment
|
||||||
|
|
||||||
assert_equal({
|
assert_equal({
|
||||||
:comment => 1
|
:comment_id => 1
|
||||||
}, @hash)
|
}, @hash)
|
||||||
|
|
||||||
assert_equal({
|
assert_equal({
|
||||||
@ -159,7 +159,7 @@ class AssociationTest < ActiveModel::TestCase
|
|||||||
}, @hash)
|
}, @hash)
|
||||||
|
|
||||||
assert_equal({
|
assert_equal({
|
||||||
:custom_comments => [
|
:comments => [
|
||||||
{ :id => 1, :body => "ZOMG A COMMENT" }
|
{ :id => 1, :body => "ZOMG A COMMENT" }
|
||||||
]
|
]
|
||||||
}, @root_hash)
|
}, @root_hash)
|
||||||
@ -167,17 +167,17 @@ class AssociationTest < ActiveModel::TestCase
|
|||||||
|
|
||||||
def test_with_default_has_one_with_custom_key
|
def test_with_default_has_one_with_custom_key
|
||||||
@post_serializer_class.class_eval do
|
@post_serializer_class.class_eval do
|
||||||
has_one :comment, :key => :custom_comment
|
has_one :comment, :key => :custom_comment_id
|
||||||
end
|
end
|
||||||
|
|
||||||
include! :comment
|
include! :comment
|
||||||
|
|
||||||
assert_equal({
|
assert_equal({
|
||||||
:custom_comment => 1
|
:custom_comment_id => 1
|
||||||
}, @hash)
|
}, @hash)
|
||||||
|
|
||||||
assert_equal({
|
assert_equal({
|
||||||
:custom_comments => [
|
:comments => [
|
||||||
{ :id => 1, :body => "ZOMG A COMMENT" }
|
{ :id => 1, :body => "ZOMG A COMMENT" }
|
||||||
]
|
]
|
||||||
}, @root_hash)
|
}, @root_hash)
|
||||||
@ -207,7 +207,7 @@ class AssociationTest < ActiveModel::TestCase
|
|||||||
include_bare! :comments
|
include_bare! :comments
|
||||||
|
|
||||||
assert_equal({
|
assert_equal({
|
||||||
:comments => [ 1 ]
|
:comment_ids => [ 1 ]
|
||||||
}, @hash)
|
}, @hash)
|
||||||
|
|
||||||
assert_equal({}, @root_hash)
|
assert_equal({}, @root_hash)
|
||||||
@ -232,7 +232,7 @@ class AssociationTest < ActiveModel::TestCase
|
|||||||
include_bare! :comments
|
include_bare! :comments
|
||||||
|
|
||||||
assert_equal({
|
assert_equal({
|
||||||
:comments => [ 1 ]
|
:comment_ids => [ 1 ]
|
||||||
}, @hash)
|
}, @hash)
|
||||||
|
|
||||||
assert_equal({
|
assert_equal({
|
||||||
@ -250,7 +250,7 @@ class AssociationTest < ActiveModel::TestCase
|
|||||||
include_bare! :comment
|
include_bare! :comment
|
||||||
|
|
||||||
assert_equal({
|
assert_equal({
|
||||||
:comment => 1
|
:comment_id => 1
|
||||||
}, @hash)
|
}, @hash)
|
||||||
|
|
||||||
assert_equal({}, @root_hash)
|
assert_equal({}, @root_hash)
|
||||||
@ -275,7 +275,7 @@ class AssociationTest < ActiveModel::TestCase
|
|||||||
include_bare! :comment
|
include_bare! :comment
|
||||||
|
|
||||||
assert_equal({
|
assert_equal({
|
||||||
:comment => 1
|
:comment_id => 1
|
||||||
}, @hash)
|
}, @hash)
|
||||||
|
|
||||||
assert_equal({
|
assert_equal({
|
||||||
@ -333,7 +333,7 @@ class AssociationTest < ActiveModel::TestCase
|
|||||||
:post => {
|
:post => {
|
||||||
:title => "New Post",
|
:title => "New Post",
|
||||||
:body => "Body",
|
:body => "Body",
|
||||||
:comments => [ 1 ]
|
:comment_ids => [ 1 ]
|
||||||
},
|
},
|
||||||
:comments => [
|
:comments => [
|
||||||
{ :id => 1, :body => "ZOMG A COMMENT" }
|
{ :id => 1, :body => "ZOMG A COMMENT" }
|
||||||
@ -352,7 +352,7 @@ class AssociationTest < ActiveModel::TestCase
|
|||||||
:post => {
|
:post => {
|
||||||
:title => "New Post",
|
:title => "New Post",
|
||||||
:body => "Body",
|
:body => "Body",
|
||||||
:comments => [ 1 ]
|
:comment_ids => [ 1 ]
|
||||||
}
|
}
|
||||||
}, json)
|
}, json)
|
||||||
end
|
end
|
||||||
@ -368,7 +368,7 @@ class AssociationTest < ActiveModel::TestCase
|
|||||||
:post => {
|
:post => {
|
||||||
:title => "New Post",
|
:title => "New Post",
|
||||||
:body => "Body",
|
:body => "Body",
|
||||||
:comments => [ 1 ]
|
:comment_ids => [ 1 ]
|
||||||
}
|
}
|
||||||
}, json)
|
}, json)
|
||||||
end
|
end
|
||||||
@ -384,7 +384,7 @@ class AssociationTest < ActiveModel::TestCase
|
|||||||
:post => {
|
:post => {
|
||||||
:title => "New Post",
|
:title => "New Post",
|
||||||
:body => "Body",
|
:body => "Body",
|
||||||
:comments => [ 1 ]
|
:comment_ids => [ 1 ]
|
||||||
},
|
},
|
||||||
:comments => [
|
:comments => [
|
||||||
{ :id => 1, :body => "ZOMG A COMMENT" }
|
{ :id => 1, :body => "ZOMG A COMMENT" }
|
||||||
|
|||||||
@ -281,8 +281,8 @@ class SerializerTest < ActiveModel::TestCase
|
|||||||
:post => {
|
:post => {
|
||||||
:title => "New Post",
|
:title => "New Post",
|
||||||
:body => "Body of new post",
|
:body => "Body of new post",
|
||||||
:comments => [1, 2],
|
:comment_ids => [1, 2],
|
||||||
:author => nil
|
:author_id => nil
|
||||||
}
|
}
|
||||||
}, serializer.as_json)
|
}, serializer.as_json)
|
||||||
end
|
end
|
||||||
@ -305,8 +305,8 @@ class SerializerTest < ActiveModel::TestCase
|
|||||||
:post => {
|
:post => {
|
||||||
:title => "New Post",
|
:title => "New Post",
|
||||||
:body => "Body of new post",
|
:body => "Body of new post",
|
||||||
:comments => [1, 2],
|
:comment_ids => [1, 2],
|
||||||
:author => nil
|
:author_id => nil
|
||||||
},
|
},
|
||||||
:comments => [
|
:comments => [
|
||||||
{ :title => "Comment1" },
|
{ :title => "Comment1" },
|
||||||
@ -323,8 +323,8 @@ class SerializerTest < ActiveModel::TestCase
|
|||||||
:post => {
|
:post => {
|
||||||
:title => "New Post",
|
:title => "New Post",
|
||||||
:body => "Body of new post",
|
:body => "Body of new post",
|
||||||
:comments => [1, 2],
|
:comment_ids => [1, 2],
|
||||||
:author => 1
|
:author_id => 1
|
||||||
},
|
},
|
||||||
:comments => [
|
:comments => [
|
||||||
{ :title => "Comment1" },
|
{ :title => "Comment1" },
|
||||||
@ -558,7 +558,7 @@ class SerializerTest < ActiveModel::TestCase
|
|||||||
:post => {
|
:post => {
|
||||||
:title => "New Post",
|
:title => "New Post",
|
||||||
:body => "It's a new post!",
|
:body => "It's a new post!",
|
||||||
:author => 5
|
:author_id => 5
|
||||||
}
|
}
|
||||||
}, hash.as_json)
|
}, hash.as_json)
|
||||||
end
|
end
|
||||||
@ -776,12 +776,12 @@ class SerializerTest < ActiveModel::TestCase
|
|||||||
actual = ActiveModel::ArraySerializer.new([post], :root => :posts).as_json
|
actual = ActiveModel::ArraySerializer.new([post], :root => :posts).as_json
|
||||||
assert_equal({
|
assert_equal({
|
||||||
:posts => [
|
:posts => [
|
||||||
{ :title => "New Post", :body => "NEW POST", :id => 1, :comments => [1,2] }
|
{ :title => "New Post", :body => "NEW POST", :id => 1, :comment_ids => [1,2] }
|
||||||
],
|
],
|
||||||
|
|
||||||
:comments => [
|
:comments => [
|
||||||
{ :body => "EWOT", :id => 1, :tags => [1,3] },
|
{ :body => "EWOT", :id => 1, :tag_ids => [1,3] },
|
||||||
{ :body => "YARLY", :id => 2, :tags => [1,2] }
|
{ :body => "YARLY", :id => 2, :tag_ids => [1,2] }
|
||||||
],
|
],
|
||||||
|
|
||||||
:tags => [
|
:tags => [
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user