Skip to content

Commit 651b99f

Browse files
committed
Support has_one to be compatible with 0.8.x
Update README and CHANGELOG
1 parent 77fb050 commit 651b99f

File tree

6 files changed

+66
-7
lines changed

6 files changed

+66
-7
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@
22

33
* adds support for `meta` and `meta_key` [@kurko]
44
* adds method to override association [adcb99e, @kurko]
5+
* add `has_one` attribute for backwards compatibility [@ggordon]

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -224,13 +224,14 @@ $ rails g serializer post
224224
```
225225

226226
The generated seralizer will contain basic `attributes` and
227-
`has_many`/`belongs_to` declarations, based on the model. For example:
227+
`has_many`/`has_one`/`belongs_to` declarations, based on the model. For example:
228228

229229
```ruby
230230
class PostSerializer < ActiveModel::Serializer
231231
attributes :title, :body
232232

233233
has_many :comments
234+
has_one :author
234235

235236
url :post
236237
end
@@ -250,7 +251,7 @@ end
250251

251252
The attribute names are a **whitelist** of attributes to be serialized.
252253

253-
The `has_many` and `belongs_to` declarations describe relationships between
254+
The `has_many`, `has_one`, and `belongs_to` declarations describe relationships between
254255
resources. By default, when you serialize a `Post`, you will get its `Comment`s
255256
as well.
256257

lib/active_model/serializer.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,16 @@ def self.belongs_to(*attrs)
6666
associate(:belongs_to, attrs)
6767
end
6868

69+
# Defines an association in the object should be rendered.
70+
#
71+
# The serializer object should implement the association name
72+
# as a method which should return an object when invoked. If a method
73+
# with the association name does not exist, the association name is
74+
# dispatched to the serialized object.
75+
def self.has_one(*attrs)
76+
associate(:has_one, attrs)
77+
end
78+
6979
def self.associate(type, attrs) #:nodoc:
7080
options = attrs.extract_options!
7181
self._associations = _associations.dup
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
require 'test_helper'
2+
3+
module ActiveModel
4+
class Serializer
5+
class Adapter
6+
class JsonApi
7+
class HasOneTest < Minitest::Test
8+
def setup
9+
@author = Author.new(id: 1, name: 'Steve K.')
10+
@bio = Bio.new(id: 43, content: 'AMS Contributor')
11+
@author.bio = @bio
12+
@bio.author = @author
13+
@post = Post.new(id: 42, title: 'New Post', body: 'Body')
14+
@anonymous_post = Post.new(id: 43, title: 'Hello!!', body: 'Hello, world!!')
15+
@comment = Comment.new(id: 1, body: 'ZOMG A COMMENT')
16+
@post.comments = [@comment]
17+
@anonymous_post.comments = []
18+
@comment.post = @post
19+
@comment.author = nil
20+
@post.author = @author
21+
@anonymous_post.author = nil
22+
@blog = Blog.new(id: 1, name: "My Blog!!")
23+
@blog.writer = @author
24+
@blog.articles = [@post, @anonymous_post]
25+
@author.posts = []
26+
@author.roles = []
27+
28+
@serializer = AuthorSerializer.new(@author)
29+
@adapter = ActiveModel::Serializer::Adapter::JsonApi.new(@serializer, include: 'bio,posts')
30+
end
31+
32+
def test_includes_bio_id
33+
assert_equal("43", @adapter.serializable_hash[:authors][:links][:bio])
34+
end
35+
36+
def test_includes_linked_bio
37+
@adapter = ActiveModel::Serializer::Adapter::JsonApi.new(@serializer, include: 'bio')
38+
assert_equal([{id: "43", :content=>"AMS Contributor", :links=>{:author=>"1"}}], @adapter.serializable_hash[:linked][:bios])
39+
end
40+
end
41+
end
42+
end
43+
end
44+
end

test/fixtures/poro.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ def self.root_name
9999

100100
has_many :posts, embed: :ids
101101
has_many :roles, embed: :ids
102-
belongs_to :bio
102+
has_one :bio
103103
end
104104

105105
RoleSerializer = Class.new(ActiveModel::Serializer) do

test/serializers/associations_test.rb

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ def method_missing(meth, *args)
2323
end
2424
end
2525

26-
2726
def setup
2827
@author = Author.new(name: 'Steve K.')
2928
@author.bio = nil
@@ -43,11 +42,11 @@ def setup
4342
@comment_serializer = CommentSerializer.new(@comment)
4443
end
4544

46-
def test_has_many
45+
def test_has_many_and_has_one
4746
assert_equal(
4847
{ posts: { type: :has_many, association_options: { embed: :ids } },
4948
roles: { type: :has_many, association_options: { embed: :ids } },
50-
bio: { type: :belongs_to, association_options: {} } },
49+
bio: { type: :has_one, association_options: {} } },
5150
@author_serializer.class._associations
5251
)
5352
@author_serializer.each_association do |name, serializer, options|
@@ -67,7 +66,11 @@ def test_has_many
6766
end
6867

6968
def test_belongs_to
70-
assert_equal({post: {type: :belongs_to, association_options: {}}, :author=>{:type=>:belongs_to, :association_options=>{}}}, @comment_serializer.class._associations)
69+
assert_equal(
70+
{ post: { type: :belongs_to, association_options: {} },
71+
author: { type: :belongs_to, association_options: {} } },
72+
@comment_serializer.class._associations
73+
)
7174
@comment_serializer.each_association do |name, serializer, options|
7275
if name == :post
7376
assert_equal({}, options)

0 commit comments

Comments
 (0)