@@ -120,8 +120,8 @@ def build_response_document(object, options)
120120 # @option options [JSONAPI::Resource] :resource which resource class to be used
121121 # rather than using the default one (inferred)
122122 #
123- # @option options [ActiveRecord::Base, JSONAPI::Resource] :source source of related resource,
124- # the result should be interpreted as a related resources response
123+ # @option options [ActiveRecord::Base, JSONAPI::Resource] :source parent model/ resource
124+ # of the related resource
125125 #
126126 # @option options [String, Symbol] :relationship which relationship the data is from
127127 #
@@ -136,27 +136,35 @@ def build_collection_result(object, options)
136136 records = build_collection ( object , options )
137137 result_options = result_options ( object , options )
138138
139- if options [ :source ] . present? && related_resource_operation?
139+ if related_resource_operation? ( options )
140140 source_resource = turn_source_into_resource ( options [ :source ] )
141141 relationship_type = get_source_relationship ( options )
142- JSONAPI ::RelatedResourcesOperationResult . new ( :ok ,
143- source_resource ,
144- relationship_type ,
145- records ,
146- result_options
147- )
142+ JSONAPI ::RelatedResourcesOperationResult . new ( :ok , source_resource , relationship_type , records , result_options )
148143 else
149144 JSONAPI ::ResourcesOperationResult . new ( :ok , records , result_options )
150145 end
151146 end
152147
153148 # Is this a request for related resources?
154149 #
150+ # In order to answer that it needs to check for some {options}
151+ # controller params like {params[:source]} and {params[:relationship]}.
152+ #
153+ # @option options [Boolean] :related when true, jsonapi-utils infers the parent and
154+ # related resources from controller's {params} values.
155+ #
156+ # @option options [ActiveRecord::Base, JSONAPI::Resource] :source parent model/resource
157+ # of the related resource
158+ #
159+ # @option options [String, Symbol] :relationship which relationship the data is from
160+ #
155161 # @return [Boolean]
156162 #
157163 # @api private
158- def related_resource_operation?
159- params [ :source ] . present? && params [ :relationship ] . present?
164+ def related_resource_operation? ( options )
165+ ( options [ :related ] || options [ :source ] . present? ) &&
166+ params [ :source ] . present? &&
167+ params [ :relationship ] . present?
160168 end
161169
162170 # Apply a proper action setup for custom requests/actions.
@@ -263,16 +271,15 @@ def get_source_relationship(options)
263271 # @api private
264272 def result_options ( records , options )
265273 { } . tap do |data |
266- if JSONAPI . configuration . default_paginator != :none &&
267- JSONAPI . configuration . top_level_links_include_pagination
274+ if include_pagination_links?
268275 data [ :pagination_params ] = pagination_params ( records , options )
269276 end
270277
271278 if JSONAPI . configuration . top_level_meta_include_record_count
272279 data [ :record_count ] = record_count_for ( records , options )
273280 end
274281
275- if JSONAPI . configuration . top_level_meta_include_page_count
282+ if include_page_count?
276283 data [ :page_count ] = page_count_for ( data [ :record_count ] )
277284 end
278285 end
0 commit comments