Class: Pagination::Collection
- Inherits:
-
Object
- Object
- Pagination::Collection
- Includes:
- Enumerable
- Defined in:
- lib/pagination/collection.rb
Direct Known Subclasses
Constant Summary collapse
- Unimplemented =
Class.new(StandardError)
Instance Attribute Summary collapse
-
#page ⇒ Object
readonly
Returns the value of attribute page.
-
#per_page ⇒ Object
readonly
Returns the value of attribute per_page.
-
#total ⇒ Object
readonly
Returns the value of attribute total.
Instance Method Summary collapse
-
#current?(other_page) ⇒ Boolean
Mainly used as syntatic sugar instead of doing.
-
#displayed_pages(limit = 10) ⇒ Object
Provides dirt-simple logic for spitting out page numbers based on the current page.
- #each(&block) ⇒ Object
-
#initialize(collection, options = {}) ⇒ Collection
constructor
When subclassing ‘Pagination::Collection`, make sure you call super in order to use `page` and `per_page`.
-
#next_page ⇒ Object
When there’s a valid next page, returns that number.
-
#prev_page ⇒ Object
When there’s a valid previous page, returns that number.
-
#render? ⇒ Boolean
Mainly used in the presentation layer as a front-line check if we should even proceed with all the nitty-gritty details of rendering.
Constructor Details
#initialize(collection, options = {}) ⇒ Collection
When subclassing ‘Pagination::Collection`, make sure you call super in order to use `page` and `per_page`.
11 12 13 14 |
# File 'lib/pagination/collection.rb', line 11 def initialize(collection, = {}) @page = Integer([:page] || 1) @per_page = Integer([:per_page] || Pagination.per_page) end |
Instance Attribute Details
#page ⇒ Object (readonly)
Returns the value of attribute page.
7 8 9 |
# File 'lib/pagination/collection.rb', line 7 def page @page end |
#per_page ⇒ Object (readonly)
Returns the value of attribute per_page.
7 8 9 |
# File 'lib/pagination/collection.rb', line 7 def per_page @per_page end |
#total ⇒ Object (readonly)
Returns the value of attribute total.
7 8 9 |
# File 'lib/pagination/collection.rb', line 7 def total @total end |
Instance Method Details
#current?(other_page) ⇒ Boolean
Mainly used as syntatic sugar instead of doing
if items.page == params[:page]
for example, you will do
if items.current?(params[:page])
39 40 41 |
# File 'lib/pagination/collection.rb', line 39 def current?(other_page) page == other_page end |
#displayed_pages(limit = 10) ⇒ Object
Provides dirt-simple logic for spitting out page numbers based on the current page.
If we have 100 pages for example and we’re at page 50, this would simple return
[50, 51, 52, 53, 54, 55, 56, 57, 58, 59]
When we’re at page 1, it displays 1 to 10.
You can pass in a number to limit the total displayed pages. 1 2 3 4 5
55 56 57 58 59 60 |
# File 'lib/pagination/collection.rb', line 55 def displayed_pages(limit = 10) lower = [page, [pages.last - limit, 0].max + 1].min upper = [page + limit - 1, pages.last].min (lower..upper).to_a end |
#each(&block) ⇒ Object
71 72 73 |
# File 'lib/pagination/collection.rb', line 71 def each(&block) collection.each(&block) end |
#next_page ⇒ Object
When there’s a valid next page, returns that number
Otherwise returns nil
27 28 29 |
# File 'lib/pagination/collection.rb', line 27 def next_page page + 1 if pages.include?(page + 1) end |
#prev_page ⇒ Object
When there’s a valid previous page, returns that number
Otherwise returns nil
20 21 22 |
# File 'lib/pagination/collection.rb', line 20 def prev_page page - 1 if pages.include?(page - 1) end |
#render? ⇒ Boolean
Mainly used in the presentation layer as a front-line check if we should even proceed with all the nitty-gritty details of rendering.
This basically returns false when there’s only 1 page for the given collection, otherwise returns true.
67 68 69 |
# File 'lib/pagination/collection.rb', line 67 def render? pages.to_a.size > 1 end |