mirror of
https://github.com/kaminari/kaminari.git
synced 2022-11-09 13:44:37 -05:00
fixes #28 a page can be both current and first or last
merge _current_page, _first_page_link, _last_page_link and _page_link into one _page partial
This commit is contained in:
parent
91e9137096
commit
4beef97cea
12 changed files with 44 additions and 148 deletions
|
@ -1,9 +0,0 @@
|
||||||
<%# The current page
|
|
||||||
- available local variables
|
|
||||||
page: the page number of this page
|
|
||||||
current_page: the page number of currently displayed page
|
|
||||||
num_pages: total number of pages
|
|
||||||
per_page: number of items to fetch per page
|
|
||||||
remote: data-remote
|
|
||||||
-%>
|
|
||||||
<span class="page current"><%= page %></span>
|
|
|
@ -1,9 +0,0 @@
|
||||||
-# The current page
|
|
||||||
- available local variables
|
|
||||||
page: the page number of this page
|
|
||||||
current_page: the page number of currently displayed page
|
|
||||||
num_pages: total number of pages
|
|
||||||
per_page: number of items to fetch per page
|
|
||||||
remote: data-remote
|
|
||||||
%span.page.current
|
|
||||||
= page
|
|
|
@ -1,12 +0,0 @@
|
||||||
<%# Link with page number that appears at the leftmost
|
|
||||||
- available local variables
|
|
||||||
page: the page number of this page
|
|
||||||
url: url to this page
|
|
||||||
current_page: the page number of currently displayed page
|
|
||||||
num_pages: total number of pages
|
|
||||||
per_page: number of items to fetch per page
|
|
||||||
remote: data-remote
|
|
||||||
-%>
|
|
||||||
<span class="page first">
|
|
||||||
<%= link_to page, url, :remote => remote %>
|
|
||||||
</span>
|
|
|
@ -1,10 +0,0 @@
|
||||||
-# Link with page number that appears at the leftmost
|
|
||||||
- available local variables
|
|
||||||
page: the page number of this page
|
|
||||||
url: url to this page
|
|
||||||
current_page: the page number of currently displayed page
|
|
||||||
num_pages: total number of pages
|
|
||||||
per_page: number of items to fetch per page
|
|
||||||
remote: data-remote
|
|
||||||
%span.page.first
|
|
||||||
= link_to page, url, :remote => remote
|
|
|
@ -1,12 +0,0 @@
|
||||||
<%# Link with page number that appears at the rightmost
|
|
||||||
- available local variables
|
|
||||||
page: the page number of this page
|
|
||||||
url: url to this page
|
|
||||||
current_page: the page number of currently displayed page
|
|
||||||
num_pages: total number of pages
|
|
||||||
per_page: number of items to fetch per page
|
|
||||||
remote: data-remote
|
|
||||||
-%>
|
|
||||||
<span class="page last">
|
|
||||||
<%= link_to page, url, :remote => remote %>
|
|
||||||
</span>
|
|
|
@ -1,10 +0,0 @@
|
||||||
-# Link with page number that appears at the rightmost
|
|
||||||
- available local variables
|
|
||||||
page: the page number of this page
|
|
||||||
url: url to this page
|
|
||||||
current_page: the page number of currently displayed page
|
|
||||||
num_pages: total number of pages
|
|
||||||
per_page: number of items to fetch per page
|
|
||||||
remote: data-remote
|
|
||||||
%span.page.last
|
|
||||||
= link_to page, url, :remote => remote
|
|
|
@ -7,6 +7,6 @@
|
||||||
per_page: number of items to fetch per page
|
per_page: number of items to fetch per page
|
||||||
remote: data-remote
|
remote: data-remote
|
||||||
-%>
|
-%>
|
||||||
<span class="page">
|
<span class="page<%= ' first' if page.first? %><%= ' last' if page.last? %><%= ' current' if page.current? %>">
|
||||||
<%= link_to page, url, :remote => remote %>
|
<%= link_to_unless page == current_page, page, url, :remote => remote %>
|
||||||
</span>
|
</span>
|
|
@ -6,5 +6,5 @@
|
||||||
num_pages: total number of pages
|
num_pages: total number of pages
|
||||||
per_page: number of items to fetch per page
|
per_page: number of items to fetch per page
|
||||||
remote: data-remote
|
remote: data-remote
|
||||||
%span.page
|
%span{:class => "page#{' first' if page.first?}#{' last' if page.last?}#{' current' if page.current?}"}
|
||||||
= link_to page, url, :remote => remote
|
= link_to_unless page == current_page, page, url, :remote => remote
|
|
@ -10,16 +10,8 @@
|
||||||
<nav class="pagination">
|
<nav class="pagination">
|
||||||
<%= current_page > 1 ? prev_link_tag : prev_span_tag %>
|
<%= current_page > 1 ? prev_link_tag : prev_span_tag %>
|
||||||
<% each_page do |page| -%>
|
<% each_page do |page| -%>
|
||||||
<% if page.current? -%>
|
<% if page.left_outer? || page.right_outer? || page.inside_window? -%>
|
||||||
<%= current_page_tag %>
|
<%= page_tag page %>
|
||||||
<% elsif page.left_outer? || page.right_outer? || page.inside_window? -%>
|
|
||||||
<% if page.first? -%>
|
|
||||||
<%= first_page_link_tag %>
|
|
||||||
<% elsif page.last? -%>
|
|
||||||
<%= last_page_link_tag %>
|
|
||||||
<% else -%>
|
|
||||||
<%= page_link_tag %>
|
|
||||||
<% end -%>
|
|
||||||
<% elsif !page.was_truncated? -%>
|
<% elsif !page.was_truncated? -%>
|
||||||
<%= truncated_span_tag %>
|
<%= truncated_span_tag %>
|
||||||
<% end -%>
|
<% end -%>
|
||||||
|
|
|
@ -9,15 +9,8 @@
|
||||||
%nav.pagination
|
%nav.pagination
|
||||||
= current_page > 1 ? prev_link_tag : prev_span_tag
|
= current_page > 1 ? prev_link_tag : prev_span_tag
|
||||||
- each_page do |page|
|
- each_page do |page|
|
||||||
- if page.current?
|
- if page.left_outer? || page.right_outer? || page.inside_window?
|
||||||
= current_page_tag
|
= page_tag page
|
||||||
- elsif page.left_outer? || page.right_outer? || page.inside_window?
|
|
||||||
- if page.first?
|
|
||||||
= first_page_link_tag
|
|
||||||
- elsif page.last?
|
|
||||||
= last_page_link_tag
|
|
||||||
- else
|
|
||||||
= page_link_tag
|
|
||||||
- elsif !page.was_truncated?
|
- elsif !page.was_truncated?
|
||||||
= truncated_span_tag
|
= truncated_span_tag
|
||||||
= num_pages > current_page ? next_link_tag : next_span_tag
|
= num_pages > current_page ? next_link_tag : next_span_tag
|
||||||
|
|
|
@ -86,17 +86,12 @@ module Kaminari
|
||||||
# enumerate each page providing PageProxy object as the block parameter
|
# enumerate each page providing PageProxy object as the block parameter
|
||||||
def each_page
|
def each_page
|
||||||
1.upto(@options[:num_pages]) do |i|
|
1.upto(@options[:num_pages]) do |i|
|
||||||
@page = i
|
|
||||||
yield PageProxy.new(options, i, @last)
|
yield PageProxy.new(options, i, @last)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
%w[current_page first_page_link last_page_link page_link].each do |tag|
|
def page_tag(page)
|
||||||
eval <<-DEF
|
@last = Page.new @template, :page => page
|
||||||
def #{tag}_tag
|
|
||||||
@last = #{tag.classify}.new @template, :page => @page
|
|
||||||
end
|
|
||||||
DEF
|
|
||||||
end
|
end
|
||||||
|
|
||||||
%w[prev_link prev_span next_link next_span truncated_span].each do |tag|
|
%w[prev_link prev_span next_link next_span truncated_span].each do |tag|
|
||||||
|
@ -113,6 +108,8 @@ module Kaminari
|
||||||
|
|
||||||
# Wraps a "page number" and provides some utility methods
|
# Wraps a "page number" and provides some utility methods
|
||||||
class PageProxy
|
class PageProxy
|
||||||
|
include Comparable
|
||||||
|
|
||||||
def initialize(options, page, last) #:nodoc:
|
def initialize(options, page, last) #:nodoc:
|
||||||
@options, @page, @last = options, page, last
|
@options, @page, @last = options, page, last
|
||||||
end
|
end
|
||||||
|
@ -156,25 +153,27 @@ module Kaminari
|
||||||
def was_truncated?
|
def was_truncated?
|
||||||
@last.is_a? TruncatedSpan
|
@last.is_a? TruncatedSpan
|
||||||
end
|
end
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
# A page
|
def to_i
|
||||||
module Page
|
number
|
||||||
include Renderable
|
end
|
||||||
# target page number
|
|
||||||
def page
|
def to_s
|
||||||
raise 'Override page with the actual page value to be a Page.'
|
number.to_s
|
||||||
end
|
end
|
||||||
def to_s(locals = {}) #:nodoc:
|
|
||||||
super locals.merge(:page => page)
|
def <=>(other)
|
||||||
|
to_i <=> other.to_i
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
# Tag that contains a link
|
# Tag that contains a link
|
||||||
module Link
|
module Link
|
||||||
include Renderable
|
include Renderable
|
||||||
include Page
|
def page
|
||||||
|
raise 'Override page with the actual page value to be a Page.'
|
||||||
|
end
|
||||||
# the link's href
|
# the link's href
|
||||||
def url
|
def url
|
||||||
page_url_for page
|
page_url_for page
|
||||||
|
@ -184,6 +183,18 @@ module Kaminari
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# A page
|
||||||
|
class Page < Tag
|
||||||
|
include Link
|
||||||
|
# target page number
|
||||||
|
def page
|
||||||
|
@options[:page]
|
||||||
|
end
|
||||||
|
def to_s(locals = {}) #:nodoc:
|
||||||
|
super locals.merge(:page => page)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
# Tag that doesn't contain a link
|
# Tag that doesn't contain a link
|
||||||
module NonLink
|
module NonLink
|
||||||
include Renderable
|
include Renderable
|
||||||
|
@ -229,32 +240,6 @@ module Kaminari
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
# Link showing page number
|
|
||||||
class PageLink < Tag
|
|
||||||
include Page
|
|
||||||
include Link
|
|
||||||
def page #:nodoc:
|
|
||||||
@options[:page]
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
# Non-link tag showing the current page number
|
|
||||||
class CurrentPage < Tag
|
|
||||||
include Page
|
|
||||||
include NonLink
|
|
||||||
def page #:nodoc:
|
|
||||||
@options[:page]
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
# Link with page number that appears at the leftmost
|
|
||||||
class FirstPageLink < PageLink
|
|
||||||
end
|
|
||||||
|
|
||||||
# Link with page number that appears at the rightmost
|
|
||||||
class LastPageLink < PageLink
|
|
||||||
end
|
|
||||||
|
|
||||||
# Non-link tag that stands for skipped pages...
|
# Non-link tag that stands for skipped pages...
|
||||||
class TruncatedSpan < Tag
|
class TruncatedSpan < Tag
|
||||||
include NonLink
|
include NonLink
|
||||||
|
|
|
@ -9,35 +9,23 @@ describe 'Kaminari::Helpers' do
|
||||||
end
|
end
|
||||||
describe 'PrevLink' do
|
describe 'PrevLink' do
|
||||||
subject { PrevLink }
|
subject { PrevLink }
|
||||||
its(:ancestor_renderables) { should == [PrevLink, Prev, Link, Page] }
|
its(:ancestor_renderables) { should == [PrevLink, Prev, Link] }
|
||||||
end
|
end
|
||||||
describe 'PrevSpan' do
|
describe 'PrevSpan' do
|
||||||
subject { PrevSpan }
|
subject { PrevSpan }
|
||||||
its(:ancestor_renderables) { should == [PrevSpan, Prev, NonLink] }
|
its(:ancestor_renderables) { should == [PrevSpan, Prev, NonLink] }
|
||||||
end
|
end
|
||||||
describe 'FirstPageLink' do
|
describe 'Page' do
|
||||||
subject { FirstPageLink }
|
subject { Page }
|
||||||
its(:ancestor_renderables) { should == [FirstPageLink, PageLink, Link, Page] }
|
its(:ancestor_renderables) { should == [Page, Link] }
|
||||||
end
|
|
||||||
describe 'PageLink' do
|
|
||||||
subject { PageLink }
|
|
||||||
its(:ancestor_renderables) { should == [PageLink, Link, Page] }
|
|
||||||
end
|
|
||||||
describe 'CurrentPage' do
|
|
||||||
subject { CurrentPage }
|
|
||||||
its(:ancestor_renderables) { should == [CurrentPage, NonLink, Page] }
|
|
||||||
end
|
end
|
||||||
describe 'TruncatedSpan' do
|
describe 'TruncatedSpan' do
|
||||||
subject { TruncatedSpan }
|
subject { TruncatedSpan }
|
||||||
its(:ancestor_renderables) { should == [TruncatedSpan, NonLink] }
|
its(:ancestor_renderables) { should == [TruncatedSpan, NonLink] }
|
||||||
end
|
end
|
||||||
describe 'LastPageLink' do
|
|
||||||
subject { LastPageLink }
|
|
||||||
its(:ancestor_renderables) { should == [LastPageLink, PageLink, Link, Page] }
|
|
||||||
end
|
|
||||||
describe 'NextLink' do
|
describe 'NextLink' do
|
||||||
subject { NextLink }
|
subject { NextLink }
|
||||||
its(:ancestor_renderables) { should == [NextLink, Next, Link, Page] }
|
its(:ancestor_renderables) { should == [NextLink, Next, Link] }
|
||||||
end
|
end
|
||||||
describe 'NextSpan' do
|
describe 'NextSpan' do
|
||||||
subject { NextSpan }
|
subject { NextSpan }
|
||||||
|
@ -149,7 +137,7 @@ describe 'Kaminari::Helpers' do
|
||||||
its(:was_truncated?) { should be_true }
|
its(:was_truncated?) { should be_true }
|
||||||
end
|
end
|
||||||
context 'last.is not a TruncatedSpan' do
|
context 'last.is not a TruncatedSpan' do
|
||||||
subject { Paginator::PageProxy.new({}, 10, PageLink.new(@template)) }
|
subject { Paginator::PageProxy.new({}, 10, Page.new(@template)) }
|
||||||
its(:was_truncated?) { should_not be_true }
|
its(:was_truncated?) { should_not be_true }
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue