Class: Paginate::Paginators::Simple
- Inherits:
-
Object
- Object
- Paginate::Paginators::Simple
- Defined in:
- lib/paginators/simple.rb
Overview
This is for use by Array
and similar objects.
Direct Known Subclasses
Instance Method Summary collapse
-
#initialize(full_collection, options = {}) ⇒ Simple
constructor
full_collection is an
Array
(or any Object that responds tolength
and [](start_index, length), such asLazyArray
. #paginate will return the appropriate slice of that Object. options include: -page
: The desired page (may be negative) -limit
: Number of items per page. Other options are ignored. See README for more details. (TODO). -
#paginate ⇒ Object
Perform the pagination.
Constructor Details
#initialize(full_collection, options = {}) ⇒ Simple
full_collection is an Array
(or any Object that responds to length
and [](start_index, length), such as LazyArray
. #paginate will return the appropriate slice of that Object. options include:
-
page
: The desired page (may be negative) -
limit
: Number of items per page.
Other options are ignored. See README for more details. (TODO)
13 14 15 16 |
# File 'lib/paginators/simple.rb', line 13 def initialize(full_collection, = {}) @full_collection = full_collection @options = end |
Instance Method Details
#paginate ⇒ Object
Perform the pagination. Returns the records for the given page, with singleton methods:
-
current_page
: The number of the current page. -
pages
: The total number of pages of records.
See the documents for the module that your class extends for more details.
Descendents classes should not need to override this method, but rather + get_full_count+ and get_paginated_collection
private methods.
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/paginators/simple.rb', line 27 def paginate page = @options.delete(:page).to_i limit = @options[:limit] ? @options[:limit].to_i : Paginate.config[:default_limit] order = @options[:order] # Remove some options before calling +count+ that are not applicable. # order and limit are needed later and have been saved above. [:offset, :limit, :order].each do |key| @options.delete(key) end # Determine total number of pages and set offset option. pages = (get_full_count.to_f / limit).ceil page = (pages + 1 + page) if page < 0 # Negative page page = pages if page > pages # page should not be more than total pages page = 1 if page < 1 # Minimum is 1 even if 0 records. pages = 1 if pages < 1 # Minimum is 1 even if 0 records. @options[:offset] = ((page - 1) * limit) # Add limit and order back into options, from above. @options[:limit] = limit @options[:order] = order if order # Call +all+. collection = get_paginated_collection # Create +pages+ and +current_page+ methods for collection, for use by # pagination links. collection.instance_variable_set(:@pages, pages) collection.instance_variable_set(:@current_page, page) def collection.pages; @pages; end def collection.current_page; @current_page; end return collection end |