Skip to content

Give resource more control over sortable fields#1002

Merged
lgebhardt merged 1 commit intoJSONAPI-Resources:masterfrom
rfb:resource_sortable_field
Mar 21, 2017
Merged

Give resource more control over sortable fields#1002
lgebhardt merged 1 commit intoJSONAPI-Resources:masterfrom
rfb:resource_sortable_field

Conversation

@rfb
Copy link
Copy Markdown
Contributor

@rfb rfb commented Mar 15, 2017

I'm in a situation where I want clients to have repeatable, random sort
order on a collection. My solution is to pass a ?sort=rand-N, with N
being the seed used for sorting. This way, if the client was to page
through the list or link to the list, the sort order would be preserved.

For example, /providers?sort=rand-42 would apply a random sort order
with 42 as the key.

To accommodate this, I need JSONAPI::RequestParser to allow sort keys
that are not in a predetermined list. I think a good solution to this
would be to have the RequestParser defer to the Resource when checking
if a sort key is valid.

The existing behaviour is maintained by moving the check on
sortable_fields to sortable_field?, which can be overloaded by
application specific resource classes.

I will write some tests and docs if this approach is acceptable to the
maintainers.

I'm in a situation where I want clients to have repeatable, random sort
order on a collection. My solution is to pass a ?sort=rand-N, with N
being the seed used for sorting. This way, if the client was to page
through the list or link to the list, the sort order would be preserved.

For example, `/providers?sort=rand-42` would apply a random sort order
with 42 as the key.

To accommodate this, I need JSONAPI::RequestParser to allow sort keys
that are not in a predetermined list. I think a good solution to this
would be to have the RequestParser defer to the Resource when checking
if a sort key is valid.

The existing behaviour is maintained by moving the check on
sortable_fields to sortable_field?, which can be overloaded by
application specific resource classes.
@rfb
Copy link
Copy Markdown
Contributor Author

rfb commented Mar 16, 2017

related PR for the guides: https://github.com/cerebris/jsonapi-resources-site/pull/6/files

@lgebhardt lgebhardt merged commit f8606fb into JSONAPI-Resources:master Mar 21, 2017
@lgebhardt
Copy link
Copy Markdown
Contributor

@rfb Thanks!

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.

3 participants