mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-24 14:56:50 +00:00
Allow serializers to receive instructions to
explicitly include or exclude specific lists of associations.
This commit is contained in:
parent
cb316b00f7
commit
4ad9c64e46
@ -372,7 +372,23 @@ module ActiveModel
|
|||||||
|
|
||||||
def include_associations!(node)
|
def include_associations!(node)
|
||||||
_associations.each do |attr, klass|
|
_associations.each do |attr, klass|
|
||||||
include! attr, :node => node
|
opts = { :node => node }
|
||||||
|
|
||||||
|
if options.include?(:include) || options.include?(:exclude)
|
||||||
|
opts[:include] = included_association?(attr)
|
||||||
|
end
|
||||||
|
|
||||||
|
include! attr, opts
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def included_association?(name)
|
||||||
|
if options.key?(:include)
|
||||||
|
options[:include].include?(name)
|
||||||
|
elsif options.key?(:exclude)
|
||||||
|
!options[:exclude].include?(name)
|
||||||
|
else
|
||||||
|
true
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@ -305,4 +305,88 @@ class AssociationTest < ActiveModel::TestCase
|
|||||||
}, @root_hash)
|
}, @root_hash)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
class InclusionTest < AssociationTest
|
||||||
|
def setup
|
||||||
|
super
|
||||||
|
|
||||||
|
comment_serializer_class = @comment_serializer_class
|
||||||
|
|
||||||
|
@post_serializer_class.class_eval do
|
||||||
|
root :post
|
||||||
|
embed :ids, :include => true
|
||||||
|
has_many :comments, :serializer => comment_serializer_class
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_when_it_is_included
|
||||||
|
post_serializer = @post_serializer_class.new(
|
||||||
|
@post, nil, :include => [:comments]
|
||||||
|
)
|
||||||
|
|
||||||
|
json = post_serializer.as_json
|
||||||
|
|
||||||
|
assert_equal({
|
||||||
|
:post => {
|
||||||
|
:title => "New Post",
|
||||||
|
:body => "Body",
|
||||||
|
:comments => [ 1 ]
|
||||||
|
},
|
||||||
|
:comments => [
|
||||||
|
{ :id => 1, :body => "ZOMG A COMMENT" }
|
||||||
|
]
|
||||||
|
}, json)
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_when_it_is_not_included
|
||||||
|
post_serializer = @post_serializer_class.new(
|
||||||
|
@post, nil, :include => []
|
||||||
|
)
|
||||||
|
|
||||||
|
json = post_serializer.as_json
|
||||||
|
|
||||||
|
assert_equal({
|
||||||
|
:post => {
|
||||||
|
:title => "New Post",
|
||||||
|
:body => "Body",
|
||||||
|
:comments => [ 1 ]
|
||||||
|
}
|
||||||
|
}, json)
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_when_it_is_excluded
|
||||||
|
post_serializer = @post_serializer_class.new(
|
||||||
|
@post, nil, :exclude => [:comments]
|
||||||
|
)
|
||||||
|
|
||||||
|
json = post_serializer.as_json
|
||||||
|
|
||||||
|
assert_equal({
|
||||||
|
:post => {
|
||||||
|
:title => "New Post",
|
||||||
|
:body => "Body",
|
||||||
|
:comments => [ 1 ]
|
||||||
|
}
|
||||||
|
}, json)
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_when_it_is_not_excluded
|
||||||
|
post_serializer = @post_serializer_class.new(
|
||||||
|
@post, nil, :exclude => []
|
||||||
|
)
|
||||||
|
|
||||||
|
json = post_serializer.as_json
|
||||||
|
|
||||||
|
assert_equal({
|
||||||
|
:post => {
|
||||||
|
:title => "New Post",
|
||||||
|
:body => "Body",
|
||||||
|
:comments => [ 1 ]
|
||||||
|
},
|
||||||
|
:comments => [
|
||||||
|
{ :id => 1, :body => "ZOMG A COMMENT" }
|
||||||
|
]
|
||||||
|
}, json)
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user