Skip to content

Option to get plain paginated records directly #1

@nicolas-fricke

Description

@nicolas-fricke

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_result

and 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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions