Class: ActionController::Pagination::Paginator
- Inherits:
-
Object
- Object
- ActionController::Pagination::Paginator
- Includes:
- Enumerable
- Defined in:
- vendor/plugins/classic_pagination/lib/pagination.rb
Overview
A class representing a paginator for an Active Record collection.
Defined Under Namespace
Instance Attribute Summary
-
- (Object) controller
readonly
Returns the value of attribute controller.
-
- (Object) item_count
readonly
Returns the value of attribute item_count.
-
- (Object) items_per_page
readonly
Returns the value of attribute items_per_page.
Instance Method Summary
-
- (Object) [](number)
Returns a new Page representing the page with the given index number.
-
- (Object) current_page
(also: #current)
Returns a Page object representing this paginator’s current page.
-
- (Object) current_page(page)
Sets the current page number of this paginator.
-
- (Object) each(&block)
Successively yields all the paginator’s pages to the given block.
-
- (Object) first_page
(also: #first)
Returns a new Page representing the first page in this paginator.
-
- (Boolean) has_page_number?(number)
Returns true if this paginator contains the page of index number.
-
- (Paginator) initialize(controller, item_count, items_per_page, current_page = 1)
constructor
Creates a new Paginator on the given controller for a set of items of size item_count and having items_per_page items per page.
-
- (Object) last_page
(also: #last)
Returns a new Page representing the last page in this paginator.
-
- (Object) page_count
(also: #length)
Returns the number of pages in this paginator.
Constructor Details
- (Paginator) initialize(controller, item_count, items_per_page, current_page = 1)
Creates a new Paginator on the given controller for a set of items of size item_count and having items_per_page items per page. Raises ArgumentError if items_per_page is out of bounds (i.e., less than or equal to zero). The page CGI parameter for links defaults to "page" and can be overridden with page_parameter.
218 219 220 221 222 223 224 225 226 227 228 |
# File 'vendor/plugins/classic_pagination/lib/pagination.rb', line 218 def initialize(controller, item_count, items_per_page, current_page=1) raise ArgumentError, 'must have at least one item per page' if items_per_page <= 0 @controller = controller @item_count = item_count || 0 @items_per_page = items_per_page @pages = {} self.current_page = current_page end |
Instance Attribute Details
- (Object) controller (readonly)
Returns the value of attribute controller
229 230 231 |
# File 'vendor/plugins/classic_pagination/lib/pagination.rb', line 229 def controller @controller end |
- (Object) item_count (readonly)
Returns the value of attribute item_count
229 230 231 |
# File 'vendor/plugins/classic_pagination/lib/pagination.rb', line 229 def item_count @item_count end |
- (Object) items_per_page (readonly)
Returns the value of attribute items_per_page
229 230 231 |
# File 'vendor/plugins/classic_pagination/lib/pagination.rb', line 229 def items_per_page @items_per_page end |
Instance Method Details
- (Object) [](number)
Returns a new Page representing the page with the given index number.
276 277 278 |
# File 'vendor/plugins/classic_pagination/lib/pagination.rb', line 276 def [](number) @pages[number] ||= Page.new(self, number) end |
- (Object) current_page Also known as: current
Returns a Page object representing this paginator’s current page.
244 245 246 |
# File 'vendor/plugins/classic_pagination/lib/pagination.rb', line 244 def current_page @current_page ||= self[@current_page_number] end |
- (Object) current_page=(page)
Sets the current page number of this paginator. If page is a Page object, its number attribute is used as the value; if the page does not belong to this Paginator, an ArgumentError is raised.
234 235 236 237 238 239 240 241 |
# File 'vendor/plugins/classic_pagination/lib/pagination.rb', line 234 def current_page=(page) if page.is_a? Page raise ArgumentError, 'Page/Paginator mismatch' unless page.paginator == self end page = page.to_i @current_page_number = has_page_number?(page) ? page : 1 end |
- (Object) each(&block)
Successively yields all the paginator’s pages to the given block.
281 282 283 284 285 |
# File 'vendor/plugins/classic_pagination/lib/pagination.rb', line 281 def each(&block) page_count.times do |n| yield self[n+1] end end |
- (Object) first_page Also known as: first
Returns a new Page representing the first page in this paginator.
250 251 252 |
# File 'vendor/plugins/classic_pagination/lib/pagination.rb', line 250 def first_page @first_page ||= self[1] end |
- (Boolean) has_page_number?(number)
Returns true if this paginator contains the page of index number.
270 271 272 |
# File 'vendor/plugins/classic_pagination/lib/pagination.rb', line 270 def has_page_number?(number) number >= 1 and number <= page_count end |
- (Object) last_page Also known as: last
Returns a new Page representing the last page in this paginator.
256 257 258 |
# File 'vendor/plugins/classic_pagination/lib/pagination.rb', line 256 def last_page @last_page ||= self[page_count] end |
- (Object) page_count Also known as: length
Returns the number of pages in this paginator.
262 263 264 265 |
# File 'vendor/plugins/classic_pagination/lib/pagination.rb', line 262 def page_count @page_count ||= @item_count.zero? ? 1 : (q,r=@item_count.divmod(@items_per_page); r==0? q : q+1) end |