File tree Expand file tree Collapse file tree 4 files changed +47
-1
lines changed Expand file tree Collapse file tree 4 files changed +47
-1
lines changed Original file line number Diff line number Diff line change @@ -17,6 +17,8 @@ Features:
17
17
- [ #1158 ] ( https://github.com/rails-api/active_model_serializers/pull/1158 ) Add support for wildcards in ` include ` option (@beauby )
18
18
- [ #1127 ] ( https://github.com/rails-api/active_model_serializers/pull/1127 ) Add support for nested
19
19
associations for JSON and Attributes adapters via the ` include ` option (@NullVoxPopuli , @beauby ).
20
+ - [ #1141 ] ( https://github.com/rails-api/active_model_serializers/pull/1141 ) Add support for
21
+ dynamically including or excluding attributes in serializers (@lautis ).
20
22
21
23
Fixes:
22
24
Original file line number Diff line number Diff line change @@ -188,6 +188,26 @@ class PostSerializer < ActiveModel::Serializer
188
188
end
189
189
```
190
190
191
+ ### Conditionally including or excluding attributes
192
+
193
+ You can also dynamically include or exclude serialized attributes by defining a
194
+ method named include_attributes. This is typically used to customize the output
195
+ based on ` current_user ` . For example:
196
+
197
+ ``` ruby
198
+ class PostSerializer < ActiveModel ::Serializer
199
+ attributes :id , :body , :author
200
+
201
+ def include_attributes (included )
202
+ if current_user.admin?
203
+ included
204
+ else
205
+ included - [:author ]
206
+ end
207
+ end
208
+ end
209
+ ```
210
+
191
211
### Built in Adapters
192
212
193
213
#### Attributes
Original file line number Diff line number Diff line change @@ -143,6 +143,10 @@ def json_key
143
143
root || object . class . model_name . to_s . underscore
144
144
end
145
145
146
+ def include_attributes ( included )
147
+ included
148
+ end
149
+
146
150
def attributes ( options = { } )
147
151
attributes =
148
152
if options [ :fields ]
@@ -151,7 +155,7 @@ def attributes(options = {})
151
155
self . class . _attributes . dup
152
156
end
153
157
154
- attributes . each_with_object ( { } ) do |name , hash |
158
+ include_attributes ( attributes ) . each_with_object ( { } ) do |name , hash |
155
159
unless self . class . _fragmented
156
160
hash [ name ] = send ( name )
157
161
else
Original file line number Diff line number Diff line change @@ -18,11 +18,31 @@ def test_attributes_definition
18
18
@profile_serializer . class . _attributes )
19
19
end
20
20
21
+ def test_attributes
22
+ assert_equal ( { name : 'Name 1' , description : 'Description 1' } ,
23
+ @profile_serializer . attributes )
24
+ end
25
+
21
26
def test_attributes_with_fields_option
22
27
assert_equal ( { name : 'Name 1' } ,
23
28
@profile_serializer . attributes ( fields : [ :name ] ) )
24
29
end
25
30
31
+ def test_attributes_with_fields_method
32
+ @profile_serializer . define_singleton_method ( :include_attributes ) do |included |
33
+ included - [ :name ]
34
+ end
35
+ assert_equal ( { description : 'Description 1' } ,
36
+ @profile_serializer . attributes )
37
+ end
38
+
39
+ def test_attributes_with_fields_option_and_method
40
+ @profile_serializer . define_singleton_method ( :include_attributes ) do |included |
41
+ included - [ :name ]
42
+ end
43
+ assert_equal ( { } , @profile_serializer . attributes ( fields : [ :name ] ) )
44
+ end
45
+
26
46
def test_attributes_inheritance_definition
27
47
assert_equal ( [ :id , :body ] , @serializer_klass . _attributes )
28
48
end
You can’t perform that action at this time.
0 commit comments