@@ -6,6 +6,8 @@ class JsonApi < Base
66 autoload :PaginationLinks
77 autoload :FragmentCache
88 autoload :Link
9+ autoload :Association
10+ autoload :ResourceIdentifier
911 autoload :Deserialization
1012
1113 # TODO: if we like this abstraction and other API objects to it,
@@ -97,7 +99,7 @@ def resource_objects_for(serializers)
9799 end
98100
99101 def process_resource ( serializer , primary )
100- resource_identifier = resource_identifier_for ( serializer )
102+ resource_identifier = JsonApi :: ResourceIdentifier . new ( serializer ) . as_json
101103 return false unless @resource_identifiers . add? ( resource_identifier )
102104
103105 resource_object = resource_object_for ( serializer )
@@ -127,45 +129,22 @@ def process_relationship(serializer, include_tree)
127129 process_relationships ( serializer , include_tree )
128130 end
129131
130- def resource_identifier_type_for ( serializer )
131- return serializer . _type if serializer . _type
132- if ActiveModelSerializers . config . jsonapi_resource_type == :singular
133- serializer . object . class . model_name . singular
134- else
135- serializer . object . class . model_name . plural
136- end
137- end
138-
139- def resource_identifier_id_for ( serializer )
140- if serializer . respond_to? ( :id )
141- serializer . id
142- else
143- serializer . object . id
144- end
145- end
146-
147- def resource_identifier_for ( serializer )
148- type = resource_identifier_type_for ( serializer )
149- id = resource_identifier_id_for ( serializer )
150-
151- { id : id . to_s , type : type }
152- end
153-
154132 def attributes_for ( serializer , fields )
155133 serializer . attributes ( fields ) . except ( :id )
156134 end
157135
158136 def resource_object_for ( serializer )
159137 resource_object = cache_check ( serializer ) do
160- resource_object = resource_identifier_for ( serializer )
138+ resource_object = JsonApi :: ResourceIdentifier . new ( serializer ) . as_json
161139
162140 requested_fields = fieldset && fieldset . fields_for ( resource_object [ :type ] )
163141 attributes = attributes_for ( serializer , requested_fields )
164142 resource_object [ :attributes ] = attributes if attributes . any?
165143 resource_object
166144 end
167145
168- relationships = relationships_for ( serializer )
146+ requested_associations = fieldset . fields_for ( resource_object [ :type ] ) || '*'
147+ relationships = relationships_for ( serializer , requested_associations )
169148 resource_object [ :relationships ] = relationships if relationships . any?
170149
171150 links = links_for ( serializer )
@@ -174,24 +153,15 @@ def resource_object_for(serializer)
174153 resource_object
175154 end
176155
177- def relationship_value_for ( serializer , options = { } )
178- if serializer . respond_to? ( :each )
179- serializer . map { |s | resource_identifier_for ( s ) }
180- else
181- if options [ :virtual_value ]
182- options [ :virtual_value ]
183- elsif serializer && serializer . object
184- resource_identifier_for ( serializer )
185- end
186- end
187- end
188-
189- def relationships_for ( serializer )
190- resource_type = resource_identifier_type_for ( serializer )
191- requested_associations = fieldset . fields_for ( resource_type ) || '*'
156+ def relationships_for ( serializer , requested_associations )
192157 include_tree = IncludeTree . from_include_args ( requested_associations )
193158 serializer . associations ( include_tree ) . each_with_object ( { } ) do |association , hash |
194- hash [ association . key ] = { data : relationship_value_for ( association . serializer , association . options ) }
159+ hash [ association . key ] = JsonApi ::Association . new ( serializer ,
160+ association . serializer ,
161+ association . options ,
162+ association . links ,
163+ association . meta )
164+ . as_json
195165 end
196166 end
197167
0 commit comments