Skip to content

Commit 7136bdd

Browse files
authored
Merge pull request #1165 from HQTrust/hotfix-master/allow-pagination-for-nested-resources-but-not-includes
Allow pagination for nested resources but not for included resources
2 parents d185ebb + abe63e5 commit 7136bdd

3 files changed

Lines changed: 39 additions & 6 deletions

File tree

lib/jsonapi/active_relation_resource_finder.rb

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -110,13 +110,13 @@ def find_fragments(filters, options = {})
110110
# @return [Hash{ResourceIdentity => {identity: => ResourceIdentity, cache: cache_field, attributes: => {name => value}, related: {relationship_name: [] }}}]
111111
# the ResourceInstances matching the filters, sorting, and pagination rules along with any request
112112
# additional_field values
113-
def find_related_fragments(source_rids, relationship_name, options = {})
113+
def find_related_fragments(source_rids, relationship_name, options = {}, included_key = nil)
114114
relationship = _relationship(relationship_name)
115115

116116
if relationship.polymorphic? && relationship.foreign_key_on == :self
117117
find_related_polymorphic_fragments(source_rids, relationship, options)
118118
else
119-
find_related_monomorphic_fragments(source_rids, relationship, options)
119+
find_related_monomorphic_fragments(source_rids, relationship, included_key, options)
120120
end
121121
end
122122

@@ -162,7 +162,7 @@ def find_records_by_keys(keys, options = {})
162162
records.where({ _primary_key => keys })
163163
end
164164

165-
def find_related_monomorphic_fragments(source_rids, relationship, options = {})
165+
def find_related_monomorphic_fragments(source_rids, relationship, included_key, options = {})
166166
source_ids = source_rids.collect {|rid| rid.id}
167167

168168
context = options[:context]
@@ -185,7 +185,8 @@ def find_related_monomorphic_fragments(source_rids, relationship, options = {})
185185

186186
# ToDO: Remove count check. Currently pagination isn't working with multiple source_rids (i.e. it only works
187187
# for show relationships, not related includes).
188-
if paginator && source_rids.count == 1
188+
# Check included_key to not paginate included resources but ensure that nested resources can be paginated
189+
if paginator && source_rids.count == 1 && !included_key
189190
records = related_klass.apply_pagination(records, paginator, order_options)
190191
end
191192

lib/jsonapi/processor.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -469,7 +469,9 @@ def get_related(resource_klass, source_resources, include_related, options)
469469
find_related_resource_options[:sort_criteria] = relationship.resource_klass.default_sort
470470
find_related_resource_options[:cache] = resource_klass.caching?
471471

472-
related_identities = resource_klass.find_related_fragments(source_rids, relationship_name, find_related_resource_options)
472+
related_identities = resource_klass.find_related_fragments(
473+
source_rids, relationship_name, find_related_resource_options, key
474+
)
473475

474476
related_identities.each_pair do |identity, v|
475477
related[relationship_name][:resources][identity] =

test/unit/resource/active_relation_resource_finder_test.rb

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,36 @@ def test_find_related_has_many_fragments_no_attributes
121121
assert_equal 2, related_identities[JSONAPI::ResourceIdentity.new(TagResource, 502)][:related][:tags].length
122122
end
123123

124+
def test_find_related_has_many_fragments_pagination
125+
params = ActionController::Parameters.new(number: 2, size: 4)
126+
options = { paginator: PagedPaginator.new(params) }
127+
source_rids = [JSONAPI::ResourceIdentity.new(ARPostResource, 15)]
128+
129+
related_identities = ARPostResource.find_related_fragments(source_rids, 'tags', options)
130+
131+
assert_equal 1, related_identities.length
132+
assert_equal JSONAPI::ResourceIdentity.new(TagResource, 516), related_identities.keys[0]
133+
assert_equal JSONAPI::ResourceIdentity.new(TagResource, 516), related_identities.values[0][:identity]
134+
assert related_identities.values[0].is_a?(Hash)
135+
assert_equal 2, related_identities.values[0].length
136+
assert_equal 1, related_identities.values[0][:related][:tags].length
137+
end
138+
139+
def test_find_related_has_many_fragments_pagination_included_key
140+
params = ActionController::Parameters.new(number: 2, size: 4)
141+
options = { paginator: PagedPaginator.new(params) }
142+
source_rids = [JSONAPI::ResourceIdentity.new(ARPostResource, 15)]
143+
144+
related_identities = ARPostResource.find_related_fragments(source_rids, 'tags', options, :tags)
145+
146+
assert_equal 5, related_identities.length
147+
assert_equal JSONAPI::ResourceIdentity.new(TagResource, 502), related_identities.keys[0]
148+
assert_equal JSONAPI::ResourceIdentity.new(TagResource, 502), related_identities.values[0][:identity]
149+
assert related_identities.values[0].is_a?(Hash)
150+
assert_equal 2, related_identities.values[0].length
151+
assert_equal 1, related_identities.values[0][:related][:tags].length
152+
end
153+
124154
def test_find_related_has_many_fragments_cache_field
125155
options = { cache: true }
126156
source_rids = [JSONAPI::ResourceIdentity.new(ARPostResource, 1),
@@ -200,7 +230,7 @@ def test_find_related_polymorphic_fragments_cache_field
200230
end
201231

202232
def test_find_related_polymorphic_fragments_cache_field_attributes
203-
options = { cache: true , attributes: [:name] }
233+
options = { cache: true, attributes: [:name] }
204234
source_rids = [JSONAPI::ResourceIdentity.new(PictureResource, 1),
205235
JSONAPI::ResourceIdentity.new(PictureResource, 2),
206236
JSONAPI::ResourceIdentity.new(PictureResource, 20)]

0 commit comments

Comments
 (0)