Skip to content

Commit 8dd0ed4

Browse files
committed
Build collection result in a separate method
1 parent 3820417 commit 8dd0ed4

File tree

1 file changed

+28
-25
lines changed

1 file changed

+28
-25
lines changed

lib/jsonapi/utils/response/formatters.rb

Lines changed: 28 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -105,20 +105,7 @@ def build_response_document(object, options)
105105
results = JSONAPI::OperationResults.new
106106

107107
if object.respond_to?(:to_ary)
108-
records = build_collection(object, options)
109-
110-
if params[:source].present? && params[:relationship].present?
111-
source_resource = turn_source_into_resource(options[:source], options)
112-
relationship_type = get_source_relationship(options)
113-
114-
results.add_result(JSONAPI::RelatedResourcesOperationResult.new(:ok,
115-
source_resource,
116-
relationship_type,
117-
records,
118-
result_options(object, options)))
119-
else
120-
results.add_result(JSONAPI::ResourcesOperationResult.new(:ok, records, result_options(object, options)))
121-
end
108+
results.add_result(build_collection_result(object, options))
122109
else
123110
record = turn_into_resource(object, options)
124111
results.add_result(JSONAPI::ResourceOperationResult.new(:ok, record))
@@ -127,6 +114,25 @@ def build_response_document(object, options)
127114
@_response_document = create_response_document(results)
128115
end
129116

117+
# TODO: add YARD documentation
118+
def build_collection_result(object, options)
119+
records = build_collection(object, options)
120+
result_options = result_options(object, options)
121+
122+
if params[:source].present? && params[:relationship].present?
123+
source_resource = turn_source_into_resource(options[:source])
124+
relationship_type = get_source_relationship(options)
125+
JSONAPI::RelatedResourcesOperationResult.new(:ok,
126+
source_resource,
127+
relationship_type,
128+
records,
129+
result_options
130+
)
131+
else
132+
JSONAPI::ResourcesOperationResult.new(:ok, records, result_options)
133+
end
134+
end
135+
130136
# Apply a proper action setup for custom requests/actions.
131137
#
132138
# @note The setup_(index|show)_action comes from JSONAPI::Resources' API.
@@ -156,10 +162,9 @@ def custom_get_request_with_params?
156162
# Objects to be instantiated as JSONAPI::Resource ones.
157163
# e.g.: User.all, [{ data: { id: 1, first_name: 'Tiago' } }]
158164
#
159-
# @option options [JSONAPI::Resource] resource: it tells the buider which resource
160-
# class to be used rather than use an infered one (default behaviour)
165+
# @option options [JSONAPI::Resource] :resource it resource class to be used rather than default one (infered)
161166
#
162-
# @option options [Integer] count: if it's rendering a collection of resources, the default
167+
# @option options [Integer] :count if it's rendering a collection of resources, the default
163168
# gem's counting method can be bypassed by the use of this options. It's shows then the total
164169
# records resulting from that request and also calculates the pagination.
165170
#
@@ -194,17 +199,15 @@ def turn_into_resource(record, options)
194199
end
195200

196201
# Get JSONAPI::Resource for source object
197-
# @option options [JSONAPI::Resource] resource: it tells which resource
198-
# class to be used rather than use an infered one (default behaviour)
202+
#
203+
# @param record [ActiveRecord::Base, JSONAPI::Resource]
204+
#
199205
# @return [JSONAPI::Resource]
200206
#
201207
# @api private
202-
def turn_source_into_resource(source, options)
203-
if source.kind_of? JSONAPI::Resource
204-
source
205-
else
206-
@request.source_klass.new(source, context)
207-
end
208+
def turn_source_into_resource(record)
209+
return record if record.kind_of?(JSONAPI::Resource)
210+
@request.source_klass.new(record, context)
208211
end
209212

210213
# Get relationship type of source object

0 commit comments

Comments
 (0)