Class: ActionController::Pagination::Paginator::Page

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
vendor/plugins/classic_pagination/lib/pagination.rb

Overview

A class representing a single page in a paginator.

Instance Attribute Summary

Instance Method Summary

Constructor Details

- (Page) initialize(paginator, number)

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.



294
295
296
297
298
# File 'vendor/plugins/classic_pagination/lib/pagination.rb', line 294

def initialize(paginator, number)
  @paginator = paginator
  @number = number.to_i
  @number = 1 unless @paginator.has_page_number? @number
end

Instance Attribute Details

- (Object) number (readonly) Also known as: to_i

Returns the value of attribute number



299
300
301
# File 'vendor/plugins/classic_pagination/lib/pagination.rb', line 299

def number
  @number
end

- (Object) paginator (readonly)

Returns the value of attribute paginator



299
300
301
# File 'vendor/plugins/classic_pagination/lib/pagination.rb', line 299

def paginator
  @paginator
end

Instance Method Details

- (Object) <=>(page)

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.

Raises:

  • (ArgumentError)


315
316
317
318
# File 'vendor/plugins/classic_pagination/lib/pagination.rb', line 315

def <=>(page)
  raise ArgumentError unless @paginator == page.paginator
  @number <=> page.number
end

- (Object) ==(page)

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).



305
306
307
308
309
# File 'vendor/plugins/classic_pagination/lib/pagination.rb', line 305

def ==(page)
  return false if page.nil?
  @paginator == page.paginator and 
    @number == page.number
end

- (Boolean) first?

Returns true if this page is the first page in the paginator.

Returns:

  • (Boolean)


336
337
338
# File 'vendor/plugins/classic_pagination/lib/pagination.rb', line 336

def first?
  self == @paginator.first
end

- (Object) first_item

Returns the number of the first item displayed.



326
327
328
# File 'vendor/plugins/classic_pagination/lib/pagination.rb', line 326

def first_item
  offset + 1
end

- (Boolean) last?

Returns true if this page is the last page in the paginator.

Returns:

  • (Boolean)


341
342
343
# File 'vendor/plugins/classic_pagination/lib/pagination.rb', line 341

def last?
  self == @paginator.last
end

- (Object) last_item

Returns the number of the last item displayed.



331
332
333
# File 'vendor/plugins/classic_pagination/lib/pagination.rb', line 331

def last_item
  [@paginator.items_per_page * @number, @paginator.item_count].min
end

- (Object) next

Returns a new Page object representing the page just after this page, or nil if this is the last page.



353
354
355
# File 'vendor/plugins/classic_pagination/lib/pagination.rb', line 353

def next
  if last? then nil else @paginator[@number + 1] end
end

- (Object) offset

Returns the item offset for the first item in this page.



321
322
323
# File 'vendor/plugins/classic_pagination/lib/pagination.rb', line 321

def offset
  @paginator.items_per_page * (@number - 1)
end

- (Object) previous

Returns a new Page object representing the page just before this page, or nil if this is the first page.



347
348
349
# File 'vendor/plugins/classic_pagination/lib/pagination.rb', line 347

def previous
  if first? then nil else @paginator[@number - 1] end
end

- (Object) to_param

:nodoc:



368
369
370
# File 'vendor/plugins/classic_pagination/lib/pagination.rb', line 368

def to_param 
  @number.to_s
end

- (Object) to_sql

Returns the limit/offset array for this page.



364
365
366
# File 'vendor/plugins/classic_pagination/lib/pagination.rb', line 364

def to_sql
  [@paginator.items_per_page, offset]
end

- (Object) window(padding = 2)

Returns a new Window object for this page with the specified padding.



359
360
361
# File 'vendor/plugins/classic_pagination/lib/pagination.rb', line 359

def window(padding=2)
  Window.new(self, padding)
end