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.
Instance Method Summary collapse
- #current_page ⇒ Object
- #first_page? ⇒ Boolean
-
#initialize(records, page:, per_page:, total_count: nil, has_next: nil) ⇒ PaginatedResult
constructor
A new instance of PaginatedResult.
- #last_page? ⇒ Boolean
- #limit_value ⇒ Object
- #next_page ⇒ Object
- #out_of_range? ⇒ Boolean
- #prev_page ⇒ Object
- #total_count ⇒ Object
- #total_pages ⇒ Object
- #transform! ⇒ Object
Constructor Details
#initialize(records, page:, per_page:, total_count: nil, has_next: nil) ⇒ PaginatedResult
Returns a new instance of PaginatedResult.
54 55 56 57 58 59 60 |
# File 'lib/app_query/paginatable.rb', line 54 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 ⇒ Object
62 |
# File 'lib/app_query/paginatable.rb', line 62 def current_page = @page |
#first_page? ⇒ Boolean
68 |
# File 'lib/app_query/paginatable.rb', line 68 def first_page? = @page == 1 |
#last_page? ⇒ Boolean
87 88 89 90 91 92 93 |
# File 'lib/app_query/paginatable.rb', line 87 def last_page? if @total_count @page >= total_pages else !@has_next end end |
#limit_value ⇒ Object
64 |
# File 'lib/app_query/paginatable.rb', line 64 def limit_value = @per_page |
#next_page ⇒ Object
79 80 81 82 83 84 85 |
# File 'lib/app_query/paginatable.rb', line 79 def next_page if @total_count (@page < total_pages) ? @page + 1 : nil else @has_next ? @page + 1 : nil end end |
#out_of_range? ⇒ Boolean
95 96 97 |
# File 'lib/app_query/paginatable.rb', line 95 def out_of_range? empty? && @page > 1 end |
#prev_page ⇒ Object
66 |
# File 'lib/app_query/paginatable.rb', line 66 def prev_page = (@page > 1) ? @page - 1 : nil |
#total_count ⇒ Object
70 71 72 |
# File 'lib/app_query/paginatable.rb', line 70 def total_count @total_count || raise("total_count not available in without_count mode") end |
#total_pages ⇒ Object
74 75 76 77 |
# File 'lib/app_query/paginatable.rb', line 74 def total_pages return nil unless @total_count (@total_count.to_f / @per_page).ceil end |
#transform! ⇒ Object
99 100 101 102 |
# File 'lib/app_query/paginatable.rb', line 99 def transform! @records = @records.map { |r| yield(r) } self end |