Skip to content
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
wants to merge 4 commits into
base: master
Choose a base branch
from
Open

Conversation

ghost
Copy link

@ghost ghost commented Mar 27, 2017

Pivotal: https://www.pivotaltracker.com/story/show/140512959
Paginator Module displays X number of Model items per page and outputs a corresponding pagination panel.

@ghost ghost force-pushed the view_bets_and_pagination_commits branch from 7701211 to 405cde9 Compare March 27, 2017 23:16
@ghost ghost changed the title Users can see all the bets Create Paginator Module Mar 27, 2017
@ghost ghost mentioned this pull request Mar 27, 2017
@ghost ghost force-pushed the view_bets_and_pagination_commits branch 2 times, most recently from 5db032c to 369fa14 Compare March 29, 2017 18:49
<% blocks.each do |page|%>
<%= render 'shared/blocks', page: page,
page_item_css: page_item_css(page,
current_page) %>
Copy link
Member

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.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, just noticed!

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
Copy link
Member

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.

Copy link
Author

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.

Copy link
Member

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.

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)
Copy link
Member

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.

Copy link
Author

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.

Copy link
Member

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.

require 'rails_helper'

RSpec.describe Utilities::Paginator, type: :module do
let!(:dummy) { Class.new { extend Utilities::Paginator } }
Copy link
Member

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'?

@ghost ghost force-pushed the view_bets_and_pagination_commits branch 2 times, most recently from 40f6a74 to d3ffda1 Compare March 30, 2017 16:10
last_page: @last_page }
end

def page_item_css(page, current_page)
Copy link

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?

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

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.

private

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

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.

end

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

magic number


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

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
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

magic numbers

start = 1
finish = max_page_blocks - 1
[*(start..finish), '...']
end
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

magic numbers

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

magic number


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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

magic number

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

magic numbers

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

magic numbers

Copy link

@natyork natyork left a 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.

@ghost ghost force-pushed the view_bets_and_pagination_commits branch 5 times, most recently from ed870d5 to 23b4f2a Compare April 10, 2017 17:40
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.
@ghost ghost force-pushed the view_bets_and_pagination_commits branch from 23b4f2a to a9f2557 Compare April 11, 2017 18:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants