1
0
Fork 0
mirror of https://github.com/kaminari/kaminari.git synced 2022-11-09 13:44:37 -05:00

move the whole pagination logic to the paginator partial so that users can see and edit the logic

this removes an internal hack but introduces one or two other kind of black magics...
This commit is contained in:
Akira Matsuda 2011-02-16 06:48:35 +09:00
parent ec5c1be458
commit 5b76f94b51
4 changed files with 38 additions and 29 deletions

View file

@ -5,6 +5,18 @@
per_page: number of items to fetch per page
remote: data-remote
-%>
<nav class='pagination'>
<%= content_for :kaminari_paginator_tags %>
</nav>
<% paginator.compose_tags do -%>
<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? -%>
<%= page_link_tag %>
<% elsif !page.was_truncated? -%>
<%= truncated_span_tag %>
<% end -%>
<% end -%>
<%= num_pages > current_page ? next_link_tag : next_span_tag %>
</nav>
<% end -%>

View file

@ -4,5 +4,14 @@
num_pages: total number of pages
per_page: number of items to fetch per page
remote: data-remote
%nav.pagination
= content_for :kaminari_paginator_tags
- paginator.compose_tags do
%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?
= page_link_tag
- elsif !page.was_truncated?
= truncated_span_tag
= num_pages > current_page ? next_link_tag : next_span_tag

View file

@ -7,16 +7,18 @@ module Kaminari
def initialize(template, options) #:nodoc:
@template, @options = template, options
# so that this Renderer instance can actually "render". Black magic?
@output_buffer = @template.instance_variable_get('@output_buffer')
@params = options[:params] ? template.params.merge(options.delete :params) : template.params
@left, @window, @right = (options[:left] || options[:outer_window] || 1), (options[:window] || options[:inner_window] || 4), (options[:right] || options[:outer_window] || 1)
end
def current_page
def current_page_tag
@last = CurrentPage.new self, :page => @page
end
def page_link
case @page
def page_link_tag
@last = case @page
when 1
FirstPageLink
when @options[:num_pages]
@ -28,7 +30,7 @@ module Kaminari
%w[prev_link prev_span next_link next_span truncated_span].each do |tag|
eval <<-DEF
def #{tag}
def #{tag}_tag
@last = #{tag.classify}.new self
end
DEF
@ -41,22 +43,11 @@ module Kaminari
end
end
def tagify_links #:nodoc:
def compose_tags(&block) #:nodoc:
num_pages, current_page = @options[:num_pages], @options[:current_page]
return [] if num_pages <= 1
tags = []
tags << (current_page > 1 ? prev_link : prev_span)
each_page do |page|
if page.current?
tags << current_page()
elsif page.left_outer? || page.right_outer? || page.inside_window?
tags << page_link
else
tags << truncated_span unless page.was_truncated?
end
end
tags << (num_pages > current_page ? next_link : next_span)
instance_eval &block
end
def partial_exists?(name) #:nodoc:
@ -66,8 +57,6 @@ module Kaminari
def to_s #:nodoc:
suppress_logging_render_partial do
clear_content_for :kaminari_paginator_tags
@template.content_for :kaminari_paginator_tags, tagify_links.join.html_safe
Paginator.new(self).to_s
end
end
@ -111,11 +100,6 @@ module Kaminari
end
end
# another dirty hack
def clear_content_for(name)
@template.instance_variable_get('@_content_for')[name] = ActiveSupport::SafeBuffer.new
end
class PageProxy
def initialize(renderer, page, last)
@renderer, @page, @last = renderer, page, last

View file

@ -168,6 +168,10 @@ module Kaminari
# The container tag
class Paginator < Tag
include Renderable
def to_s(locals = {}) #:nodoc:
super locals.merge(:renderer => @renderer)
end
end
end
end