Skip to content

Commit a2c2b0e

Browse files
committed
Check for the :source option when dealing with requests for related resources
1 parent 8dd0ed4 commit a2c2b0e

File tree

2 files changed

+39
-17
lines changed

2 files changed

+39
-17
lines changed

jsonapi-utils.gemspec

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,4 @@ Gem::Specification.new do |spec|
3030
spec.add_development_dependency 'smart_rspec', '~> 0.1.6'
3131
spec.add_development_dependency 'pry', '~> 0.10.3'
3232
spec.add_development_dependency 'pry-byebug'
33-
spec.add_development_dependency 'debase'
34-
spec.add_development_dependency 'ruby-debug-ide'
3533
end

lib/jsonapi/utils/response/formatters.rb

Lines changed: 39 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -82,17 +82,15 @@ def active_record_obj?(object)
8282
# Build the full response document.
8383
#
8484
# @param object [ActiveRecord::Base, ActiveRecord::Relation, Hash, Array<Hash>]
85-
# Object to be formatted into JSON
86-
# e.g.: User.first, User.all, { data: { id: 1, first_name: 'Tiago' } },
87-
# [{ data: { id: 1, first_name: 'Tiago' } }]
85+
# Object to be formatted into JSON.
8886
#
89-
# @option options [JSONAPI::Resource] resource: it tells the builder which resource
90-
# class to be used rather than use an infered one (default behaviour)
87+
# @option options [JSONAPI::Resource] :resource which resource class to be used
88+
# rather than using the default one (inferred)
9189
#
92-
# @option options [ActiveRecord::Base, JSONAPI::Resource] source: it tells the builder that this response is from a related resource
93-
# and the result should be interpreted as a related resources response
90+
# @option options [ActiveRecord::Base, JSONAPI::Resource] :source source of related resource,
91+
# the result should be interpreted as a related resources response
9492
#
95-
# @option options [String, Symbol] relationship: it tells that the builder which relationship the data is from
93+
# @option options [String, Symbol] :relationship which relationship the data is from
9694
#
9795
# @option options [Integer] count: if it's rendering a collection of resources, the default
9896
# gem's counting method can be bypassed by the use of this options. It's shows then the total
@@ -114,12 +112,31 @@ def build_response_document(object, options)
114112
@_response_document = create_response_document(results)
115113
end
116114

117-
# TODO: add YARD documentation
115+
# Build the result operation object for collection actions.
116+
#
117+
# @param object [ActiveRecord::Relation, Array<Hash>]
118+
# Object to be formatted into JSON.
119+
#
120+
# @option options [JSONAPI::Resource] :resource which resource class to be used
121+
# rather than using the default one (inferred)
122+
#
123+
# @option options [ActiveRecord::Base, JSONAPI::Resource] :source source of related resource,
124+
# the result should be interpreted as a related resources response
125+
#
126+
# @option options [String, Symbol] :relationship which relationship the data is from
127+
#
128+
# @option options [Integer] count: if it's rendering a collection of resources, the default
129+
# gem's counting method can be bypassed by the use of this options. It's shows then the total
130+
# records resulting from that request and also calculates the pagination.
131+
#
132+
# @return [JSONAPI::ResourcesOperationResult, JSONAPI::RelatedResourcesOperationResult]
133+
#
134+
# @api private
118135
def build_collection_result(object, options)
119136
records = build_collection(object, options)
120137
result_options = result_options(object, options)
121138

122-
if params[:source].present? && params[:relationship].present?
139+
if options[:source].present? && related_resource_operation?
123140
source_resource = turn_source_into_resource(options[:source])
124141
relationship_type = get_source_relationship(options)
125142
JSONAPI::RelatedResourcesOperationResult.new(:ok,
@@ -133,6 +150,15 @@ def build_collection_result(object, options)
133150
end
134151
end
135152

153+
# Is this a request for related resources?
154+
#
155+
# @return [Boolean]
156+
#
157+
# @api private
158+
def related_resource_operation?
159+
params[:source].present? && params[:relationship].present?
160+
end
161+
136162
# Apply a proper action setup for custom requests/actions.
137163
#
138164
# @note The setup_(index|show)_action comes from JSONAPI::Resources' API.
@@ -211,17 +237,15 @@ def turn_source_into_resource(record)
211237
end
212238

213239
# Get relationship type of source object
240+
#
214241
# @option options [Symbol] relationship: it tells which relationship
215242
# to be used rather than use an infered one (default behaviour)
243+
#
216244
# @return [Symbol]
217245
#
218246
# @api private
219247
def get_source_relationship(options)
220-
if options[:relationship].present?
221-
options[:relationship].to_sym
222-
else
223-
params[:relationship].to_sym || @request.resource_klass._type
224-
end
248+
options[:relationship]&.to_sym || @request.resource_klass._type
225249
end
226250

227251
# Apply some result options like pagination params and record count to collection responses.

0 commit comments

Comments
 (0)