mirror of
https://github.com/kaminari/kaminari.git
synced 2022-11-09 13:44:37 -05:00
cosmetics
This commit is contained in:
parent
2353584af5
commit
b4263a8264
4 changed files with 42 additions and 9 deletions
|
@ -4,8 +4,9 @@
|
||||||
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
|
||||||
|
paginator: the paginator that renders the pagination tags inside
|
||||||
-%>
|
-%>
|
||||||
<% paginator.compose_tags do -%>
|
<% paginator.render do -%>
|
||||||
<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| -%>
|
||||||
|
|
|
@ -4,7 +4,8 @@
|
||||||
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
|
||||||
- paginator.compose_tags do
|
paginator: the paginator that renders the pagination tags inside
|
||||||
|
- paginator.render do
|
||||||
%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|
|
||||||
|
|
|
@ -2,6 +2,7 @@ require File.join(File.dirname(__FILE__), 'tags')
|
||||||
|
|
||||||
module Kaminari
|
module Kaminari
|
||||||
module Helpers
|
module Helpers
|
||||||
|
# Wraps the template context and helps each tag render itselves
|
||||||
class TemplateWrapper
|
class TemplateWrapper
|
||||||
attr_reader :options, :params
|
attr_reader :options, :params
|
||||||
delegate :render, :url_for, :to => :@template
|
delegate :render, :url_for, :to => :@template
|
||||||
|
@ -16,7 +17,7 @@ module Kaminari
|
||||||
resolver.find_all(*args_for_lookup(name)).present?
|
resolver.find_all(*args_for_lookup(name)).present?
|
||||||
end
|
end
|
||||||
|
|
||||||
def output_buffer
|
def output_buffer #:nodoc:
|
||||||
@template.instance_variable_get('@output_buffer')
|
@template.instance_variable_get('@output_buffer')
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -34,19 +35,34 @@ module Kaminari
|
||||||
method.call name, 'kaminari', true, []
|
method.call name, 'kaminari', true, []
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
||||||
def method_missing(meth, *args, &blk)
|
# The main class that controlls the whole process
|
||||||
@template.send meth, *args, &blk
|
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
|
end
|
||||||
|
|
||||||
|
def to_s #:nodoc:
|
||||||
|
suppress_logging_render_partial do
|
||||||
|
Paginator.new(@template, @window_options).to_s
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
private
|
||||||
# dirty hack
|
# dirty hack
|
||||||
def suppress_logging_render_partial(&blk)
|
def suppress_logging_render_partial(&blk)
|
||||||
if subscriber = ActionView::LogSubscriber.log_subscribers.detect {|ls| ls.is_a? ActionView::LogSubscriber}
|
if subscriber = ActionView::LogSubscriber.log_subscribers.detect {|ls| ls.is_a? ActionView::LogSubscriber}
|
||||||
class << subscriber
|
class << subscriber
|
||||||
alias_method :render_partial_with_logging, :render_partial
|
alias_method :render_partial_with_logging, :render_partial
|
||||||
# do nothing
|
# do nothing
|
||||||
def render_partial(event)
|
def render_partial(event); end
|
||||||
end
|
|
||||||
end
|
end
|
||||||
ret = blk.call
|
ret = blk.call
|
||||||
class << subscriber
|
class << subscriber
|
||||||
|
|
|
@ -76,10 +76,12 @@ module Kaminari
|
||||||
@output_buffer = @template.output_buffer
|
@output_buffer = @template.output_buffer
|
||||||
end
|
end
|
||||||
|
|
||||||
def compose_tags(&block) #:nodoc:
|
# render given block as a view template
|
||||||
|
def render(&block)
|
||||||
instance_eval &block if @options[:num_pages] > 1
|
instance_eval &block if @options[:num_pages] > 1
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# 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
|
@page = i
|
||||||
|
@ -107,35 +109,48 @@ module Kaminari
|
||||||
super window_options.merge :paginator => self
|
super window_options.merge :paginator => self
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Wraps a "page number" and provides some utility methods
|
||||||
class PageProxy
|
class PageProxy
|
||||||
def initialize(options, page, last)
|
def initialize(options, page, last) #:nodoc:
|
||||||
@options, @page, @last = options, page, last
|
@options, @page, @last = options, page, last
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# the page number
|
||||||
|
def number
|
||||||
|
@page
|
||||||
|
end
|
||||||
|
|
||||||
|
# current page or not
|
||||||
def current?
|
def current?
|
||||||
@page == @options[:current_page]
|
@page == @options[:current_page]
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# the first page or not
|
||||||
def first?
|
def first?
|
||||||
@page == 1
|
@page == 1
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# the last page or not
|
||||||
def last?
|
def last?
|
||||||
@page == @options[:num_pages]
|
@page == @options[:num_pages]
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# within the left outer window or not
|
||||||
def left_outer?
|
def left_outer?
|
||||||
@page <= @options[:left] + 1
|
@page <= @options[:left] + 1
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# within the right outer window or not
|
||||||
def right_outer?
|
def right_outer?
|
||||||
@options[:num_pages] - @page <= @options[:right]
|
@options[:num_pages] - @page <= @options[:right]
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# inside the inner window or not
|
||||||
def inside_window?
|
def inside_window?
|
||||||
(@page - @options[:current_page]).abs <= @options[:window]
|
(@page - @options[:current_page]).abs <= @options[:window]
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# The last rendered tag was "truncated" or not
|
||||||
def was_truncated?
|
def was_truncated?
|
||||||
@last.is_a? TruncatedSpan
|
@last.is_a? TruncatedSpan
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue