Skip to content

Commit

Permalink
Support has_one to be compatible with 0.8.x
Browse files Browse the repository at this point in the history
  • Loading branch information
ggordon committed Nov 11, 2014
1 parent 33e8d09 commit b8108ae
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 2 deletions.
10 changes: 10 additions & 0 deletions lib/active_model/serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,16 @@ def self.belongs_to(*attrs)
associate(:belongs_to, attrs)
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:
options = attrs.extract_options!
self._associations = _associations.dup
Expand Down
43 changes: 43 additions & 0 deletions test/adapter/json_api/has_one_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
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 = []

@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"}], @adapter.serializable_hash[:linked][:bios])
end
end
end
end
end
end
2 changes: 1 addition & 1 deletion test/fixtures/poro.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ class ProfileSerializer < ActiveModel::Serializer
attributes :id, :name

has_many :posts, embed: :ids
belongs_to :bio
has_one :bio
end

BioSerializer = Class.new(ActiveModel::Serializer) do
Expand Down
2 changes: 1 addition & 1 deletion test/serializers/associations_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def setup
def test_has_many
assert_equal(
{ posts: { type: :has_many, options: { embed: :ids } },
bio: { type: :belongs_to, options: {} } },
bio: { type: :has_one, options: {} } },
@author_serializer.class._associations
)
@author_serializer.each_association do |name, serializer, options|
Expand Down

0 comments on commit b8108ae

Please sign in to comment.