File tree Expand file tree Collapse file tree 2 files changed +15
-0
lines changed
unit/active_model/serializer Expand file tree Collapse file tree 2 files changed +15
-0
lines changed Original file line number Diff line number Diff line change @@ -113,6 +113,13 @@ class CategorySerializer < ActiveModel::Serializer
113
113
class PostSerializer < ActiveModel ::Serializer
114
114
attributes :title , :body
115
115
116
+ def title
117
+ keyword = serialization_options [ :highlight_keyword ]
118
+ title = object . read_attribute_for_serialization ( :title )
119
+ title = title . gsub ( keyword , "'#{ keyword } '" ) if keyword
120
+ title
121
+ end
122
+
116
123
has_many :comments
117
124
end
118
125
Original file line number Diff line number Diff line change @@ -21,6 +21,9 @@ def description
21
21
serialization_options [ :force_the_description ]
22
22
end
23
23
end
24
+
25
+ @category = Category . new ( { name : 'Category 1' } )
26
+ @category_serializer = CategorySerializer . new ( @category )
24
27
end
25
28
26
29
def test_filtered_attributes_serialization
@@ -29,6 +32,11 @@ def test_filtered_attributes_serialization
29
32
'profile' => { name : 'Name 1' , description : forced_description }
30
33
} , @profile_serializer . as_json ( force_the_description : forced_description ) )
31
34
end
35
+
36
+ def test_filtered_attributes_serialization_across_association
37
+ assert_equal ( "'T1'" ,
38
+ @category_serializer . as_json ( highlight_keyword : 'T1' ) [ 'category' ] [ :posts ] [ 0 ] [ :title ] )
39
+ end
32
40
end
33
41
end
34
42
end
You can’t perform that action at this time.
0 commit comments