Class: Commontator::Collection

Inherits:
WillPaginate::Collection
  • Object
show all
Defined in:
app/models/commontator/collection.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(array, count, root_per_page, per_page, page = 1) ⇒ Collection

Returns a new instance of Collection.



9
10
11
12
13
14
15
16
17
# File 'app/models/commontator/collection.rb', line 9

def initialize(array, count, root_per_page, per_page, page = 1)
  self.total_entries = count
  @root_per_page = root_per_page
  @per_page = per_page
  @current_page = page_zero? ? 0 : WillPaginate::PageNumber(page)
  @first_call = true

  replace(array)
end

Instance Attribute Details

#root_per_pageObject (readonly)

Returns the value of attribute root_per_page.



2
3
4
# File 'app/models/commontator/collection.rb', line 2

def root_per_page
  @root_per_page
end

Instance Method Details

#page_zero?Boolean

This method determines if we are in a shorter version of the first page, which we call page 0

Returns:

  • (Boolean)


5
6
7
# File 'app/models/commontator/collection.rb', line 5

def page_zero?
  total_entries > @per_page && @per_page < @root_per_page
end

#total_pagesObject

We return 2 total_pages under certain conditions to trick will_paginate into rendering the pagination controls when it otherwise wouldn’t



21
22
23
24
25
# File 'app/models/commontator/collection.rb', line 21

def total_pages
  min_total_pages = page_zero? && @first_call ? 2 : 1
  @first_call = false
  [ (total_entries.to_f/@root_per_page).ceil, min_total_pages ].max
end