1
0
Fork 0
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:
Akira Matsuda 2011-02-25 10:35:21 +09:00
parent 91e9137096
commit 4beef97cea
12 changed files with 44 additions and 148 deletions

View file

@ -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>

View file

@ -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

View file

@ -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>

View file

@ -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

View file

@ -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>

View file

@ -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

View file

@ -7,6 +7,6 @@
per_page: number of items to fetch per page
remote: data-remote
-%>
<span class="page">
<%= link_to page, url, :remote => remote %>
<span class="page<%= ' first' if page.first? %><%= ' last' if page.last? %><%= ' current' if page.current? %>">
<%= link_to_unless page == current_page, page, url, :remote => remote %>
</span>

View file

@ -6,5 +6,5 @@
num_pages: total number of pages
per_page: number of items to fetch per page
remote: data-remote
%span.page
= link_to page, url, :remote => remote
%span{:class => "page#{' first' if page.first?}#{' last' if page.last?}#{' current' if page.current?}"}
= link_to_unless page == current_page, page, url, :remote => remote

View file

@ -10,16 +10,8 @@
<nav class="pagination">
<%= current_page > 1 ? prev_link_tag : prev_span_tag %>
<% each_page do |page| -%>
<% if page.current? -%>
<%= current_page_tag %>
<% 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 -%>
<% if page.left_outer? || page.right_outer? || page.inside_window? -%>
<%= page_tag page %>
<% elsif !page.was_truncated? -%>
<%= truncated_span_tag %>
<% end -%>

View file

@ -9,15 +9,8 @@
%nav.pagination
= current_page > 1 ? prev_link_tag : prev_span_tag
- each_page do |page|
- if page.current?
= current_page_tag
- 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
- if page.left_outer? || page.right_outer? || page.inside_window?
= page_tag page
- elsif !page.was_truncated?
= truncated_span_tag
= num_pages > current_page ? next_link_tag : next_span_tag

View file

@ -86,17 +86,12 @@ module Kaminari
# enumerate each page providing PageProxy object as the block parameter
def each_page
1.upto(@options[:num_pages]) do |i|
@page = i
yield PageProxy.new(options, i, @last)
end
end
%w[current_page first_page_link last_page_link page_link].each do |tag|
eval <<-DEF
def #{tag}_tag
@last = #{tag.classify}.new @template, :page => @page
end
DEF
def page_tag(page)
@last = Page.new @template, :page => page
end
%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
class PageProxy
include Comparable
def initialize(options, page, last) #:nodoc:
@options, @page, @last = options, page, last
end
@ -156,25 +153,27 @@ module Kaminari
def was_truncated?
@last.is_a? TruncatedSpan
end
end
end
# A page
module Page
include Renderable
# target page number
def page
raise 'Override page with the actual page value to be a Page.'
end
def to_s(locals = {}) #:nodoc:
super locals.merge(:page => page)
def to_i
number
end
def to_s
number.to_s
end
def <=>(other)
to_i <=> other.to_i
end
end
end
# Tag that contains a link
module Link
include Renderable
include Page
def page
raise 'Override page with the actual page value to be a Page.'
end
# the link's href
def url
page_url_for page
@ -184,6 +183,18 @@ module Kaminari
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
module NonLink
include Renderable
@ -229,32 +240,6 @@ module Kaminari
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...
class TruncatedSpan < Tag
include NonLink

View file

@ -9,35 +9,23 @@ describe 'Kaminari::Helpers' do
end
describe 'PrevLink' do
subject { PrevLink }
its(:ancestor_renderables) { should == [PrevLink, Prev, Link, Page] }
its(:ancestor_renderables) { should == [PrevLink, Prev, Link] }
end
describe 'PrevSpan' do
subject { PrevSpan }
its(:ancestor_renderables) { should == [PrevSpan, Prev, NonLink] }
end
describe 'FirstPageLink' do
subject { FirstPageLink }
its(:ancestor_renderables) { should == [FirstPageLink, PageLink, Link, Page] }
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] }
describe 'Page' do
subject { Page }
its(:ancestor_renderables) { should == [Page, Link] }
end
describe 'TruncatedSpan' do
subject { TruncatedSpan }
its(:ancestor_renderables) { should == [TruncatedSpan, NonLink] }
end
describe 'LastPageLink' do
subject { LastPageLink }
its(:ancestor_renderables) { should == [LastPageLink, PageLink, Link, Page] }
end
describe 'NextLink' do
subject { NextLink }
its(:ancestor_renderables) { should == [NextLink, Next, Link, Page] }
its(:ancestor_renderables) { should == [NextLink, Next, Link] }
end
describe 'NextSpan' do
subject { NextSpan }
@ -149,7 +137,7 @@ describe 'Kaminari::Helpers' do
its(:was_truncated?) { should be_true }
end
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 }
end
end