Class: RailsAdminNext::PaginatedCollection
- Inherits:
-
Object
- Object
- RailsAdminNext::PaginatedCollection
- Includes:
- Enumerable
- Defined in:
- lib/rails_admin_next/support/paginated_collection.rb
Overview
View-facing adapter over a single page of records.
Wraps either a GearedPagination::Page (ActiveRecord lists, via .from_recordset) or a manually built Array (the PaperTrail audit log, whose VersionProxy objects are not an ActiveRecord::Relation) and exposes only the pagination contract the index/history views and the shared pager partial consume. Counting is lazy so the count-free "limited" pager can render prev/next without triggering a COUNT query.
Instance Attribute Summary collapse
-
#current_page ⇒ Object
readonly
Returns the value of attribute current_page.
-
#per_page ⇒ Object
readonly
Returns the value of attribute per_page.
Class Method Summary collapse
Instance Method Summary collapse
- #each ⇒ Object
- #first_page? ⇒ Boolean
-
#full_page? ⇒ Boolean
This page holds a full slice, so (without a COUNT) more records may follow — drives the count-free "limited" pager's next link.
-
#initialize(records, current_page:, per_page:, total_count:, total_pages: nil) ⇒ PaginatedCollection
constructor
total_count / total_pages may each be an Integer, nil, or a callable returning one; callables are evaluated (and memoized) lazily on first read so the count-free pager never triggers a COUNT query.
- #last_page? ⇒ Boolean
- #next_page ⇒ Object
-
#total_count ⇒ Object
A grouped relation makes the underlying COUNT return a Hash (one entry per group); treat its length as the count so grouped queries paginate correctly instead of letting Hash arithmetic raise.
-
#total_pages ⇒ Object
Derived from total_count when not supplied, so a grouped relation never reaches GearedPagination's Hash-unaware page_count arithmetic.
Constructor Details
#initialize(records, current_page:, per_page:, total_count:, total_pages: nil) ⇒ PaginatedCollection
total_count / total_pages may each be an Integer, nil, or a callable returning one; callables are evaluated (and memoized) lazily on first read so the count-free pager never triggers a COUNT query. total_pages defaults to a value derived from total_count (see #total_pages).
29 30 31 32 33 34 35 |
# File 'lib/rails_admin_next/support/paginated_collection.rb', line 29 def initialize(records, current_page:, per_page:, total_count:, total_pages: nil) @records = records.to_a @current_page = current_page.to_i @per_page = per_page.to_i @total_count = total_count @total_pages = total_pages end |
Instance Attribute Details
#current_page ⇒ Object (readonly)
Returns the value of attribute current_page.
15 16 17 |
# File 'lib/rails_admin_next/support/paginated_collection.rb', line 15 def current_page @current_page end |
#per_page ⇒ Object (readonly)
Returns the value of attribute per_page.
15 16 17 |
# File 'lib/rails_admin_next/support/paginated_collection.rb', line 15 def per_page @per_page end |
Class Method Details
.from_recordset(recordset, page_number, per_page) ⇒ Object
17 18 19 20 21 22 23 |
# File 'lib/rails_admin_next/support/paginated_collection.rb', line 17 def self.from_recordset(recordset, page_number, per_page) page = recordset.page(page_number) new page.records, current_page: page.number, per_page:, total_count: -> { recordset.records_count } end |
Instance Method Details
#each ⇒ Object
37 38 39 |
# File 'lib/rails_admin_next/support/paginated_collection.rb', line 37 def each(&) @records.each(&) end |
#first_page? ⇒ Boolean
61 62 63 |
# File 'lib/rails_admin_next/support/paginated_collection.rb', line 61 def first_page? current_page <= 1 end |
#full_page? ⇒ Boolean
This page holds a full slice, so (without a COUNT) more records may follow — drives the count-free "limited" pager's next link.
75 76 77 |
# File 'lib/rails_admin_next/support/paginated_collection.rb', line 75 def full_page? size >= per_page end |
#last_page? ⇒ Boolean
65 66 67 |
# File 'lib/rails_admin_next/support/paginated_collection.rb', line 65 def last_page? current_page >= total_pages end |
#next_page ⇒ Object
69 70 71 |
# File 'lib/rails_admin_next/support/paginated_collection.rb', line 69 def next_page current_page + 1 unless last_page? end |
#total_count ⇒ Object
A grouped relation makes the underlying COUNT return a Hash (one entry per group); treat its length as the count so grouped queries paginate correctly instead of letting Hash arithmetic raise.
46 47 48 49 50 |
# File 'lib/rails_admin_next/support/paginated_collection.rb', line 46 def total_count @total_count = @total_count.call if @total_count.respond_to?(:call) @total_count = @total_count.size if @total_count.is_a?(Hash) @total_count end |
#total_pages ⇒ Object
Derived from total_count when not supplied, so a grouped relation never reaches GearedPagination's Hash-unaware page_count arithmetic.
54 55 56 57 58 59 |
# File 'lib/rails_admin_next/support/paginated_collection.rb', line 54 def total_pages @total_pages = @total_pages.call if @total_pages.respond_to?(:call) @total_pages = @total_pages.size if @total_pages.is_a?(Hash) @total_pages ||= [(total_count.to_f / per_page).ceil, 1].max @total_pages end |