Skip to content

Commit 01be251

Browse files
authored
Merge branch 'master' into resources-0.9.10
2 parents 26b1c16 + 4523300 commit 01be251

File tree

4 files changed

+47
-20
lines changed

4 files changed

+47
-20
lines changed

jsonapi-utils.gemspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ Gem::Specification.new do |spec|
2222
spec.add_runtime_dependency 'jsonapi-resources', '0.9.10'
2323

2424
spec.add_development_dependency 'bundler', '~> 1.10'
25-
spec.add_development_dependency 'rake', '~> 10.0'
25+
spec.add_development_dependency 'rake', '~> 12.3.3'
2626
spec.add_development_dependency 'rails', ENV['RAILS_VERSION'] || '~> 5.1'
2727
spec.add_development_dependency 'sqlite3', '~> 1.3.6'
2828
spec.add_development_dependency 'rspec-rails', '~> 3.1'

lib/jsonapi/utils/response/formatters.rb

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -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

lib/jsonapi/utils/support/pagination.rb

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,40 @@ module Support
44
module Pagination
55
RecordCountError = Class.new(ArgumentError)
66

7+
# Check whether pagination links should be included.
8+
#
9+
# @api public
10+
# @return [Boolean]
11+
def include_pagination_links?
12+
JSONAPI.configuration.default_paginator != :none &&
13+
JSONAPI.configuration.top_level_links_include_pagination
14+
end
15+
16+
# Check whether pagination's page count should be included
17+
# on the "meta" key.
18+
#
19+
# @api public
20+
# @return [Boolean]
21+
def include_page_count?
22+
JSONAPI.configuration.top_level_meta_include_page_count
23+
end
24+
725
# Apply proper pagination to the records.
826
#
927
# @param records [ActiveRecord::Relation, Array] collection of records
1028
# e.g.: User.all or [{ id: 1, name: 'Tiago' }, { id: 2, name: 'Doug' }]
1129
#
12-
# @param options [Hash] JU's options
30+
# @param options [Hash] JSONAPI::Utils' options
1331
# e.g.: { resource: V2::UserResource, count: 100 }
1432
#
1533
# @return [ActiveRecord::Relation, Array]
1634
#
1735
# @api public
1836
def apply_pagination(records, options = {})
19-
return records unless apply_pagination?(options)
20-
records.is_a?(Array) ? records[paginate_with(:range)] : paginate_with(:paginator).apply(records, nil)
37+
if !apply_pagination?(options) then records
38+
elsif records.is_a?(Array) then records[paginate_with(:range)]
39+
else paginate_with(:paginator).apply(records, nil)
40+
end
2141
end
2242

2343
# Mount pagination params for JSONAPI::ResourcesOperationResult.
@@ -32,9 +52,10 @@ def apply_pagination(records, options = {})
3252
# @return [Hash]
3353
# e.g.: {"first"=>{"number"=>1, "size"=>2}, "next"=>{"number"=>2, "size"=>2}, "last"=>{"number"=>2, "size"=>2}}
3454
#
55+
#
3556
# @api public
3657
def pagination_params(records, options)
37-
return {} unless JSONAPI.configuration.top_level_links_include_pagination
58+
return {} unless include_pagination_links?
3859
paginator.links_page_params(record_count: record_count_for(records, options))
3960
end
4061

spec/features/page_count_spec.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ def TestApp.draw_page_count_test_routes
2929
# Feature Tests
3030
##
3131

32-
3332
describe PageCountTestController, type: :controller do
3433
include_context 'JSON API headers'
3534

0 commit comments

Comments
 (0)