-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Create Paginator Module #60
Open
ghost
wants to merge
4
commits into
master
Choose a base branch
from
view_bets_and_pagination_commits
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,3 +23,4 @@ Metrics/BlockLength: | |
Metrics/LineLength: | ||
IgnoredPatterns: | ||
- '\w*it.+do' | ||
- '\w*context.+do' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<%= | ||
content_tag :li, class: page_item_css_class do | ||
link_to page, bets_path(page: page), class: 'page-link' | ||
end | ||
%> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<div class="paginator"> | ||
<ol class="pagination"> | ||
<%= | ||
content_tag :li, class: previous_link_css_class do | ||
link_to bets_path(page: '1'), class: 'page-link' do | ||
content_tag :i, nil, class: 'fa fa-angle-double-left' | ||
end | ||
end | ||
%> | ||
<%= | ||
content_tag :li, class: previous_link_css_class do | ||
link_to bets_path(page: current_page - 1), class: 'page-link' do | ||
content_tag :i, nil, class: 'fa fa-angle-left' | ||
end | ||
end | ||
%> | ||
|
||
<% blocks.each do |page|%> | ||
<%= render 'shared/blocks', page: page, | ||
page_item_css_class: page_item_css_class(page, current_page) %> | ||
<% end %> | ||
|
||
<%= | ||
content_tag :li, class: next_link_css_class do | ||
link_to bets_path(page: current_page + 1), class: 'page-link' do | ||
content_tag :i, nil, class: 'fa fa-angle-right' | ||
end | ||
end | ||
%> | ||
<%= | ||
content_tag :li, class: next_link_css_class do | ||
link_to bets_path(page: last_page), class: 'page-link' do | ||
content_tag :i, nil, class: 'fa fa-angle-double-right' | ||
end | ||
end | ||
%> | ||
</ol> | ||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
module Utilities | ||
module Paginator | ||
ELLIPSES = '...'.freeze | ||
OFFSET = 1 | ||
FIRST_PAGE = 1 | ||
DIVIDER = 2 | ||
|
||
def items_to_display(model:, items_per_page: 5, current_page: 1) | ||
@items_per_page = items_per_page | ||
@current_page = current_page | ||
@number_of_items = model.count | ||
@last_page = (@number_of_items.to_f / @items_per_page).ceil | ||
model.offset(items_per_page * (@current_page - OFFSET)) | ||
.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_class: previous_link_css_class, | ||
next_link_css_class: next_link_css_class, | ||
last_page: @last_page } | ||
end | ||
|
||
def page_item_css_class(page, current_page) | ||
if page == current_page | ||
'page-item active' | ||
elsif page.to_s == ELLIPSES | ||
'page-item disabled' | ||
end | ||
end | ||
|
||
private | ||
|
||
def previous_link_css_class | ||
@current_page == FIRST_PAGE ? 'page-item disabled' : 'page-item' | ||
end | ||
|
||
def next_link_css_class | ||
@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 / DIVIDER) | ||
end | ||
|
||
def current_page_at_the_end?(current_page, number_of_pages, max_page_blocks) | ||
current_page > (number_of_pages - (max_page_blocks / DIVIDER)) | ||
end | ||
|
||
def general_pagination(current_page, max_page_blocks) | ||
if max_page_blocks == 1 | ||
[current_page] | ||
elsif max_page_blocks == 2 | ||
[current_page, ELLIPSES] | ||
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 = FIRST_PAGE | ||
finish = max_page_blocks - OFFSET | ||
[*(start..finish), ELLIPSES] | ||
end | ||
|
||
def end_pagination(max_page_blocks, number_of_pages) | ||
start = number_of_pages - max_page_blocks + OFFSET * 2 | ||
finish = number_of_pages | ||
[ELLIPSES, *(start..finish)] | ||
end | ||
|
||
def complete_pagination(last_page) | ||
[*FIRST_PAGE..last_page] | ||
end | ||
|
||
def odd_pages(current_page, max_page_blocks) | ||
start = (current_page - (max_page_blocks - OFFSET) / DIVIDER) + OFFSET | ||
finish = (current_page + (max_page_blocks - OFFSET) / DIVIDER) - OFFSET | ||
[ELLIPSES, *(start..finish), ELLIPSES] | ||
end | ||
|
||
def even_pages(current_page, max_page_blocks) | ||
start = (current_page - (max_page_blocks - OFFSET) / DIVIDER) + OFFSET | ||
finish = (current_page + (max_page_blocks / DIVIDER)) - OFFSET | ||
[ELLIPSES, *(start..finish), ELLIPSES] | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
magic numbers