cosmetics

This commit is contained in:
Akira Matsuda 2011-02-16 23:24:31 +09:00
parent 2353584af5
commit b4263a8264
4 changed files with 42 additions and 9 deletions

View File

@ -4,8 +4,9 @@
num_pages: total number of pages
per_page: number of items to fetch per page
remote: data-remote
paginator: the paginator that renders the pagination tags inside
-%>
<% paginator.compose_tags do -%>
<% paginator.render do -%>
<nav class='pagination'>
<%= current_page > 1 ? prev_link_tag : prev_span_tag %>
<% each_page do |page| -%>

View File

@ -4,7 +4,8 @@
num_pages: total number of pages
per_page: number of items to fetch per page
remote: data-remote
- paginator.compose_tags do
paginator: the paginator that renders the pagination tags inside
- paginator.render do
%nav.pagination
= current_page > 1 ? prev_link_tag : prev_span_tag
- each_page do |page|

View File

@ -2,6 +2,7 @@ require File.join(File.dirname(__FILE__), 'tags')
module Kaminari
module Helpers
# Wraps the template context and helps each tag render itselves
class TemplateWrapper
attr_reader :options, :params
delegate :render, :url_for, :to => :@template
@ -16,7 +17,7 @@ module Kaminari
resolver.find_all(*args_for_lookup(name)).present?
end
def output_buffer
def output_buffer #:nodoc:
@template.instance_variable_get('@output_buffer')
end
@ -34,19 +35,34 @@ module Kaminari
method.call name, 'kaminari', true, []
end
end
end
def method_missing(meth, *args, &blk)
@template.send meth, *args, &blk
# The main class that controlls the whole process
class PaginationRenderer
def initialize(template, options) #:nodoc:
@window_options = {}.tap do |h|
h[:window] = options.delete(:window) || options.delete(:inner_window) || 4
outer_window = options.delete(:outer_window)
h[:left] = options.delete(:left) || outer_window || 1
h[:right] = options.delete(:right) || outer_window || 1
end
@template = TemplateWrapper.new(template, options)
end
def to_s #:nodoc:
suppress_logging_render_partial do
Paginator.new(@template, @window_options).to_s
end
end
private
# dirty hack
def suppress_logging_render_partial(&blk)
if subscriber = ActionView::LogSubscriber.log_subscribers.detect {|ls| ls.is_a? ActionView::LogSubscriber}
class << subscriber
alias_method :render_partial_with_logging, :render_partial
# do nothing
def render_partial(event)
end
def render_partial(event); end
end
ret = blk.call
class << subscriber

View File

@ -76,10 +76,12 @@ module Kaminari
@output_buffer = @template.output_buffer
end
def compose_tags(&block) #:nodoc:
# render given block as a view template
def render(&block)
instance_eval &block if @options[:num_pages] > 1
end
# enumerate each page providing PageProxy object as the block parameter
def each_page
1.upto(@options[:num_pages]) do |i|
@page = i
@ -107,35 +109,48 @@ module Kaminari
super window_options.merge :paginator => self
end
# Wraps a "page number" and provides some utility methods
class PageProxy
def initialize(options, page, last)
def initialize(options, page, last) #:nodoc:
@options, @page, @last = options, page, last
end
# the page number
def number
@page
end
# current page or not
def current?
@page == @options[:current_page]
end
# the first page or not
def first?
@page == 1
end
# the last page or not
def last?
@page == @options[:num_pages]
end
# within the left outer window or not
def left_outer?
@page <= @options[:left] + 1
end
# within the right outer window or not
def right_outer?
@options[:num_pages] - @page <= @options[:right]
end
# inside the inner window or not
def inside_window?
(@page - @options[:current_page]).abs <= @options[:window]
end
# The last rendered tag was "truncated" or not
def was_truncated?
@last.is_a? TruncatedSpan
end