mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-25 07:16:49 +00:00
Encapsulate serialized_associations; test inline associations
This commit is contained in:
@@ -126,6 +126,35 @@ module ActiveModel
|
||||
assert expected_association_keys.include? :site
|
||||
end
|
||||
|
||||
class InlineAssociationTestPostSerializer < ActiveModel::Serializer
|
||||
has_many :comments
|
||||
has_many :last_comments do
|
||||
object.comments.last(1)
|
||||
end
|
||||
end
|
||||
|
||||
def test_virtual_attribute_block
|
||||
comment1 = ::ARModels::Comment.create!(contents: 'first comment')
|
||||
comment2 = ::ARModels::Comment.create!(contents: 'last comment')
|
||||
post = ::ARModels::Post.create!(
|
||||
title: 'inline association test',
|
||||
body: 'etc',
|
||||
comments: [comment1, comment2]
|
||||
)
|
||||
actual = serializable(post, adapter: :attributes, serializer: InlineAssociationTestPostSerializer).as_json
|
||||
expected = {
|
||||
:comments => [
|
||||
{ :id => 1, :contents => 'first comment' },
|
||||
{ :id => 2, :contents => 'last comment' }
|
||||
],
|
||||
:last_comments => [
|
||||
{ :id => 2, :contents => 'last comment' }
|
||||
]
|
||||
}
|
||||
|
||||
assert_equal expected, actual
|
||||
end
|
||||
|
||||
class NamespacedResourcesTest < Minitest::Test
|
||||
class ResourceNamespace
|
||||
Post = Class.new(::Model)
|
||||
|
||||
Reference in New Issue
Block a user