-
Notifications
You must be signed in to change notification settings - Fork 21
Description
In some cases, we might want to access the paginated records directly and not just have the resulting hash object. Right now, to be able to access these, we would have to first get our paginated result:
posts = Post.all
pagination_result = RailsCursrorPagination::Paginator.new(first: first).fetch
and then access the actual records via
paginated_posts = pagination_result[:page].pluck(:data)This feels quite cumbersome just to access the paginated data.
Furthermore, it might also be nice to actually interface with objects rather than just having a Hash returned by the #fetch action.
Right now, all the gem's logic is mainly in one big God Class, our RailsCursorPagination::Paginator. So we could use this as an opportunity to also refactor this a bit. However, what's important is that we still want to be able to call
render json: pagination_resultand get the proper JSON response rendered.
So whatever #fetch returns, it should respond to #to_json and return the expected string. But we could do this while still having #fetch return something like a PaginationResult object which then returns a list of PaginationItems when calling #page on it. And each item could return its record when calling #data and could return something like a Cursor instance when #cursor is invoked. And ultimately, the PaginationResult could then have a method #records that collects all the Active Record instances from the individual PaginationItems.
This way, we could split the logic about what cursors are and how they work, what the actual page is and how it's transformed to a hash / to JSON into more self-contained blocks.