Class: PaginationHelper::Paginator

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
app/helpers/pagination_helper.rb

Overview

A class representing a paginator for an Active Record collection.

Defined Under Namespace

Classes: Page, Window

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(controller, item_count, items_per_page, current_page = 1, page_parameter = 'page') ⇒ Paginator

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.

Raises:

  • (ArgumentError)


260
261
262
263
264
265
266
267
268
269
270
271
# File 'app/helpers/pagination_helper.rb', line 260

def initialize(controller, item_count, items_per_page, current_page=1,
               page_parameter='page')
  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
  @page_parameter = page_parameter

  self.current_page = current_page
end

Instance Attribute Details

#controllerObject (readonly)

Returns the value of attribute controller.



272
273
274
# File 'app/helpers/pagination_helper.rb', line 272

def controller
  @controller
end

#item_countObject (readonly)

Returns the value of attribute item_count.



272
273
274
# File 'app/helpers/pagination_helper.rb', line 272

def item_count
  @item_count
end

#items_per_pageObject (readonly)

Returns the value of attribute items_per_page.



272
273
274
# File 'app/helpers/pagination_helper.rb', line 272

def items_per_page
  @items_per_page
end

#page_parameterObject (readonly)

Returns the value of attribute page_parameter.



272
273
274
# File 'app/helpers/pagination_helper.rb', line 272

def page_parameter
  @page_parameter
end

Instance Method Details

#[](number) ⇒ Object

Returns a new Page representing the page with the given index number.



318
319
320
# File 'app/helpers/pagination_helper.rb', line 318

def [](number)
  Page.new(self, number)
end

#basic_html(view, window_size = 2, link_to_current_page = false, params = {}) ⇒ Object

Creates a basic HTML link bar for the given view. The first and last pages of the paginator are always shown, along with window_size pages around the current page. By default, the current page is displayed, but not linked; to change this behavior, pass true to the link_to_current_page argument. Specify additional link_to parameters with params.



335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
# File 'app/helpers/pagination_helper.rb', line 335

def basic_html(view, window_size=2, link_to_current_page=false, params={})
  window_pages = current.window(window_size).pages
  return if window_pages.length <= 1 unless link_to_current_page
  
  html = ''
  unless window_pages[0].first?
    html << view.link_to(first.number, first.to_link(params))
    html << ' ... ' if window_pages[0].number - first.number > 1
    html << ' '
  end

  window_pages.each do |page|
    if current == page and not link_to_current_page
      html << page.number.to_s
    else
      html << view.link_to(page.number, page.to_link(params))
    end
    html << ' '
  end

  unless window_pages.last.last?
    html << ' ... ' if last.number - window_pages[-1].number > 1
    html << view.link_to(last.number, last.to_link(params))
  end
  
  html
end

#current_pageObject Also known as: current

Returns a Page object representing this paginator’s current page.



287
288
289
# File 'app/helpers/pagination_helper.rb', line 287

def current_page
  self[@current_page]
end

#current_page=(page) ⇒ Object

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.



277
278
279
280
281
282
283
284
# File 'app/helpers/pagination_helper.rb', line 277

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 = has_page_number?(page) ? page : 1
end

#each(&block) ⇒ Object

Successively yields all the paginator’s pages to the given block.



323
324
325
326
327
# File 'app/helpers/pagination_helper.rb', line 323

def each(&block)
  page_count.times do |n|
    yield self[n+1]
  end
end

#first_pageObject Also known as: first

Returns a new Page representing the first page in this paginator.



293
294
295
# File 'app/helpers/pagination_helper.rb', line 293

def first_page
  self[1]
end

#has_page_number?(number) ⇒ Boolean

Returns true if this paginator contains the page of index number.

Returns:

  • (Boolean)


312
313
314
315
# File 'app/helpers/pagination_helper.rb', line 312

def has_page_number?(number)
  return false unless number.is_a? Fixnum
  number >= 1 and number <= page_count
end

#last_pageObject Also known as: last

Returns a new Page representing the last page in this paginator.



299
300
301
# File 'app/helpers/pagination_helper.rb', line 299

def last_page
  self[page_count] 
end

#page_countObject Also known as: length

Returns the number of pages in this paginator.



305
306
307
308
# File 'app/helpers/pagination_helper.rb', line 305

def page_count
  return 1 if @item_count.zero?
  (@item_count / @items_per_page.to_f).ceil
end