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

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/action_controller/pagination.rb

Overview

A class representing a single page in a paginator.

Instance Attribute Summary collapse

Instance Method Summary collapse

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.



291
292
293
294
295
# File 'lib/action_controller/pagination.rb', line 291

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

Instance Attribute Details

#numberObject (readonly) Also known as: to_i

Returns the value of attribute number.



296
297
298
# File 'lib/action_controller/pagination.rb', line 296

def number
  @number
end

#paginatorObject (readonly)

Returns the value of attribute paginator.



296
297
298
# File 'lib/action_controller/pagination.rb', line 296

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.

Raises:

  • (ArgumentError)


312
313
314
315
# File 'lib/action_controller/pagination.rb', line 312

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



302
303
304
305
306
# File 'lib/action_controller/pagination.rb', line 302

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

#first?Boolean

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

Returns:

  • (Boolean)


333
334
335
# File 'lib/action_controller/pagination.rb', line 333

def first?
  self == @paginator.first
end

#first_itemObject

Returns the number of the first item displayed.



323
324
325
# File 'lib/action_controller/pagination.rb', line 323

def first_item
  offset + 1
end

#last?Boolean

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

Returns:

  • (Boolean)


338
339
340
# File 'lib/action_controller/pagination.rb', line 338

def last?
  self == @paginator.last
end

#last_itemObject

Returns the number of the last item displayed.



328
329
330
# File 'lib/action_controller/pagination.rb', line 328

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

#nextObject

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



350
351
352
# File 'lib/action_controller/pagination.rb', line 350

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

#offsetObject

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



318
319
320
# File 'lib/action_controller/pagination.rb', line 318

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

#previousObject

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



344
345
346
# File 'lib/action_controller/pagination.rb', line 344

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

#to_paramObject

:nodoc:



365
366
367
# File 'lib/action_controller/pagination.rb', line 365

def to_param #:nodoc:
  @number.to_s
end

#to_sqlObject

Returns the limit/offset array for this page.



361
362
363
# File 'lib/action_controller/pagination.rb', line 361

def to_sql
  [@paginator.items_per_page, offset]
end

#window(padding = 2) ⇒ Object

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



356
357
358
# File 'lib/action_controller/pagination.rb', line 356

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