Skip to content

Commit

Permalink
Merge pull request #708 from ride/fix-for-nil-associations
Browse files Browse the repository at this point in the history
Handle correctly null associations
  • Loading branch information
kurko committed Oct 31, 2014
2 parents baf3db1 + 19ac139 commit 462efc0
Show file tree
Hide file tree
Showing 12 changed files with 64 additions and 17 deletions.
2 changes: 1 addition & 1 deletion lib/active_model/serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ def each_association(&block)
self.class._associations.dup.each do |name, options|
association = object.send(name)
serializer_class = ActiveModel::Serializer.serializer_for(association)
serializer = serializer_class.new(association)
serializer = serializer_class.new(association) if serializer_class

if block_given?
block.call(name, serializer, options[:options])
Expand Down
6 changes: 5 additions & 1 deletion lib/active_model/serializer/adapter/json.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@ def serializable_hash(options = {})
array_serializer = association
@result[name] = array_serializer.map { |item| item.attributes(opts) }
else
@result[name] = association.attributes(options)
if association
@result[name] = association.attributes(options)
else
@result[name] = nil
end
end
end
end
Expand Down
14 changes: 9 additions & 5 deletions lib/active_model/serializer/adapter/json_api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,17 @@ def add_links(name, serializers, options)
end

def add_link(name, serializer, options)
@hash[@root][:links][name] = serializer.id.to_s
if serializer
@hash[@root][:links][name] = serializer.id.to_s

unless options[:embed] == :ids
plural_name = name.to_s.pluralize.to_sym
unless options[:embed] == :ids
plural_name = name.to_s.pluralize.to_sym

@hash[:linked][plural_name] ||= []
@hash[:linked][plural_name].push attributes_for_serializer(serializer, options)
@hash[:linked][plural_name] ||= []
@hash[:linked][plural_name].push attributes_for_serializer(serializer, options)
end
else
@hash[@root][:links][name] = nil
end
end

Expand Down
11 changes: 11 additions & 0 deletions test/adapter/json/belongs_to_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,13 @@ class Json
class BelongsToTest < Minitest::Test
def setup
@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
@post.author = @author
@anonymous_post.author = nil

@serializer = CommentSerializer.new(@comment)
@adapter = ActiveModel::Serializer::Adapter::Json.new(@serializer)
Expand All @@ -18,6 +22,13 @@ def setup
def test_includes_post
assert_equal({id: 42, title: 'New Post', body: 'Body'}, @adapter.serializable_hash[:post])
end

def test_include_nil_author
serializer = PostSerializer.new(@anonymous_post)
adapter = ActiveModel::Serializer::Adapter::Json.new(serializer)

assert_equal({title: "Hello!!", body: "Hello, world!!", id: 43, comments: [], author: nil}, adapter.serializable_hash)
end
end
end
end
Expand Down
7 changes: 5 additions & 2 deletions test/adapter/json/collection_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,22 @@ class Adapter
class Json
class Collection < Minitest::Test
def setup
@author = Author.new(id: 1, name: 'Steve K.')
@first_post = Post.new(id: 1, title: 'Hello!!', body: 'Hello, world!!')
@second_post = Post.new(id: 2, title: 'New Post', body: 'Body')
@first_post.comments = []
@second_post.comments = []
@first_post.author = @author
@second_post.author = @author

@serializer = ArraySerializer.new([@first_post, @second_post])
@adapter = ActiveModel::Serializer::Adapter::Json.new(@serializer)
end

def test_include_multiple_posts
assert_equal([
{title: "Hello!!", body: "Hello, world!!", id: 1, comments: []},
{title: "New Post", body: "Body", id: 2, comments: []}
{title: "Hello!!", body: "Hello, world!!", id: 1, comments: [], author: {id: 1, name: "Steve K."}},
{title: "New Post", body: "Body", id: 2, comments: [], author: {id: 1, name: "Steve K."}}
], @adapter.serializable_hash)
end
end
Expand Down
2 changes: 2 additions & 0 deletions test/adapter/json/has_many_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@ class Adapter
class Json
class HasManyTestTest < Minitest::Test
def setup
@author = Author.new(id: 1, name: 'Steve K.')
@post = Post.new(title: 'New Post', body: 'Body')
@first_comment = Comment.new(id: 1, body: 'ZOMG A COMMENT')
@second_comment = Comment.new(id: 2, body: 'ZOMG ANOTHER COMMENT')
@post.comments = [@first_comment, @second_comment]
@post.author = @author
@first_comment.post = @post
@second_comment.post = @post

Expand Down
12 changes: 12 additions & 0 deletions test/adapter/json_api/belongs_to_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,15 @@ class Adapter
class JsonApi
class BelongsToTest < Minitest::Test
def setup
@author = Author.new(id: 1, name: 'Steve K.')
@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
@post.author = @author
@anonymous_post.author = nil

@serializer = CommentSerializer.new(@comment)
@adapter = ActiveModel::Serializer::Adapter::JsonApi.new(@serializer)
Expand All @@ -22,6 +27,13 @@ def test_includes_post_id
def test_includes_linked_post
assert_equal([{id: "42", title: 'New Post', body: 'Body'}], @adapter.serializable_hash[:linked][:posts])
end

def test_include_nil_author
serializer = PostSerializer.new(@anonymous_post)
adapter = ActiveModel::Serializer::Adapter::JsonApi.new(serializer)

assert_equal({comments: [], author: nil}, adapter.serializable_hash[:posts][:links])
end
end
end
end
Expand Down
7 changes: 5 additions & 2 deletions test/adapter/json_api/collection_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,22 @@ class Adapter
class JsonApi
class Collection < Minitest::Test
def setup
@author = Author.new(id: 1, name: 'Steve K.')
@first_post = Post.new(id: 1, title: 'Hello!!', body: 'Hello, world!!')
@second_post = Post.new(id: 2, title: 'New Post', body: 'Body')
@first_post.comments = []
@second_post.comments = []
@first_post.author = @author
@second_post.author = @author

@serializer = ArraySerializer.new([@first_post, @second_post])
@adapter = ActiveModel::Serializer::Adapter::JsonApi.new(@serializer)
end

def test_include_multiple_posts
assert_equal([
{title: "Hello!!", body: "Hello, world!!", id: "1", links: {comments: []}},
{title: "New Post", body: "Body", id: "2", links: {comments: []}}
{title: "Hello!!", body: "Hello, world!!", id: "1", links: {comments: [], author: "1"}},
{title: "New Post", body: "Body", id: "2", links: {comments: [], author: "1"}}
], @adapter.serializable_hash[:posts])
end
end
Expand Down
2 changes: 2 additions & 0 deletions test/adapter/json_api/has_many_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@ class Adapter
class JsonApi
class HasManyTest < Minitest::Test
def setup
@author = Author.new(id: 1, name: 'Steve K.')
@post = Post.new(title: 'New Post', body: 'Body')
@first_comment = Comment.new(id: 1, body: 'ZOMG A COMMENT')
@second_comment = Comment.new(id: 2, body: 'ZOMG ANOTHER COMMENT')
@post.comments = [@first_comment, @second_comment]
@first_comment.post = @post
@second_comment.post = @post
@post.author = @author

@serializer = PostSerializer.new(@post)
@adapter = ActiveModel::Serializer::Adapter::JsonApi.new(@serializer)
Expand Down
2 changes: 2 additions & 0 deletions test/adapter/json_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@ class Serializer
class Adapter
class JsonTest < Minitest::Test
def setup
@author = Author.new(id: 1, name: 'Steve K.')
@post = Post.new(title: 'New Post', body: 'Body')
@first_comment = Comment.new(id: 1, body: 'ZOMG A COMMENT')
@second_comment = Comment.new(id: 2, body: 'ZOMG ANOTHER COMMENT')
@post.comments = [@first_comment, @second_comment]
@first_comment.post = @post
@second_comment.post = @post
@post.author = @author

@serializer = PostSerializer.new(@post)
@adapter = ActiveModel::Serializer::Adapter::Json.new(@serializer)
Expand Down
1 change: 1 addition & 0 deletions test/fixtures/poro.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ class ProfileSerializer < ActiveModel::Serializer
attributes :title, :body, :id

has_many :comments
belongs_to :author
url :comments
end

Expand Down
15 changes: 9 additions & 6 deletions test/serializers/associations_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

module ActiveModel
class Serializer
class AssocationsTest < Minitest::Test
class AssociationsTest < Minitest::Test
class Model
def initialize(hash={})
@attributes = hash
Expand All @@ -25,20 +25,23 @@ def method_missing(meth, *args)


def setup
@author = Author.new(name: 'Steve K.')
@post = Post.new({ title: 'New Post', body: 'Body' })
@comment = Comment.new({ id: 1, body: 'ZOMG A COMMENT' })
@post.comments = [@comment]
@comment.post = @post
@post.author = @author
@author.posts = [@post]

@post_serializer = PostSerializer.new(@post)
@author_serializer = AuthorSerializer.new(@author)
@comment_serializer = CommentSerializer.new(@comment)
end

def test_has_many
assert_equal({comments: {type: :has_many, options: {}}}, @post_serializer.class._associations)
@post_serializer.each_association do |name, serializer, options|
assert_equal(:comments, name)
assert_equal({}, options)
assert_equal({posts: {type: :has_many, options: {embed: :ids}}}, @author_serializer.class._associations)
@author_serializer.each_association do |name, serializer, options|
assert_equal(:posts, name)
assert_equal({embed: :ids}, options)
assert_kind_of(ActiveModel::Serializer.config.array_serializer, serializer)
end
end
Expand Down

0 comments on commit 462efc0

Please sign in to comment.