diff --git a/lib/active_model/serializer.rb b/lib/active_model/serializer.rb index f0fe62ae..106c5e71 100644 --- a/lib/active_model/serializer.rb +++ b/lib/active_model/serializer.rb @@ -98,14 +98,23 @@ module ActiveModel @object = object end - def attributes + def attributes(options = {}) self.class._attributes.dup.each_with_object({}) do |name, hash| hash[name] = send(name) end end - def associations - self.class._associations.dup + def associations(options = {}) + self.class._associations.dup.each_with_object({}) do |(name, value), hash| + association = object.send(name) + serializer_class = ActiveModel::Serializer.serializer_for(association) + if serializer_class + serializer = serializer_class.new(association) + hash[name] = serializer + else + hash[name] = association + end + end end end end diff --git a/lib/active_model/serializer/array_serializer.rb b/lib/active_model/serializer/array_serializer.rb index d385d6bf..13ab08c7 100644 --- a/lib/active_model/serializer/array_serializer.rb +++ b/lib/active_model/serializer/array_serializer.rb @@ -1,7 +1,9 @@ module ActiveModel class Serializer - class ArraySerializer - + class ArraySerializer < Serializer + def initialize(object) + @object = object + end end end end diff --git a/test/serializers/associations_test.rb b/test/serializers/associations_test.rb index 46dcb63b..45fbd8d1 100644 --- a/test/serializers/associations_test.rb +++ b/test/serializers/associations_test.rb @@ -26,58 +26,38 @@ module ActiveModel end end end + Post = Class.new(Model) + Comment = Class.new(Model) + PostSerializer = Class.new(ActiveModel::Serializer) do + attributes :title, :body + + has_many :comments + end + + CommentSerializer = Class.new(ActiveModel::Serializer) do + attributes :id, :body + + belongs_to :post + end def setup - @post = Model.new({ title: 'New Post', body: 'Body' }) - @comment = Model.new({ id: 1, body: 'ZOMG A COMMENT' }) + @post = Post.new({ title: 'New Post', body: 'Body' }) + @comment = Comment.new({ id: 1, body: 'ZOMG A COMMENT' }) @post.comments = [@comment] @comment.post = @post - @post_serializer_class = def_serializer do - attributes :title, :body - end - - @comment_serializer_class = def_serializer do - attributes :id, :body - end - - @post_serializer = @post_serializer_class.new(@post) - @comment_serializer = @comment_serializer_class.new(@comment) + @post_serializer = PostSerializer.new(@post) + @comment_serializer = CommentSerializer.new(@comment) end def test_has_many - @post_serializer_class.class_eval do - has_many :comments - end - assert_equal({comments: {type: :has_many, options: {}}}, @post_serializer.class._associations) + assert_kind_of(ActiveModel::Serializer::ArraySerializer, @post_serializer.associations[:comments]) end def test_has_one - @comment_serializer_class.class_eval do - belongs_to :post - end - assert_equal({post: {type: :belongs_to, options: {}}}, @comment_serializer.class._associations) - end - - def test_associations - @comment_serializer_class.class_eval do - belongs_to :post - has_many :comments - end - - expected_associations = { - post: { - type: :belongs_to, - options: {} - }, - comments: { - type: :has_many, - options: {} - }, - } - assert_equal(expected_associations, @comment_serializer.associations) + assert_kind_of(PostSerializer, @comment_serializer.associations[:post]) end end end