File tree Expand file tree Collapse file tree 4 files changed +31
-1
lines changed Expand file tree Collapse file tree 4 files changed +31
-1
lines changed Original file line number Diff line number Diff line change @@ -17,6 +17,8 @@ def setup
17
17
@second_comment . post = @post
18
18
@blog = Blog . new ( id : 1 , name : "My Blog!!" )
19
19
@post . blog = @blog
20
+ @tag = Tag . new ( id : 1 , name : "#hash_tag" )
21
+ @post . tags = [ @tag ]
20
22
21
23
@serializer = PostSerializer . new ( @post )
22
24
@adapter = ActiveModel ::Serializer ::Adapter ::Json . new ( @serializer )
@@ -28,9 +30,14 @@ def test_has_many
28
30
{ id : 2 , body : 'ZOMG ANOTHER COMMENT' }
29
31
] , @adapter . serializable_hash [ :post ] [ :comments ] )
30
32
end
33
+
34
+ def test_has_many_with_no_serializer
35
+ serializer = PostWithTagsSerializer . new ( @post )
36
+ adapter = ActiveModel ::Serializer ::Adapter ::Json . new ( serializer )
37
+ assert_includes ( adapter . as_json , :tags )
38
+ end
31
39
end
32
40
end
33
41
end
34
42
end
35
43
end
36
-
Original file line number Diff line number Diff line change @@ -27,6 +27,8 @@ def setup
27
27
@blog . articles = [ @post ]
28
28
@post . blog = @blog
29
29
@post_without_comments . blog = nil
30
+ @tag = Tag . new ( id : 1 , name : "#hash_tag" )
31
+ @post . tags = [ @tag ]
30
32
31
33
@serializer = PostSerializer . new ( @post )
32
34
@adapter = ActiveModel ::Serializer ::Adapter ::JsonApi . new ( @serializer )
@@ -103,6 +105,12 @@ def test_include_type_for_association_when_different_than_name
103
105
}
104
106
assert_equal expected , actual
105
107
end
108
+
109
+ def test_has_many_with_no_serializer
110
+ serializer = PostWithTagsSerializer . new ( @post )
111
+ adapter = ActiveModel ::Serializer ::Adapter ::JsonApi . new ( serializer )
112
+ assert_includes ( adapter . serializable_hash , :tags )
113
+ end
106
114
end
107
115
end
108
116
end
Original file line number Diff line number Diff line change @@ -76,6 +76,7 @@ class ProfilePreviewSerializer < ActiveModel::Serializer
76
76
User = Class . new ( Model )
77
77
Location = Class . new ( Model )
78
78
Place = Class . new ( Model )
79
+ Tag = Class . new ( Model )
79
80
Comment = Class . new ( Model ) do
80
81
# Uses a custom non-time-based cache key
81
82
def cache_key
@@ -224,6 +225,12 @@ def self.root_name
224
225
belongs_to :author , serializer : AuthorPreviewSerializer
225
226
end
226
227
228
+ PostWithTagsSerializer = Class . new ( ActiveModel ::Serializer ) do
229
+ attributes :id
230
+
231
+ has_many :tags
232
+ end
233
+
227
234
Spam ::UnrelatedLinkSerializer = Class . new ( ActiveModel ::Serializer ) do
228
235
attributes :id
229
236
end
Original file line number Diff line number Diff line change @@ -29,8 +29,10 @@ def setup
29
29
@author . roles = [ ]
30
30
@blog = Blog . new ( { name : 'AMS Blog' } )
31
31
@post = Post . new ( { title : 'New Post' , body : 'Body' } )
32
+ @tag = Tag . new ( { name : '#hashtagged' } )
32
33
@comment = Comment . new ( { id : 1 , body : 'ZOMG A COMMENT' } )
33
34
@post . comments = [ @comment ]
35
+ @post . tags = [ @tag ]
34
36
@post . blog = @blog
35
37
@comment . post = @post
36
38
@comment . author = nil
@@ -65,6 +67,12 @@ def test_has_many_and_has_one
65
67
end
66
68
end
67
69
70
+ def test_has_many_with_no_serializer
71
+ PostWithTagsSerializer . new ( @post ) . each_association do |name , serializer , options |
72
+ puts "The line above will crash this test"
73
+ end
74
+ end
75
+
68
76
def test_serializer_options_are_passed_into_associations_serializers
69
77
@post_serializer . each_association do |name , association |
70
78
if name == :comments
You can’t perform that action at this time.
0 commit comments