Class: AppQuery::Paginatable::PaginatedResult
- Inherits:
-
Object
- Object
- AppQuery::Paginatable::PaginatedResult
- Includes:
- Enumerable
- Defined in:
- lib/app_query/paginatable.rb
Overview
Kaminari-compatible wrapper for paginated results.
Wraps an array of records with pagination metadata, providing a consistent interface for both counted and uncounted pagination modes.
Includes Enumerable, so all standard iteration methods work directly.
Instance Method Summary collapse
-
#current_page ⇒ Integer
The current page number.
-
#first_page? ⇒ Boolean
True if this is the first page.
-
#initialize(records, page:, per_page:, total_count: nil, has_next: nil) ⇒ PaginatedResult
constructor
private
A new instance of PaginatedResult.
-
#last_page? ⇒ Boolean
True if this is the last page.
-
#limit_value ⇒ Integer
The number of records per page.
-
#next_page ⇒ Integer?
The next page number, or nil if on last page.
-
#out_of_range? ⇒ Boolean
True if the requested page is beyond available data.
-
#prev_page ⇒ Integer?
The previous page number, or nil if on first page.
-
#total_count ⇒ Integer
The total number of records across all pages.
-
#total_pages ⇒ Integer?
The total number of pages, or nil in +without_count+ mode.
-
#transform! {|record| ... } ⇒ self
Transforms each record in place using the given block.
Constructor Details
#initialize(records, page:, per_page:, total_count: nil, has_next: nil) ⇒ PaginatedResult
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns a new instance of PaginatedResult.
66 67 68 69 70 71 72 |
# File 'lib/app_query/paginatable.rb', line 66 def initialize(records, page:, per_page:, total_count: nil, has_next: nil) @records = records @page = page @per_page = per_page @total_count = total_count @has_next = has_next end |
Instance Method Details
#current_page ⇒ Integer
Returns the current page number.
75 |
# File 'lib/app_query/paginatable.rb', line 75 def current_page = @page |
#first_page? ⇒ Boolean
Returns true if this is the first page.
84 |
# File 'lib/app_query/paginatable.rb', line 84 def first_page? = @page == 1 |
#last_page? ⇒ Boolean
Returns true if this is the last page.
108 109 110 111 112 113 114 |
# File 'lib/app_query/paginatable.rb', line 108 def last_page? if @total_count @page >= total_pages else !@has_next end end |
#limit_value ⇒ Integer
Returns the number of records per page.
78 |
# File 'lib/app_query/paginatable.rb', line 78 def limit_value = @per_page |
#next_page ⇒ Integer?
Returns the next page number, or nil if on last page.
99 100 101 102 103 104 105 |
# File 'lib/app_query/paginatable.rb', line 99 def next_page if @total_count (@page < total_pages) ? @page + 1 : nil else @has_next ? @page + 1 : nil end end |
#out_of_range? ⇒ Boolean
Returns true if the requested page is beyond available data.
117 118 119 |
# File 'lib/app_query/paginatable.rb', line 117 def out_of_range? empty? && @page > 1 end |
#prev_page ⇒ Integer?
Returns the previous page number, or nil if on first page.
81 |
# File 'lib/app_query/paginatable.rb', line 81 def prev_page = (@page > 1) ? @page - 1 : nil |
#total_count ⇒ Integer
Returns the total number of records across all pages.
88 89 90 |
# File 'lib/app_query/paginatable.rb', line 88 def total_count @total_count || raise("total_count not available in without_count mode") end |
#total_pages ⇒ Integer?
Returns the total number of pages, or nil in +without_count+ mode.
93 94 95 96 |
# File 'lib/app_query/paginatable.rb', line 93 def total_pages return nil unless @total_count (@total_count.to_f / @per_page).ceil end |
#transform! {|record| ... } ⇒ self
Transforms each record in place using the given block.
130 131 132 133 |
# File 'lib/app_query/paginatable.rb', line 130 def transform! @records = @records.map { |r| yield(r) } self end |