Non-ActiveRecord paginator support #90
Open
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
First and foremost: thanks so much for putting this gem out here!
Problem Statement
I have a service that uses
Elasticsearchinstead ofActiveRecordand I would like to return the result set counts. BecauseActiveRecord::Relationis referenced directly in the counting method, the app explodes. Under the current implementation, my options are to:Pagination.count_recordsin every Elasticsearch-backed service I writejsonapi_renderviaoptions[:count]Option one is really a strawman, and it felt odd to sprinkle
options: { count: MyEsModel.count( params ) }in every single controller action across multiple controllers in several projects.Proposed Contribution
I extracted the underlying counting interface in to a
RecordCountermodule that responds tocount( records, params = {}, options ={} )and loops through dedicatedCounterclasses that know how to count a specific class type. So like,ActiveRecord::Relationobjects go to theActiveRecordCounterandArrays go to theArrayCounterclass.The
#{Type}Counterclasses register themselves withRecordCounteron declaration withcounts #{Type.to_s.underscore}making adding new counters as easy as just declaring them in your project.What this does
I will admit that this is a somewhat involved change, but there are several benefits to this added complexity. It:
ActiveRecord::Relationin the counting mechanismJSONAPI::UtilsExample
To use my use case, an Elasticsearch-backed system would implement a counter approximately like so: