Class: PaginationHelper::Paginator::Page
- Inherits:
-
Object
- Object
- PaginationHelper::Paginator::Page
- Includes:
- Comparable
- Defined in:
- app/helpers/pagination_helper.rb
Overview
A class representing a single page in a paginator.
Instance Attribute Summary collapse
-
#number ⇒ Object
(also: #to_i)
readonly
Returns the value of attribute number.
-
#paginator ⇒ Object
readonly
Returns the value of attribute paginator.
Instance Method Summary collapse
-
#<=>(page) ⇒ Object
Compares two Page objects and returns -1 if the left-hand page comes before the right-hand page, 0 if the pages are equal, and 1 if the left-hand page comes after the right-hand page.
-
#==(page) ⇒ Object
Compares two Page objects and returns true when they represent the same page (i.e., their paginators are the same and they have the same page number).
-
#first? ⇒ Boolean
Returns true if this page is the first page in the paginator.
-
#first_item ⇒ Object
Returns the number of the first item displayed.
-
#initialize(paginator, number) ⇒ Page
constructor
Creates a new Page for the given
paginator
with the indexnumber
. -
#last? ⇒ Boolean
Returns true if this page is the last page in the paginator.
-
#last_item ⇒ Object
Returns the number of the last item displayed.
-
#next ⇒ Object
Returns a new Page object representing the page just after this page, or nil if this is the last page.
-
#offset ⇒ Object
Returns the item offset for the first item in this page.
-
#previous ⇒ Object
Returns a new Page object representing the page just before this page, or nil if this is the first page.
-
#to_link(params = {}) ⇒ Object
Returns a hash appropriate for using with link_to or url_for.
-
#to_sql ⇒ Object
Returns the SQL “LIMIT …
-
#window(padding = 2) ⇒ Object
Returns a new Window object for this page with the specified
padding
.
Constructor Details
#initialize(paginator, number) ⇒ Page
Creates a new Page for the given paginator
with the index number
.
If number
is not in the range of valid page numbers or is not a number at all, it defaults to 1.
370 371 372 373 374 |
# File 'app/helpers/pagination_helper.rb', line 370 def initialize(paginator, number) @paginator = paginator @number = number.to_i @number = 1 unless @paginator.has_page_number? @number end |
Instance Attribute Details
#number ⇒ Object (readonly) Also known as: to_i
Returns the value of attribute number.
375 376 377 |
# File 'app/helpers/pagination_helper.rb', line 375 def number @number end |
#paginator ⇒ Object (readonly)
Returns the value of attribute paginator.
375 376 377 |
# File 'app/helpers/pagination_helper.rb', line 375 def paginator @paginator end |
Instance Method Details
#<=>(page) ⇒ Object
Compares two Page objects and returns -1 if the left-hand page comes before the right-hand page, 0 if the pages are equal, and 1 if the left-hand page comes after the right-hand page. Raises ArgumentError if the pages do not belong to the same Paginator object.
390 391 392 393 |
# File 'app/helpers/pagination_helper.rb', line 390 def <=>(page) raise ArgumentError unless @paginator == page.paginator @number <=> page.number end |
#==(page) ⇒ Object
Compares two Page objects and returns true when they represent the same page (i.e., their paginators are the same and they have the same page number).
381 382 383 384 |
# File 'app/helpers/pagination_helper.rb', line 381 def ==(page) @paginator == page.paginator and @number == page.number end |
#first? ⇒ Boolean
Returns true if this page is the first page in the paginator.
411 412 413 |
# File 'app/helpers/pagination_helper.rb', line 411 def first? self == @paginator.first end |
#first_item ⇒ Object
Returns the number of the first item displayed.
401 402 403 |
# File 'app/helpers/pagination_helper.rb', line 401 def first_item offset + 1 end |
#last? ⇒ Boolean
Returns true if this page is the last page in the paginator.
416 417 418 |
# File 'app/helpers/pagination_helper.rb', line 416 def last? self == @paginator.last end |
#last_item ⇒ Object
Returns the number of the last item displayed.
406 407 408 |
# File 'app/helpers/pagination_helper.rb', line 406 def last_item [@paginator.items_per_page * @number, @paginator.item_count].min end |
#next ⇒ Object
Returns a new Page object representing the page just after this page, or nil if this is the last page.
428 429 430 |
# File 'app/helpers/pagination_helper.rb', line 428 def next if last? then nil else Page.new(@paginator, @number + 1) end end |
#offset ⇒ Object
Returns the item offset for the first item in this page.
396 397 398 |
# File 'app/helpers/pagination_helper.rb', line 396 def offset @paginator.items_per_page * (@number - 1) end |
#previous ⇒ Object
Returns a new Page object representing the page just before this page, or nil if this is the first page.
422 423 424 |
# File 'app/helpers/pagination_helper.rb', line 422 def previous if first? then nil else Page.new(@paginator, @number - 1) end end |
#to_link(params = {}) ⇒ Object
Returns a hash appropriate for using with link_to or url_for. Takes an optional params
hash for specifying additional link parameters.
440 441 442 |
# File 'app/helpers/pagination_helper.rb', line 440 def to_link(params={}) {:params => {@paginator.page_parameter => @number.to_s}.merge(params)} end |
#to_sql ⇒ Object
Returns the SQL “LIMIT … OFFSET” clause for this page.
445 446 447 |
# File 'app/helpers/pagination_helper.rb', line 445 def to_sql ['? OFFSET ?', @paginator.items_per_page, offset] end |