You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
And this is without even supporting custom ordering. This is quite cumbersome, can lead to a lot of duplicated code, and therefore is also a source of potential bugs.
What we did in our project is to then add a helper method to our ApplicationController like so:
classApplicationController < ActionController::API[...]private# Convenience method to paginate records with the RailsCursorPagination# gem and passing in the right parameter values.## @param [ActiveRecord::Relation] records# @return [Hash]defpaginate(records)RailsCursorPagination::Paginator.new(records,first: params[:first]&.to_i,after: params[:after],last: params[:last]&.to_i,before: params[:before]).fetch(with_total: ActiveModel::Type::Boolean.new.cast(params[:return_total]))endend
this then allows our controllers to be as simple as
It would be great if such a method would be directly provided by the gem so that any client can make use of it. It could e.g. be part of a concern that a user can load into their controllers or directly include in their ApplicationController to have it available on all controllers.
We would also have to consider how to allow users to add ordering to this. But this should probably be an opt-in and maybe only allowing ordering on selected columns to avoid performance issues from API users that order on un-optimized columns. I could imagine an interface similar to:
I'm interested in doing something like this - if we get the recent changes up, I'd be happy to upstream our current implementation that supports FE defined:
order field and order direction (within configured constraints per endpoint)
pagination direction (before / after)
Our Concern (which we'd be happy to upstream) does something similar to the interface you presented, but relies on the changes we suggested on the PR i've just opened!
Right now, to use the pagination in a Rails project, we have to write something like this in the controller:
And this is without even supporting custom ordering. This is quite cumbersome, can lead to a lot of duplicated code, and therefore is also a source of potential bugs.
What we did in our project is to then add a helper method to our
ApplicationController
like so:this then allows our controllers to be as simple as
It would be great if such a method would be directly provided by the gem so that any client can make use of it. It could e.g. be part of a concern that a user can load into their controllers or directly include in their
ApplicationController
to have it available on all controllers.We would also have to consider how to allow users to add ordering to this. But this should probably be an opt-in and maybe only allowing ordering on selected columns to avoid performance issues from API users that order on un-optimized columns. I could imagine an interface similar to:
The text was updated successfully, but these errors were encountered: