mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-22 22:06:50 +00:00
Merge pull request #725 from ggordon/has_one_support
Support has_one to be compatible with 0.8.x
This commit is contained in:
commit
3389218fd3
@ -2,3 +2,4 @@
|
|||||||
|
|
||||||
* adds support for `meta` and `meta_key` [@kurko]
|
* adds support for `meta` and `meta_key` [@kurko]
|
||||||
* adds method to override association [adcb99e, @kurko]
|
* adds method to override association [adcb99e, @kurko]
|
||||||
|
* add `has_one` attribute for backwards compatibility [@ggordon]
|
||||||
|
|||||||
@ -224,13 +224,14 @@ $ rails g serializer post
|
|||||||
```
|
```
|
||||||
|
|
||||||
The generated seralizer will contain basic `attributes` and
|
The generated seralizer will contain basic `attributes` and
|
||||||
`has_many`/`belongs_to` declarations, based on the model. For example:
|
`has_many`/`has_one`/`belongs_to` declarations, based on the model. For example:
|
||||||
|
|
||||||
```ruby
|
```ruby
|
||||||
class PostSerializer < ActiveModel::Serializer
|
class PostSerializer < ActiveModel::Serializer
|
||||||
attributes :title, :body
|
attributes :title, :body
|
||||||
|
|
||||||
has_many :comments
|
has_many :comments
|
||||||
|
has_one :author
|
||||||
|
|
||||||
url :post
|
url :post
|
||||||
end
|
end
|
||||||
@ -250,7 +251,7 @@ end
|
|||||||
|
|
||||||
The attribute names are a **whitelist** of attributes to be serialized.
|
The attribute names are a **whitelist** of attributes to be serialized.
|
||||||
|
|
||||||
The `has_many` and `belongs_to` declarations describe relationships between
|
The `has_many`, `has_one`, and `belongs_to` declarations describe relationships between
|
||||||
resources. By default, when you serialize a `Post`, you will get its `Comment`s
|
resources. By default, when you serialize a `Post`, you will get its `Comment`s
|
||||||
as well.
|
as well.
|
||||||
|
|
||||||
|
|||||||
@ -66,6 +66,16 @@ module ActiveModel
|
|||||||
associate(:belongs_to, attrs)
|
associate(:belongs_to, attrs)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Defines an association in the object should be rendered.
|
||||||
|
#
|
||||||
|
# The serializer object should implement the association name
|
||||||
|
# as a method which should return an object when invoked. If a method
|
||||||
|
# with the association name does not exist, the association name is
|
||||||
|
# dispatched to the serialized object.
|
||||||
|
def self.has_one(*attrs)
|
||||||
|
associate(:has_one, attrs)
|
||||||
|
end
|
||||||
|
|
||||||
def self.associate(type, attrs) #:nodoc:
|
def self.associate(type, attrs) #:nodoc:
|
||||||
options = attrs.extract_options!
|
options = attrs.extract_options!
|
||||||
self._associations = _associations.dup
|
self._associations = _associations.dup
|
||||||
|
|||||||
44
test/adapter/json_api/has_one_test.rb
Normal file
44
test/adapter/json_api/has_one_test.rb
Normal file
@ -0,0 +1,44 @@
|
|||||||
|
require 'test_helper'
|
||||||
|
|
||||||
|
module ActiveModel
|
||||||
|
class Serializer
|
||||||
|
class Adapter
|
||||||
|
class JsonApi
|
||||||
|
class HasOneTest < Minitest::Test
|
||||||
|
def setup
|
||||||
|
@author = Author.new(id: 1, name: 'Steve K.')
|
||||||
|
@bio = Bio.new(id: 43, content: 'AMS Contributor')
|
||||||
|
@author.bio = @bio
|
||||||
|
@bio.author = @author
|
||||||
|
@post = Post.new(id: 42, title: 'New Post', body: 'Body')
|
||||||
|
@anonymous_post = Post.new(id: 43, title: 'Hello!!', body: 'Hello, world!!')
|
||||||
|
@comment = Comment.new(id: 1, body: 'ZOMG A COMMENT')
|
||||||
|
@post.comments = [@comment]
|
||||||
|
@anonymous_post.comments = []
|
||||||
|
@comment.post = @post
|
||||||
|
@comment.author = nil
|
||||||
|
@post.author = @author
|
||||||
|
@anonymous_post.author = nil
|
||||||
|
@blog = Blog.new(id: 1, name: "My Blog!!")
|
||||||
|
@blog.writer = @author
|
||||||
|
@blog.articles = [@post, @anonymous_post]
|
||||||
|
@author.posts = []
|
||||||
|
@author.roles = []
|
||||||
|
|
||||||
|
@serializer = AuthorSerializer.new(@author)
|
||||||
|
@adapter = ActiveModel::Serializer::Adapter::JsonApi.new(@serializer, include: 'bio,posts')
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_includes_bio_id
|
||||||
|
assert_equal("43", @adapter.serializable_hash[:authors][:links][:bio])
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_includes_linked_bio
|
||||||
|
@adapter = ActiveModel::Serializer::Adapter::JsonApi.new(@serializer, include: 'bio')
|
||||||
|
assert_equal([{id: "43", :content=>"AMS Contributor", :links=>{:author=>"1"}}], @adapter.serializable_hash[:linked][:bios])
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
@ -10,9 +10,9 @@ module ActiveModel
|
|||||||
@author2 = Author.new(id: 2, name: 'Tenderlove')
|
@author2 = Author.new(id: 2, name: 'Tenderlove')
|
||||||
@bio1 = Bio.new(id: 1, content: 'AMS Contributor')
|
@bio1 = Bio.new(id: 1, content: 'AMS Contributor')
|
||||||
@bio2 = Bio.new(id: 2, content: 'Rails Contributor')
|
@bio2 = Bio.new(id: 2, content: 'Rails Contributor')
|
||||||
@first_post = Post.new(id: 1, title: 'Hello!!', body: 'Hello, world!!')
|
@first_post = Post.new(id: 10, title: 'Hello!!', body: 'Hello, world!!')
|
||||||
@second_post = Post.new(id: 2, title: 'New Post', body: 'Body')
|
@second_post = Post.new(id: 20, title: 'New Post', body: 'Body')
|
||||||
@third_post = Post.new(id: 3, title: 'Yet Another Post', body: 'Body')
|
@third_post = Post.new(id: 30, title: 'Yet Another Post', body: 'Body')
|
||||||
@blog = Blog.new({ name: 'AMS Blog' })
|
@blog = Blog.new({ name: 'AMS Blog' })
|
||||||
@first_post.blog = @blog
|
@first_post.blog = @blog
|
||||||
@second_post.blog = @blog
|
@second_post.blog = @blog
|
||||||
@ -44,8 +44,8 @@ module ActiveModel
|
|||||||
@second_comment.post = @first_post
|
@second_comment.post = @first_post
|
||||||
@second_comment.author = nil
|
@second_comment.author = nil
|
||||||
assert_equal([
|
assert_equal([
|
||||||
{ title: "Hello!!", body: "Hello, world!!", id: "1", links: { comments: ['1', '2'], blog: "999", author: "1" } },
|
{ title: "Hello!!", body: "Hello, world!!", id: "10", links: { comments: ['1', '2'], blog: "999", author: "1" } },
|
||||||
{ title: "New Post", body: "Body", id: "2", links: { comments: [], blog: "999", author: "2" } }
|
{ title: "New Post", body: "Body", id: "20", links: { comments: [], blog: "999", author: "2" } }
|
||||||
], @adapter.serializable_hash[:posts])
|
], @adapter.serializable_hash[:posts])
|
||||||
|
|
||||||
|
|
||||||
@ -54,14 +54,14 @@ module ActiveModel
|
|||||||
id: "1",
|
id: "1",
|
||||||
body: "ZOMG A COMMENT",
|
body: "ZOMG A COMMENT",
|
||||||
links: {
|
links: {
|
||||||
post: "1",
|
post: "10",
|
||||||
author: nil
|
author: nil
|
||||||
}
|
}
|
||||||
}, {
|
}, {
|
||||||
id: "2",
|
id: "2",
|
||||||
body: "ZOMG ANOTHER COMMENT",
|
body: "ZOMG ANOTHER COMMENT",
|
||||||
links: {
|
links: {
|
||||||
post: "1",
|
post: "10",
|
||||||
author: nil
|
author: nil
|
||||||
}
|
}
|
||||||
}],
|
}],
|
||||||
@ -69,7 +69,7 @@ module ActiveModel
|
|||||||
id: "1",
|
id: "1",
|
||||||
name: "Steve K.",
|
name: "Steve K.",
|
||||||
links: {
|
links: {
|
||||||
posts: ["1", "3"],
|
posts: ["10", "30"],
|
||||||
roles: [],
|
roles: [],
|
||||||
bio: "1"
|
bio: "1"
|
||||||
}
|
}
|
||||||
@ -77,7 +77,7 @@ module ActiveModel
|
|||||||
id: "2",
|
id: "2",
|
||||||
name: "Tenderlove",
|
name: "Tenderlove",
|
||||||
links: {
|
links: {
|
||||||
posts: ["2"],
|
posts: ["20"],
|
||||||
roles: [],
|
roles: [],
|
||||||
bio: "2"
|
bio: "2"
|
||||||
}
|
}
|
||||||
@ -117,7 +117,7 @@ module ActiveModel
|
|||||||
id: "1",
|
id: "1",
|
||||||
name: "Steve K.",
|
name: "Steve K.",
|
||||||
links: {
|
links: {
|
||||||
posts: ["1", "3"],
|
posts: ["10", "30"],
|
||||||
roles: [],
|
roles: [],
|
||||||
bio: "1"
|
bio: "1"
|
||||||
}
|
}
|
||||||
@ -125,7 +125,7 @@ module ActiveModel
|
|||||||
posts: [{
|
posts: [{
|
||||||
title: "Hello!!",
|
title: "Hello!!",
|
||||||
body: "Hello, world!!",
|
body: "Hello, world!!",
|
||||||
id: "1",
|
id: "10",
|
||||||
links: {
|
links: {
|
||||||
comments: ["1", "2"],
|
comments: ["1", "2"],
|
||||||
blog: "999",
|
blog: "999",
|
||||||
@ -134,7 +134,7 @@ module ActiveModel
|
|||||||
}, {
|
}, {
|
||||||
title: "Yet Another Post",
|
title: "Yet Another Post",
|
||||||
body: "Body",
|
body: "Body",
|
||||||
id: "3",
|
id: "30",
|
||||||
links: {
|
links: {
|
||||||
comments: [],
|
comments: [],
|
||||||
blog: nil,
|
blog: nil,
|
||||||
|
|||||||
2
test/fixtures/poro.rb
vendored
2
test/fixtures/poro.rb
vendored
@ -99,7 +99,7 @@ AuthorSerializer = Class.new(ActiveModel::Serializer) do
|
|||||||
|
|
||||||
has_many :posts, embed: :ids
|
has_many :posts, embed: :ids
|
||||||
has_many :roles, embed: :ids
|
has_many :roles, embed: :ids
|
||||||
belongs_to :bio
|
has_one :bio
|
||||||
end
|
end
|
||||||
|
|
||||||
RoleSerializer = Class.new(ActiveModel::Serializer) do
|
RoleSerializer = Class.new(ActiveModel::Serializer) do
|
||||||
|
|||||||
@ -23,7 +23,6 @@ module ActiveModel
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
def setup
|
def setup
|
||||||
@author = Author.new(name: 'Steve K.')
|
@author = Author.new(name: 'Steve K.')
|
||||||
@author.bio = nil
|
@author.bio = nil
|
||||||
@ -43,11 +42,11 @@ module ActiveModel
|
|||||||
@comment_serializer = CommentSerializer.new(@comment)
|
@comment_serializer = CommentSerializer.new(@comment)
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_has_many
|
def test_has_many_and_has_one
|
||||||
assert_equal(
|
assert_equal(
|
||||||
{ posts: { type: :has_many, association_options: { embed: :ids } },
|
{ posts: { type: :has_many, association_options: { embed: :ids } },
|
||||||
roles: { type: :has_many, association_options: { embed: :ids } },
|
roles: { type: :has_many, association_options: { embed: :ids } },
|
||||||
bio: { type: :belongs_to, association_options: {} } },
|
bio: { type: :has_one, association_options: {} } },
|
||||||
@author_serializer.class._associations
|
@author_serializer.class._associations
|
||||||
)
|
)
|
||||||
@author_serializer.each_association do |name, serializer, options|
|
@author_serializer.each_association do |name, serializer, options|
|
||||||
@ -67,7 +66,11 @@ module ActiveModel
|
|||||||
end
|
end
|
||||||
|
|
||||||
def test_belongs_to
|
def test_belongs_to
|
||||||
assert_equal({post: {type: :belongs_to, association_options: {}}, :author=>{:type=>:belongs_to, :association_options=>{}}}, @comment_serializer.class._associations)
|
assert_equal(
|
||||||
|
{ post: { type: :belongs_to, association_options: {} },
|
||||||
|
author: { type: :belongs_to, association_options: {} } },
|
||||||
|
@comment_serializer.class._associations
|
||||||
|
)
|
||||||
@comment_serializer.each_association do |name, serializer, options|
|
@comment_serializer.each_association do |name, serializer, options|
|
||||||
if name == :post
|
if name == :post
|
||||||
assert_equal({}, options)
|
assert_equal({}, options)
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user