@@ -44,13 +44,25 @@ def fragment_cache(cached_hash, non_cached_hash)
44
44
45
45
private
46
46
47
+ def resource_identifier_type ( serializer )
48
+ if ActiveModel ::Serializer . config . jsonapi_resource_type == :singular
49
+ serializer . object . class . model_name . singular
50
+ else
51
+ serializer . object . class . model_name . plural
52
+ end
53
+ end
54
+
55
+ def resource_identifier_id ( serializer )
56
+ if serializer . respond_to? ( 'id' )
57
+ serializer . id . to_s
58
+ else
59
+ serializer . object . id . to_s
60
+ end
61
+ end
62
+
47
63
def resource_identifier ( serializer )
48
- type = if ActiveModel ::Serializer . config . jsonapi_resource_type == :plural
49
- serializer . object . class . model_name . plural
50
- else
51
- serializer . object . class . model_name . singular
52
- end
53
- id = serializer . respond_to? ( 'id' ) ? serializer . id . to_s : serializer . object . id . to_s
64
+ type = resource_identifier_type ( serializer )
65
+ id = resource_identifier_id ( serializer )
54
66
55
67
{ id : id , type : type }
56
68
end
@@ -125,8 +137,8 @@ def resource_relationship_value(serializer, options = {})
125
137
else
126
138
if options [ :virtual_value ]
127
139
options [ :virtual_value ]
128
- elsif serializer . object
129
- resurce_identifier ( serializer )
140
+ elsif serializer && serializer . object
141
+ resource_identifier ( serializer )
130
142
else
131
143
nil
132
144
end
@@ -135,19 +147,13 @@ def resource_relationship_value(serializer, options = {})
135
147
136
148
def add_resource_relationships ( attrs , serializer , options = { } )
137
149
options [ :add_included ] = options . fetch ( :add_included , true )
138
-
139
150
attrs [ :relationships ] = { } if serializer . associations . any?
140
151
serializer . associations . each do |association |
141
- key = association . key
142
- serializer = association . serializer
143
- options = association . options
144
- value = resource_relationship_value ( serializer , options )
145
-
152
+ value = resource_relationship_value ( association . serializer , association . options )
146
153
attrs [ :relationships ] [ association . key ] = { data : value }
147
-
148
154
if options [ :add_included ]
149
- Array ( serializer ) . each do |s |
150
- add_included ( key , s )
155
+ Array ( association . serializer ) . each do |s |
156
+ add_included ( association . key , s )
151
157
end
152
158
end
153
159
end
0 commit comments