Skip to content

Commit

Permalink
Create Paginator module
Browse files Browse the repository at this point in the history
-Paginator module is responsible for calculating and rendering a
pagination bar based on the passed parameters.
-Added rspec for Paginator.
  • Loading branch information
LeonTenko committed Mar 29, 2017
1 parent a76f723 commit c1810ba
Show file tree
Hide file tree
Showing 2 changed files with 199 additions and 0 deletions.
106 changes: 106 additions & 0 deletions lib/utilities/paginator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
module Utilities
module Paginator
def items_to_display(model:, items_per_page: 5, current_page: 1)
@items_per_page = items_per_page
@current_page = current_page.to_i
@number_of_items = model.count
@last_page = (@number_of_items.to_f / @items_per_page).ceil
model.offset(items_per_page * (@current_page - 1))
.limit(items_per_page)
end

def paginator(max_page_blocks: 7)
render partial: 'shared/paginator',
locals: { blocks: pages_to_display(@current_page,
max_page_blocks,
@last_page),
current_page: @current_page,
previous_link_css: previous_link_css,
next_link_css: next_link_css,
last_page: @last_page }
end

def page_item_css(page, current_page)
if page == current_page
'page-item active'
elsif page.to_s == '...'
'page-item disabled'
end
end

private

def previous_link_css
@current_page == 1 ? 'page-item disabled' : 'page-item'
end

def next_link_css
@current_page == @last_page ? 'page-item disabled' : 'page-item'
end

def pages_to_display(current_page, max_page_blocks, number_of_pages)
if all_pages_fit?(number_of_pages, max_page_blocks)
complete_pagination(number_of_pages)
elsif current_page_at_the_beginning?(current_page, max_page_blocks)
beginning_pagination(max_page_blocks)
elsif current_page_at_the_end?(current_page, number_of_pages,
max_page_blocks)
end_pagination(max_page_blocks, number_of_pages)
else
general_pagination(current_page, max_page_blocks)
end
end

def all_pages_fit?(number_of_pages, max_page_blocks)
number_of_pages < max_page_blocks
end

def current_page_at_the_beginning?(current_page, max_page_blocks)
current_page <= (max_page_blocks / 2)
end

def current_page_at_the_end?(current_page, number_of_pages, max_page_blocks)
current_page > (number_of_pages - (max_page_blocks / 2))
end

def general_pagination(current_page, max_page_blocks)
if max_page_blocks == 1
[current_page]
elsif max_page_blocks == 2
[current_page, '...']
elsif max_page_blocks.odd?
odd_pages(current_page, max_page_blocks)
else
even_pages(current_page, max_page_blocks)
end
end

def beginning_pagination(max_page_blocks)
start = 1
finish = max_page_blocks - 1
[*(start..finish), '...']
end

def end_pagination(max_page_blocks, number_of_pages)
start = number_of_pages - max_page_blocks + 2
finish = number_of_pages
['...', *(start..finish)]
end

def complete_pagination(last_page)
[*1..last_page]
end

def odd_pages(current_page, max_page_blocks)
start = (current_page - (max_page_blocks - 1) / 2) + 1
finish = (current_page + (max_page_blocks - 1) / 2) - 1
['...', *(start..finish), '...']
end

def even_pages(current_page, max_page_blocks)
start = (current_page - (max_page_blocks - 1) / 2) + 1
finish = (current_page + (max_page_blocks / 2)) - 1
['...', *(start..finish), '...']
end
end
end
93 changes: 93 additions & 0 deletions spec/modules/paginator_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
require 'rails_helper'

RSpec.describe Utilities::Paginator, type: :module do
let!(:paginator) { Class.new { extend Utilities::Paginator } }

describe('#pages_to_display') do
subject(:pages_to_display) do
paginator.send(:pages_to_display,
current_page,
maximum_page_blocks,
number_of_pages)
end

context 'All pages fit within the pagination list' do
let(:maximum_page_blocks) { 7 }
let(:current_page) { 1 }
let(:number_of_pages) { 5 }
let(:expected_range_of_pages) { [1, 2, 3, 4, 5] }

it 'displays correct range of pages' do
expect(pages_to_display).to eq expected_range_of_pages
end
end

context 'Current page is in the beginning and there are more pages towards the end of the pagination list' do
let(:maximum_page_blocks) { 7 }
let(:current_page) { 3 }
let(:number_of_pages) { 11 }
let(:expected_range_of_pages) { [1, 2, 3, 4, 5, 6, '...'] }

it 'displays correct range of pages' do
expect(pages_to_display).to eq expected_range_of_pages
end
end

context 'Current page is at the end and there are more pages towards the beginning of the pagination list' do
let(:maximum_page_blocks) { 7 }
let(:current_page) { 9 }
let(:number_of_pages) { 11 }
let(:expected_range_of_pages) { ['...', 6, 7, 8, 9, 10, 11] }

it 'displays correct range of pages' do
expect(pages_to_display).to eq expected_range_of_pages
end
end

context 'Current page is in the middle and there are more pages towards the beginning and the end of the pagination list' do
context 'Maximum number of page blocks is 1' do
let(:maximum_page_blocks) { 1 }
let(:current_page) { 5 }
let(:number_of_pages) { 11 }
let(:expected_range_of_pages) { [5] }

it 'displays correct range of pages' do
expect(pages_to_display).to eq expected_range_of_pages
end
end

context 'Maximum number of page blocks is 2' do
let(:maximum_page_blocks) { 2 }
let(:current_page) { 6 }
let(:number_of_pages) { 11 }
let(:expected_range_of_pages) { [6, '...'] }

it 'displays correct range of pages' do
expect(pages_to_display).to eq expected_range_of_pages
end
end

context 'Maximum number of page blocks is odd' do
let(:maximum_page_blocks) { 7 }
let(:current_page) { 5 }
let(:number_of_pages) { 11 }
let(:expected_range_of_pages) { ['...', 3, 4, 5, 6, 7, '...'] }

it 'displays correct range of pages' do
expect(pages_to_display).to eq expected_range_of_pages
end
end

context 'Maximum number of page blocks is even' do
let(:maximum_page_blocks) { 6 }
let(:current_page) { 5 }
let(:number_of_pages) { 11 }
let(:expected_range_of_pages) { ['...', 4, 5, 6, 7, '...'] }

it 'displays correct range of pages' do
expect(pages_to_display).to eq expected_range_of_pages
end
end
end
end
end

0 comments on commit c1810ba

Please sign in to comment.