Skip to content

Commit

Permalink
Support paginating in collections
Browse files Browse the repository at this point in the history
  • Loading branch information
stevegeek committed Oct 2, 2024
1 parent 4986b27 commit b3d45f9
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 10 deletions.
5 changes: 0 additions & 5 deletions lib/quo/collection_backed_query.rb
Original file line number Diff line number Diff line change
Expand Up @@ -103,11 +103,6 @@ def to_collection
self
end

# @rbs return: Object & Enumerable[untyped]
def unwrap
underlying_query
end

private

# @rbs override
Expand Down
11 changes: 9 additions & 2 deletions lib/quo/query.rb
Original file line number Diff line number Diff line change
Expand Up @@ -367,8 +367,15 @@ def transformer
# The configured query is the underlying query with paging
def configured_query #: ActiveRecord::Relation
q = underlying_query
return q unless paged? && q.is_a?(ActiveRecord::Relation)
q.offset(offset).limit(sanitised_page_size)
return q unless paged?

if q.is_a?(ActiveRecord::Relation)
q.offset(offset).limit(sanitised_page_size)
elsif q.respond_to?(:[])
q[offset, sanitised_page_size]
else
q
end
end

def sanitised_page_size #: Integer
Expand Down
6 changes: 3 additions & 3 deletions sig/generated/quo/collection_backed_query.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ module Quo
# @rbs override
def group: ...

# @rbs override
def includes: ...

# The default implementation of `query` calls `collection` and preloads the includes, however you can also
# override this method to return an ActiveRecord::Relation or any other query-like object as usual in a Query object.
# @rbs return: Object & Enumerable[untyped]
Expand All @@ -52,9 +55,6 @@ module Quo
# @rbs override
def to_collection: ...

# @rbs return: Object & Enumerable[untyped]
def unwrap: () -> (Object & Enumerable[untyped])

private

# @rbs override
Expand Down
6 changes: 6 additions & 0 deletions sig/generated/quo/query.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,12 @@ module Quo
# Gets the count of all results ignoring the current page and page size (if set).
def count: () -> Integer

# Total number of items without paging applied.
def total_count: () -> Integer

# Alias for count
def size: () -> Integer

# Gets the actual count of elements in the page of results (assuming paging is being used, otherwise the count of
# all results)
def page_count: () -> Integer
Expand Down

0 comments on commit b3d45f9

Please sign in to comment.