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

Stop displaying first page link on second page pagination and last page link on penultimate page pagination #1005

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

* Add `#url_to_next_page` and `#url_to_prev_page` helper methods: [38e95a2](https://github.com/kaminari/kaminari/commit/38e95a262a210548c4f892aaa69d09ca8ecdce7f)
* Extract url helper methods and pack them in `Kaminari::Helpers::UrlHelper`: [ff38bee](https://github.com/kaminari/kaminari/commit/ff38bee54b5be1a948f1118c0bfd829a72a1a502)
* Stop displaying first page link on second page pagination and last page link on penultimate page pagination: [@tiagotex]

### Bug Fixes:

Expand Down
2 changes: 1 addition & 1 deletion kaminari-core/app/views/kaminari/_first_page.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@
remote: data-remote
-%>
<span class="first">
<%= link_to_unless current_page.first?, t('views.pagination.first').html_safe, url, remote: remote %>
<%= link_to_unless current_page.first? || current_page.second?, t('views.pagination.first').html_safe, url, remote: remote %>
</span>
2 changes: 1 addition & 1 deletion kaminari-core/app/views/kaminari/_first_page.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@
-# per_page: number of items to fetch per page
-# remote: data-remote
%span.first
= link_to_unless current_page.first?, t('views.pagination.first').html_safe, url, remote: remote
= link_to_unless current_page.first? || current_page.second?, t('views.pagination.first').html_safe, url, remote: remote
2 changes: 1 addition & 1 deletion kaminari-core/app/views/kaminari/_first_page.html.slim
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@
per_page : number of items to fetch per page
remote : data-remote
span.first
== link_to_unless current_page.first?, t('views.pagination.first').html_safe, url, remote: remote
== link_to_unless current_page.first? || current_page.second?, t('views.pagination.first').html_safe, url, remote: remote
'
2 changes: 1 addition & 1 deletion kaminari-core/app/views/kaminari/_last_page.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@
remote: data-remote
-%>
<span class="last">
<%= link_to_unless current_page.last?, t('views.pagination.last').html_safe, url, remote: remote %>
<%= link_to_unless current_page.last? || current_page.penultimate?, t('views.pagination.last').html_safe, url, remote: remote %>
</span>
2 changes: 1 addition & 1 deletion kaminari-core/app/views/kaminari/_last_page.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@
-# per_page: number of items to fetch per page
-# remote: data-remote
%span.last
= link_to_unless current_page.last?, t('views.pagination.last').html_safe, url, remote: remote
= link_to_unless current_page.last? || current_page.penultimate?, t('views.pagination.last').html_safe, url, remote: remote
2 changes: 1 addition & 1 deletion kaminari-core/app/views/kaminari/_last_page.html.slim
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@
per_page : number of items to fetch per page
remote : data-remote
span.last
== link_to_unless current_page.last?, t('views.pagination.last').html_safe, url, remote: remote
== link_to_unless current_page.last? || current_page.penultimate?, t('views.pagination.last').html_safe, url, remote: remote
'
4 changes: 2 additions & 2 deletions kaminari-core/app/views/kaminari/_paginator.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
-%>
<%= paginator.render do -%>
<nav class="pagination" role="navigation" aria-label="pager">
<%= first_page_tag unless current_page.first? %>
<%= first_page_tag unless current_page.first? || current_page.second? %>
<%= prev_page_tag unless current_page.first? %>
<% each_page do |page| -%>
<% if page.display_tag? -%>
Expand All @@ -19,7 +19,7 @@
<% end -%>
<% unless current_page.out_of_range? %>
<%= next_page_tag unless current_page.last? %>
<%= last_page_tag unless current_page.last? %>
<%= last_page_tag unless current_page.last? || current_page.penultimate? %>
<% end %>
</nav>
<% end -%>
4 changes: 2 additions & 2 deletions kaminari-core/app/views/kaminari/_paginator.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@
-# paginator: the paginator that renders the pagination tags inside
= paginator.render do
%nav.pagination
= first_page_tag unless current_page.first?
= first_page_tag unless current_page.first? || current_page.second?
= prev_page_tag unless current_page.first?
- each_page do |page|
- if page.display_tag?
= page_tag page
- elsif !page.was_truncated?
= gap_tag
= next_page_tag unless current_page.last?
= last_page_tag unless current_page.last?
= last_page_tag unless current_page.last? || current_page.penultimate?
4 changes: 2 additions & 2 deletions kaminari-core/app/views/kaminari/_paginator.html.slim
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@

== paginator.render do
nav.pagination
== first_page_tag unless current_page.first?
== first_page_tag unless current_page.first? || current_page.second?
== prev_page_tag unless current_page.first?
- each_page do |page|
- if page.display_tag?
== page_tag page
- elsif !page.was_truncated?
== gap_tag
== next_page_tag unless current_page.last?
== last_page_tag unless current_page.last?
== last_page_tag unless current_page.last? || current_page.penultimate?
10 changes: 10 additions & 0 deletions kaminari-core/lib/kaminari/helpers/paginator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -106,11 +106,21 @@ def first?
@page == 1
end

# the second page or not
def second?
@page == 2
end

# the last page or not
def last?
@page == @options[:total_pages]
end

# the page before the last or not
def penultimate?
@page + 1 == @options[:total_pages]
end

# the previous page or not
def prev?
@page == @options[:current_page] - 1
Expand Down
4 changes: 2 additions & 2 deletions kaminari-core/test/helpers/paginator_tags_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def tags_for(collection, window: 1, outer_window: 1)
20.times {|i| User.create! name: "user#{i}"}

assert_equal [1, 2, :gap, 10, :next_page, :last_page], tags_for(User.page(1).per(2))
assert_equal [:first_page, :prev_page, 1, 2, 3, :gap, 10, :next_page, :last_page], tags_for(User.page(2).per(2))
assert_equal [:prev_page, 1, 2, 3, :gap, 10, :next_page, :last_page], tags_for(User.page(2).per(2))
assert_equal [:first_page, :prev_page, 1, 2, 3, 4, :gap, 10, :next_page, :last_page], tags_for(User.page(3).per(2))
# the 3rd page doesn't become a gap because it's a single gap
assert_equal [:first_page, :prev_page, 1, 2, 3, 4, 5, :gap, 10, :next_page, :last_page], tags_for(User.page(4).per(2))
Expand All @@ -66,7 +66,7 @@ def tags_for(collection, window: 1, outer_window: 1)
# the 9th page doesn't become a gap because it's a single gap
assert_equal [:first_page, :prev_page, 1, :gap, 6, 7, 8, 9, 10, :next_page, :last_page], tags_for(User.page(7).per(2))
assert_equal [:first_page, :prev_page, 1, :gap, 7, 8, 9, 10, :next_page, :last_page], tags_for(User.page(8).per(2))
assert_equal [:first_page, :prev_page, 1, :gap, 8, 9, 10, :next_page, :last_page], tags_for(User.page(9).per(2))
assert_equal [:first_page, :prev_page, 1, :gap, 8, 9, 10, :next_page], tags_for(User.page(9).per(2))
assert_equal [:first_page, :prev_page, 1, :gap, 9, 10], tags_for(User.page(10).per(2))
end
end
Expand Down