Skip to content

Fix default_sort for show_related_resources#965

Merged
lgebhardt merged 1 commit intoJSONAPI-Resources:release-0-8from
nickgonzales:fix-to_many-default_sort
Feb 23, 2017
Merged

Fix default_sort for show_related_resources#965
lgebhardt merged 1 commit intoJSONAPI-Resources:release-0-8from
nickgonzales:fix-to_many-default_sort

Conversation

@nickgonzales
Copy link
Copy Markdown

The default_sort support from #756 does not work when querying for related resources. I searched through the source and found RelationshipBuilder::build_to_many has the following logic:

sort_criteria = options.fetch(:sort_criteria, {})
unless sort_criteria.nil? || sort_criteria.empty?
  order_options = relationship.resource_klass.construct_order_options(sort_criteria)
  records = resource_klass.apply_sort(records, order_options, @context)
end

The unless block shouldn't be there. It should be the resource's responsibility to apply sorts, or, if they're null, apply the default sort:

sort_criteria = options.fetch(:sort_criteria, {})
order_options = relationship.resource_klass.construct_order_options(sort_criteria)
records = resource_klass.apply_sort(records, order_options, @context)

This method looks totally different on the 0.9 branch, so I don't know if the issue exists there.

@lgebhardt lgebhardt merged commit 2fc37d8 into JSONAPI-Resources:release-0-8 Feb 23, 2017
@lgebhardt
Copy link
Copy Markdown
Contributor

@nickgonzales Thanks, the issue did exist on 0.9.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants