From ccc46b34bd1d259d624379f17923b6f62773ceef Mon Sep 17 00:00:00 2001 From: Gary Gordon Date: Thu, 6 Nov 2014 14:54:16 -0500 Subject: [PATCH] Support has_one to be compatible with 0.8.x --- lib/active_model/serializer.rb | 10 ++++++ test/adapter/json_api/has_one_test.rb | 44 +++++++++++++++++++++++++++ test/fixtures/poro.rb | 2 +- test/serializers/associations_test.rb | 2 +- 4 files changed, 56 insertions(+), 2 deletions(-) create mode 100644 test/adapter/json_api/has_one_test.rb diff --git a/lib/active_model/serializer.rb b/lib/active_model/serializer.rb index dbc2060d9..957f16b24 100644 --- a/lib/active_model/serializer.rb +++ b/lib/active_model/serializer.rb @@ -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 diff --git a/test/adapter/json_api/has_one_test.rb b/test/adapter/json_api/has_one_test.rb new file mode 100644 index 000000000..9310250ad --- /dev/null +++ b/test/adapter/json_api/has_one_test.rb @@ -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"}], @adapter.serializable_hash[:linked][:bios]) + end + end + end + end + end +end diff --git a/test/fixtures/poro.rb b/test/fixtures/poro.rb index ae319a03d..587495818 100644 --- a/test/fixtures/poro.rb +++ b/test/fixtures/poro.rb @@ -62,7 +62,7 @@ class ProfileSerializer < ActiveModel::Serializer has_many :posts, embed: :ids has_many :roles, embed: :ids - belongs_to :bio + has_one :bio end RoleSerializer = Class.new(ActiveModel::Serializer) do diff --git a/test/serializers/associations_test.rb b/test/serializers/associations_test.rb index b2278b450..4196637dc 100644 --- a/test/serializers/associations_test.rb +++ b/test/serializers/associations_test.rb @@ -44,7 +44,7 @@ def test_has_many assert_equal( { posts: { type: :has_many, options: { embed: :ids } }, roles: { 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|