Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions lib/jsonapi/active_relation_resource_finder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -110,13 +110,13 @@ def find_fragments(filters, options = {})
# @return [Hash{ResourceIdentity => {identity: => ResourceIdentity, cache: cache_field, attributes: => {name => value}, related: {relationship_name: [] }}}]
# the ResourceInstances matching the filters, sorting, and pagination rules along with any request
# additional_field values
def find_related_fragments(source_rids, relationship_name, options = {})
def find_related_fragments(source_rids, relationship_name, options = {}, included_key = nil)
relationship = _relationship(relationship_name)

if relationship.polymorphic? && relationship.foreign_key_on == :self
find_related_polymorphic_fragments(source_rids, relationship, options)
else
find_related_monomorphic_fragments(source_rids, relationship, options)
find_related_monomorphic_fragments(source_rids, relationship, included_key, options)
end
end

Expand Down Expand Up @@ -162,7 +162,7 @@ def find_records_by_keys(keys, options = {})
records.where({ _primary_key => keys })
end

def find_related_monomorphic_fragments(source_rids, relationship, options = {})
def find_related_monomorphic_fragments(source_rids, relationship, included_key, options = {})
source_ids = source_rids.collect {|rid| rid.id}

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

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

Expand Down
4 changes: 3 additions & 1 deletion lib/jsonapi/processor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -469,7 +469,9 @@ def get_related(resource_klass, source_resources, include_related, options)
find_related_resource_options[:sort_criteria] = relationship.resource_klass.default_sort
find_related_resource_options[:cache] = resource_klass.caching?

related_identities = resource_klass.find_related_fragments(source_rids, relationship_name, find_related_resource_options)
related_identities = resource_klass.find_related_fragments(
source_rids, relationship_name, find_related_resource_options, key
)

related_identities.each_pair do |identity, v|
related[relationship_name][:resources][identity] =
Expand Down
32 changes: 31 additions & 1 deletion test/unit/resource/active_relation_resource_finder_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,36 @@ def test_find_related_has_many_fragments_no_attributes
assert_equal 2, related_identities[JSONAPI::ResourceIdentity.new(TagResource, 502)][:related][:tags].length
end

def test_find_related_has_many_fragments_pagination
params = ActionController::Parameters.new(number: 2, size: 4)
options = { paginator: PagedPaginator.new(params) }
source_rids = [JSONAPI::ResourceIdentity.new(ARPostResource, 15)]

related_identities = ARPostResource.find_related_fragments(source_rids, 'tags', options)

assert_equal 1, related_identities.length
assert_equal JSONAPI::ResourceIdentity.new(TagResource, 516), related_identities.keys[0]
assert_equal JSONAPI::ResourceIdentity.new(TagResource, 516), related_identities.values[0][:identity]
assert related_identities.values[0].is_a?(Hash)
assert_equal 2, related_identities.values[0].length
assert_equal 1, related_identities.values[0][:related][:tags].length
end

def test_find_related_has_many_fragments_pagination_included_key
params = ActionController::Parameters.new(number: 2, size: 4)
options = { paginator: PagedPaginator.new(params) }
source_rids = [JSONAPI::ResourceIdentity.new(ARPostResource, 15)]

related_identities = ARPostResource.find_related_fragments(source_rids, 'tags', options, :tags)

assert_equal 5, related_identities.length
assert_equal JSONAPI::ResourceIdentity.new(TagResource, 502), related_identities.keys[0]
assert_equal JSONAPI::ResourceIdentity.new(TagResource, 502), related_identities.values[0][:identity]
assert related_identities.values[0].is_a?(Hash)
assert_equal 2, related_identities.values[0].length
assert_equal 1, related_identities.values[0][:related][:tags].length
end

def test_find_related_has_many_fragments_cache_field
options = { cache: true }
source_rids = [JSONAPI::ResourceIdentity.new(ARPostResource, 1),
Expand Down Expand Up @@ -200,7 +230,7 @@ def test_find_related_polymorphic_fragments_cache_field
end

def test_find_related_polymorphic_fragments_cache_field_attributes
options = { cache: true , attributes: [:name] }
options = { cache: true, attributes: [:name] }
source_rids = [JSONAPI::ResourceIdentity.new(PictureResource, 1),
JSONAPI::ResourceIdentity.new(PictureResource, 2),
JSONAPI::ResourceIdentity.new(PictureResource, 20)]
Expand Down