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.
41 42 43 44 45 46 47 |
# File 'lib/app_query/paginatable.rb', line 41 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
49 |
# File 'lib/app_query/paginatable.rb', line 49 def current_page = @page |
#first_page? ⇒ Boolean
55 |
# File 'lib/app_query/paginatable.rb', line 55 def first_page? = @page == 1 |
#last_page? ⇒ Boolean
74 75 76 77 78 79 80 |
# File 'lib/app_query/paginatable.rb', line 74 def last_page? if @total_count @page >= total_pages else !@has_next end end |
#limit_value ⇒ Object
51 |
# File 'lib/app_query/paginatable.rb', line 51 def limit_value = @per_page |
#next_page ⇒ Object
66 67 68 69 70 71 72 |
# File 'lib/app_query/paginatable.rb', line 66 def next_page if @total_count (@page < total_pages) ? @page + 1 : nil else @has_next ? @page + 1 : nil end end |
#out_of_range? ⇒ Boolean
82 83 84 |
# File 'lib/app_query/paginatable.rb', line 82 def out_of_range? empty? && @page > 1 end |
#prev_page ⇒ Object
53 |
# File 'lib/app_query/paginatable.rb', line 53 def prev_page = (@page > 1) ? @page - 1 : nil |
#total_count ⇒ Object
57 58 59 |
# File 'lib/app_query/paginatable.rb', line 57 def total_count @total_count || raise("total_count not available in without_count mode") end |
#total_pages ⇒ Object
61 62 63 64 |
# File 'lib/app_query/paginatable.rb', line 61 def total_pages return nil unless @total_count (@total_count.to_f / @per_page).ceil end |
#transform! ⇒ Object
86 87 88 89 |
# File 'lib/app_query/paginatable.rb', line 86 def transform! @records = @records.map { |r| yield(r) } self end |