Skip to content

Commit

Permalink
Added ActiveModel::Serializer#default_include class attribute to impl…
Browse files Browse the repository at this point in the history
…ement rails-api#1333

Added test to cover new feature
  • Loading branch information
iMacTia committed Jul 14, 2016
1 parent ce57006 commit a6495fb
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 2 deletions.
14 changes: 12 additions & 2 deletions lib/active_model/serializer/associations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ module Associations

included do
with_options instance_writer: false, instance_reader: true do |serializer|
serializer.class_attribute :_reflections
serializer.class_attribute :_reflections, :_default_include
self._reflections ||= []
end

Expand Down Expand Up @@ -65,6 +65,14 @@ def has_one(name, options = {}, &block) # rubocop:disable Style/PredicateName
associate(HasOneReflection.new(name, options, block))
end

# Set _default_include to the parsed value of +include+.
# @param includes value to be parsed by JSONAPI::IncludeDirective::Parser
# @return [void]
#
def default_include(include, options = {})
self._default_include = JSONAPI::IncludeDirective.new(include, options)
end

private

# Add reflection and define {name} accessor.
Expand All @@ -74,7 +82,7 @@ def has_one(name, options = {}, &block) # rubocop:disable Style/PredicateName
# @api private
#
def associate(reflection)
self._reflections << reflection
_reflections << reflection
end
end

Expand All @@ -85,6 +93,8 @@ def associate(reflection)
def associations(include_directive = ActiveModelSerializers.default_include_directive)
return unless object

include_directive.merge!(_default_include) if _default_include

Enumerator.new do |y|
self.class._reflections.each do |reflection|
next if reflection.excluded?(self)
Expand Down
52 changes: 52 additions & 0 deletions test/serializers/default_include_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
require 'test_helper'

module ActiveModel
class Serializer
class DefaultIncludeTest < ActiveSupport::TestCase
class Blog < ActiveModelSerializers::Model
attr_accessor :id, :name, :posts
end
class Post < ActiveModelSerializers::Model
attr_accessor :id, :title, :author
end
class Author < ActiveModelSerializers::Model
attr_accessor :id, :name
end
class BlogSerializer < ActiveModel::Serializer
attributes :id
attribute :name, key: :title

has_many :posts
end
class PostSerializer < ActiveModel::Serializer
default_include [:author]
attributes :id, :title

has_one :author
end
class AuthorSerializer < ActiveModel::Serializer
attributes :id, :name
end

setup do
@authors = [Author.new(id: 1, name: 'Blog Author')]
@posts = [Post.new(id: 1, title: 'The first post', author: @authors.first), Post.new(id: 2, title: 'The second post', author: @authors.first)]
@blog = Blog.new(id: 2, name: 'The Blog', posts: @posts)
@serializer_instance = BlogSerializer.new(@blog)
@serializable = ActiveModelSerializers::SerializableResource.new(@blog, serializer: BlogSerializer, adapter: :attributes)
@expected_hash = {
id: 2,
title: 'The Blog',
posts: [
{ id: 1, title: 'The first post', author: { id: 1, name: 'Blog Author'} },
{ id: 2, title: 'The second post', author: { id: 1, name: 'Blog Author'} }
] }
end

test '#default_include allows to include associations in the result of #serializable_hash' do
assert_equal @serializable.serializable_hash, @serializer_instance.serializable_hash
assert_equal @expected_hash, @serializer_instance.serializable_hash
end
end
end
end

0 comments on commit a6495fb

Please sign in to comment.