-
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
base: master
Are you sure you want to change the base?
Conversation
7701211
to
405cde9
Compare
5db032c
to
369fa14
Compare
app/views/shared/_paginator.html.erb
Outdated
<% blocks.each do |page|%> | ||
<%= render 'shared/blocks', page: page, | ||
page_item_css: page_item_css(page, | ||
current_page) %> |
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.
This is kind of weird code style. You've decided to put 2 arguments on the first line and one on the second.
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.
Thanks, just noticed!
lib/utilities/paginator.rb
Outdated
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 |
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.
Why are we calling to_i
on current page? I would assume we are passing in a integer to this method and not a float or string.
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.
Ah yeah, it's because in my BetsController I pass the parameter current_page: params[:page] as the argument. So, in this case it's either I apply it here just to be safe and make sure it's always an int or I can pass it there.
Please let me know what works best.
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.
Yeah I'd like to see that done in the controller. To have current page referring to a string or integer depending on where it is being used is odd. Also is there an instance we need it to be a string? Usually the string "1" is pretty useless.
lib/utilities/paginator.rb
Outdated
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) |
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.
One argument on the first line and two on the second.
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.
I remember why I did it now - Rubocop won't let me have 11 lines of code, so. I shuffled it around - 2 lines on top, one at the bottom, hope it looks better.
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.
Maybe just one argument per line and then we can be consistent.
spec/modules/paginator_spec.rb
Outdated
require 'rails_helper' | ||
|
||
RSpec.describe Utilities::Paginator, type: :module do | ||
let!(:dummy) { Class.new { extend Utilities::Paginator } } |
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.
Perhaps this could be called 'paginator'?
40f6a74
to
d3ffda1
Compare
lib/utilities/paginator.rb
Outdated
last_page: @last_page } | ||
end | ||
|
||
def page_item_css(page, current_page) |
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.
Perhaps a more semantic name for this method would be something like page_item_class_name
?
lib/utilities/paginator.rb
Outdated
def page_item_css(page, current_page) | ||
if page == current_page | ||
'page-item active' | ||
elsif page.to_s == '...' |
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.
You use the ellipses (...
) in several places - it might be worth making it a constant, in case you ever want to change it in the future. This will also allow you to give it a more descriptive/semantic name so that you don't have 'Magic Numbers' (or text in this case) in your code.
lib/utilities/paginator.rb
Outdated
private | ||
|
||
def previous_link_css | ||
@current_page == 1 ? 'page-item disabled' : 'page-item' |
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 number - this could be made a constant such as FIRST_PAGE
instead of 1.
lib/utilities/paginator.rb
Outdated
end | ||
|
||
def current_page_at_the_beginning?(current_page, max_page_blocks) | ||
current_page <= (max_page_blocks / 2) |
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 number
lib/utilities/paginator.rb
Outdated
|
||
def current_page_at_the_end?(current_page, number_of_pages, max_page_blocks) | ||
current_page > (number_of_pages - (max_page_blocks / 2)) | ||
end |
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 number
else | ||
even_pages(current_page, max_page_blocks) | ||
end | ||
end |
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
lib/utilities/paginator.rb
Outdated
start = 1 | ||
finish = max_page_blocks - 1 | ||
[*(start..finish), '...'] | ||
end |
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
lib/utilities/paginator.rb
Outdated
start = number_of_pages - max_page_blocks + 2 | ||
finish = number_of_pages | ||
['...', *(start..finish)] | ||
end |
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 number
lib/utilities/paginator.rb
Outdated
|
||
def complete_pagination(last_page) | ||
[*1..last_page] | ||
end |
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 number
lib/utilities/paginator.rb
Outdated
start = (current_page - (max_page_blocks - 1) / 2) + 1 | ||
finish = (current_page + (max_page_blocks - 1) / 2) - 1 | ||
['...', *(start..finish), '...'] | ||
end |
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
lib/utilities/paginator.rb
Outdated
start = (current_page - (max_page_blocks - 1) / 2) + 1 | ||
finish = (current_page + (max_page_blocks / 2)) - 1 | ||
['...', *(start..finish), '...'] | ||
end |
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
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.
Overall looks pretty good, but I've flagged areas where you are using so-called 'magic numbers'. It would make your code much more readable and maintainable to use semantically-named constants rather than raw numbers/text that require interpretation throughout your code.
ed870d5
to
23b4f2a
Compare
Allow long strings for "context"
Adding pagination module that will have to be included in autoload_paths.
-Paginator module is responsible for calculating and rendering a pagination bar based on the passed parameters. -Added rspec for Paginator.
-Add _blocks.html.erb view that renders page blocks; -Add _paginator.html.erb view that renders the pagination panel for the current page of a given Model.
23b4f2a
to
a9f2557
Compare
Pivotal: https://www.pivotaltracker.com/story/show/140512959
Paginator Module displays X number of Model items per page and outputs a corresponding pagination panel.