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
Elasticsearch
instead ofActiveRecord
and I would like to return the result set counts. BecauseActiveRecord::Relation
is referenced directly in the counting method, the app explodes. Under the current implementation, my options are to:Pagination.count_records
in every Elasticsearch-backed service I writejsonapi_render
viaoptions[: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
RecordCounter
module that responds tocount( records, params = {}, options ={} )
and loops through dedicatedCounter
classes that know how to count a specific class type. So like,ActiveRecord::Relation
objects go to theActiveRecordCounter
andArray
s go to theArrayCounter
class.The
#{Type}Counter
classes register themselves withRecordCounter
on 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::Relation
in the counting mechanismJSONAPI::Utils
Example
To use my use case, an Elasticsearch-backed system would implement a counter approximately like so: