Module: LegacyMigrations::Squirrel::WillPagination

Defined in:
lib/legacy_migrations/squirrel/paginator.rb

Overview

The WillPagination module emulates the results that the will_paginate plugin returns from its #paginate methods. When it is used to extend a result set from Squirrel, it automtically pulls the result from the pagination that Squirrel performs. The methods added to that result set make it duck-equivalent to the WillPaginate::Collection class.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#current_pageObject

Returns the value of attribute current_page.



15
16
17
# File 'lib/legacy_migrations/squirrel/paginator.rb', line 15

def current_page
  @current_page
end

#per_pageObject

Returns the value of attribute per_page.



15
16
17
# File 'lib/legacy_migrations/squirrel/paginator.rb', line 15

def per_page
  @per_page
end

#total_entriesObject

Returns the value of attribute total_entries.



15
16
17
# File 'lib/legacy_migrations/squirrel/paginator.rb', line 15

def total_entries
  @total_entries
end

Class Method Details

.extended(base) ⇒ Object



9
10
11
12
13
# File 'lib/legacy_migrations/squirrel/paginator.rb', line 9

def self.extended base
  base.current_page = base.pages.current || 1
  base.per_page = base.pages.per_page
  base.total_entries = base.pages.total_results
end

Instance Method Details

#next_pageObject

Returns the current_page + 1, or nil if there are no more.



18
19
20
# File 'lib/legacy_migrations/squirrel/paginator.rb', line 18

def next_page
  current_page < page_count ? (current_page + 1) : nil
end

#offsetObject

Returns the offset of the current page that is suitable for inserting into SQL.



23
24
25
# File 'lib/legacy_migrations/squirrel/paginator.rb', line 23

def offset
  (current_page - 1) * per_page
end

#out_of_bounds?Boolean

Returns true if the current_page is greater than the total number of pages. Useful in case someone manually modifies the URL to put their own page number in.

Returns:

  • (Boolean)


29
30
31
# File 'lib/legacy_migrations/squirrel/paginator.rb', line 29

def out_of_bounds?
  current_page > page_count
end

#page_countObject Also known as: total_pages

The number of pages in the result set.



34
35
36
# File 'lib/legacy_migrations/squirrel/paginator.rb', line 34

def page_count
  pages.last
end

#previous_pageObject

Returns the current_page - 1, or nil if this is the first page.



41
42
43
# File 'lib/legacy_migrations/squirrel/paginator.rb', line 41

def previous_page
  current_page > 1 ? (current_page - 1) : nil
end