Module: AppQuery::Paginatable

Extended by:
ActiveSupport::Concern
Defined in:
lib/app_query/paginatable.rb

Overview

Note:

This is a BaseQuery middleware. Include it in classes that inherit from BaseQuery and use the +paginate+ ERB helper in your SQL template.

Middleware concern that adds pagination support to BaseQuery subclasses.

Include this module in your query class to enable pagination with Kaminari-compatible result objects.

Provides two modes:

  • With count: Full pagination with page numbers (uses COUNT query)
  • Without count: Simple prev/next for large datasets (uses limit+1 trick)

Examples:

Basic usage

class ApplicationQuery < AppQuery::BaseQuery
  include AppQuery::Paginatable
  per_page 50
end

class ArticlesQuery < ApplicationQuery
  per_page 10
end

# With count (full pagination)
articles = ArticlesQuery.new.paginate(page: 1).entries
articles.total_pages  # => 5
articles.current_page # => 1

# Without count (large datasets)
articles = ArticlesQuery.new.paginate(page: 1, without_count: true).entries
articles.next_page    # => 2 (or nil if last page)

SQL template with pagination

-- app/queries/articles.sql
SELECT * FROM articles
ORDER BY published_on DESC
<%= paginate(page: page, per_page: per_page) %>

See Also:

Defined Under Namespace

Classes: PaginatedResult

Instance Method Summary collapse

Instance Method Details

#entriesObject



128
129
130
# File 'lib/app_query/paginatable.rb', line 128

def entries
  @_entries ||= build_paginated_result(super)
end

#paginate(page: 1, per_page: self.class.per_page, without_count: false) ⇒ Object



121
122
123
124
125
126
# File 'lib/app_query/paginatable.rb', line 121

def paginate(page: 1, per_page: self.class.per_page, without_count: false)
  @page = page
  @per_page = per_page
  @without_count = without_count
  self
end

#total_countObject



132
133
134
# File 'lib/app_query/paginatable.rb', line 132

def total_count
  @_total_count ||= unpaginated_query.count
end

#unpaginated_queryObject



136
137
138
139
140
# File 'lib/app_query/paginatable.rb', line 136

def unpaginated_query
  base_query
    .render(**render_vars.except(:page, :per_page))
    .with_binds(**bind_vars)
end