File tree Expand file tree Collapse file tree 2 files changed +25
-1
lines changed Expand file tree Collapse file tree 2 files changed +25
-1
lines changed Original file line number Diff line number Diff line change @@ -125,6 +125,10 @@ def json_key
125
125
@root || object . class . model_name . to_s . underscore
126
126
end
127
127
128
+ def fields ( keys )
129
+ keys
130
+ end
131
+
128
132
def attributes ( options = { } )
129
133
attributes =
130
134
if options [ :fields ]
@@ -133,7 +137,7 @@ def attributes(options = {})
133
137
self . class . _attributes . dup
134
138
end
135
139
136
- attributes . each_with_object ( { } ) do |name , hash |
140
+ fields ( attributes ) . each_with_object ( { } ) do |name , hash |
137
141
unless self . class . _fragmented
138
142
hash [ name ] = send ( name )
139
143
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 ( :fields ) do |keys |
33
+ keys - [ :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 ( :fields ) do |keys |
41
+ keys - [ :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