Class: CEO::Paginator
- Inherits:
-
Object
- Object
- CEO::Paginator
- Includes:
- Enumerable
- Defined in:
- lib/ceo/paginator.rb
Overview
Class to Paginate over active record scopes
Ex:
Routes:
resources :users do
collection do
get 'page/:page', action: 'index', as: 'page', constraints: { page: /\d+/ }
end
en
Controller:
def index
@users = Paginator.new(
User.limit(100).order(name: :asc),
current_page: params.fetch(:page, 1).to_i,
per_page: 10
)
end
View with `paginate` helper:
.row
.large-12.columns
= paginate @users, :admin_users_path, intermediate_pages: 6
Simple View:
- if @order_paginator.has_previous?
= link_to 'previous page', orders_path(page: @order_paginator.previous_page)
- if @order_paginator.has_next?
= link_to 'next page', orders_path(page: @order_paginator.next_page)
Instance Attribute Summary collapse
-
#current_page ⇒ Object
Returns the value of attribute current_page.
-
#per_page ⇒ Object
Returns the value of attribute per_page.
-
#scope ⇒ Object
Returns the value of attribute scope.
Instance Method Summary collapse
- #each ⇒ Object
- #has_next? ⇒ Boolean
- #has_previous? ⇒ Boolean
-
#initialize(scope, options) ⇒ Paginator
constructor
Constructor.
- #intermediate_pages(max = 5) ⇒ Object
- #is_current_page?(page_number) ⇒ Boolean
- #multiple_pages? ⇒ Boolean
- #next_page ⇒ Object
- #previous_page ⇒ Object
- #total_pages ⇒ Object
- #total_results ⇒ Object
Constructor Details
#initialize(scope, options) ⇒ Paginator
Constructor
scope - The ActiveRecord scope to page over current_page - The Number of the current page. >= 1 per_page - The number of records per page
51 52 53 54 55 |
# File 'lib/ceo/paginator.rb', line 51 def initialize(scope, ) @current_page = .fetch :current_page @per_page = .fetch :per_page @scope = scope end |
Instance Attribute Details
#current_page ⇒ Object
Returns the value of attribute current_page.
43 44 45 |
# File 'lib/ceo/paginator.rb', line 43 def current_page @current_page end |
#per_page ⇒ Object
Returns the value of attribute per_page.
43 44 45 |
# File 'lib/ceo/paginator.rb', line 43 def per_page @per_page end |
#scope ⇒ Object
Returns the value of attribute scope.
43 44 45 |
# File 'lib/ceo/paginator.rb', line 43 def scope @scope end |
Instance Method Details
#each ⇒ Object
81 82 83 |
# File 'lib/ceo/paginator.rb', line 81 def each paged_scope.each{|record| yield record } end |
#has_next? ⇒ Boolean
61 62 63 |
# File 'lib/ceo/paginator.rb', line 61 def has_next? paged_scope.count == per_page end |
#has_previous? ⇒ Boolean
57 58 59 |
# File 'lib/ceo/paginator.rb', line 57 def has_previous? current_page.to_i > 1 end |
#intermediate_pages(max = 5) ⇒ Object
89 90 91 92 93 94 95 96 |
# File 'lib/ceo/paginator.rb', line 89 def intermediate_pages(max = 5) floor = current_page - (max.to_f / 2).floor ceil = current_page + (max.to_f / 2).ceil floor = 1 if floor <= 0 ceil = total_pages if ceil > total_pages floor.upto(ceil).collect{|page_num| page_num } end |
#is_current_page?(page_number) ⇒ Boolean
85 86 87 |
# File 'lib/ceo/paginator.rb', line 85 def is_current_page?(page_number) current_page.to_i == page_number.to_i end |
#multiple_pages? ⇒ Boolean
65 66 67 |
# File 'lib/ceo/paginator.rb', line 65 def multiple_pages? total_pages.to_i > 1 end |
#next_page ⇒ Object
69 70 71 |
# File 'lib/ceo/paginator.rb', line 69 def next_page current_page.to_i + 1 end |
#previous_page ⇒ Object
73 74 75 76 77 78 79 |
# File 'lib/ceo/paginator.rb', line 73 def previous_page if current_page.to_i - 1 > 0 current_page.to_i - 1 else current_page.to_i end end |
#total_pages ⇒ Object
102 103 104 |
# File 'lib/ceo/paginator.rb', line 102 def total_pages @total_pages ||= (total_results.to_f / per_page).ceil end |
#total_results ⇒ Object
98 99 100 |
# File 'lib/ceo/paginator.rb', line 98 def total_results @total_results ||= scope.count end |