@@ -50,8 +50,7 @@ def resource_identifier(serializer)
50
50
else
51
51
serializer . object . class . model_name . singular
52
52
end
53
- id = serializer . id . to_s if serializer . respond_to? ( 'id' )
54
- id ||= serializer . object . id . to_s
53
+ id = serializer . respond_to? ( 'id' ) ? serializer . id . to_s : serializer . object . id . to_s
55
54
56
55
{ id : id , type : type }
57
56
end
@@ -85,25 +84,18 @@ def add_included(resource_name, serializers, parent = nil)
85
84
86
85
def attributes_for_serializer ( serializer , options )
87
86
if serializer . respond_to? ( :each )
88
- result = [ ]
89
- serializer . each do |object |
90
- result << resource_object_for ( object , options )
91
- end
87
+ serializer . map { |s | resource_object_for ( s , options ) }
92
88
else
93
- result = resource_object_for ( serializer , options )
89
+ resource_object_for ( serializer , options )
94
90
end
95
- result
96
91
end
97
92
98
93
def resource_object_for ( serializer , options )
99
94
options [ :fields ] = @fieldset && @fieldset . fields_for ( serializer )
100
95
101
96
cache_check ( serializer ) do
102
- attributes = serializer . attributes ( options )
103
- attributes . delete ( :id )
104
-
105
97
result = resource_identifier ( serializer )
106
-
98
+ attributes = serializer . attributes ( options ) . except ( :id )
107
99
result [ :attributes ] = attributes if attributes . any?
108
100
result
109
101
end
@@ -127,25 +119,29 @@ def check_assoc(assoc)
127
119
end
128
120
end
129
121
122
+ def resource_relationship_value ( serializer , options = { } )
123
+ if serializer . respond_to? ( :each )
124
+ serializer . map { |s | resource_identifier ( s ) }
125
+ else
126
+ if options [ :virtual_value ]
127
+ options [ :virtual_value ]
128
+ elsif serializer . object
129
+ resurce_identifier ( serializer )
130
+ else
131
+ nil
132
+ end
133
+ end
134
+ end
135
+
130
136
def add_resource_relationships ( attrs , serializer , options = { } )
131
137
options [ :add_included ] = options . fetch ( :add_included , true )
132
138
133
- attrs [ :relationships ] || = { } if serializer . associations . any?
139
+ attrs [ :relationships ] = { } if serializer . associations . any?
134
140
serializer . associations . each do |association |
135
141
key = association . key
136
142
serializer = association . serializer
137
- opts = association . options
138
- value = if serializer . respond_to? ( :each )
139
- serializer . map { |s | resource_identifier ( s ) }
140
- else
141
- if opts [ :virtual_value ]
142
- opts [ :virtual_value ]
143
- elsif serializer && serializer . object
144
- resource_identifier ( serializer )
145
- else
146
- nil
147
- end
148
- end
143
+ options = association . options
144
+ value = resource_relationship_value ( serializer , options )
149
145
150
146
attrs [ :relationships ] [ association . key ] = { data : value }
151
147
0 commit comments