2011-11-21 06:14:03 -05:00
|
|
|
require 'active_support/inflector'
|
|
|
|
require 'action_view'
|
|
|
|
require 'action_view/log_subscriber'
|
2011-05-03 09:50:35 -04:00
|
|
|
require 'action_view/context'
|
2011-11-21 06:14:03 -05:00
|
|
|
require 'kaminari/helpers/tags'
|
2011-02-10 13:45:43 -05:00
|
|
|
|
2011-02-05 09:05:40 -05:00
|
|
|
module Kaminari
|
|
|
|
module Helpers
|
2011-02-27 09:25:16 -05:00
|
|
|
# The main container tag
|
|
|
|
class Paginator < Tag
|
2011-05-03 09:50:35 -04:00
|
|
|
# so that this instance can actually "render"
|
|
|
|
include ::ActionView::Context
|
|
|
|
|
2011-02-16 09:24:31 -05:00
|
|
|
def initialize(template, options) #:nodoc:
|
2013-11-23 10:39:31 -05:00
|
|
|
#FIXME for compatibility. remove num_pages at some time in the future
|
|
|
|
options[:total_pages] ||= options[:num_pages]
|
|
|
|
options[:num_pages] ||= options[:total_pages]
|
|
|
|
|
2011-02-27 09:25:16 -05:00
|
|
|
@window_options = {}.tap do |h|
|
2011-04-21 02:49:45 -04:00
|
|
|
h[:window] = options.delete(:window) || options.delete(:inner_window) || Kaminari.config.window
|
|
|
|
outer_window = options.delete(:outer_window) || Kaminari.config.outer_window
|
|
|
|
h[:left] = options.delete(:left) || Kaminari.config.left
|
|
|
|
h[:left] = outer_window if h[:left] == 0
|
|
|
|
h[:right] = options.delete(:right) || Kaminari.config.right
|
|
|
|
h[:right] = outer_window if h[:right] == 0
|
2011-02-27 09:25:16 -05:00
|
|
|
end
|
2011-02-27 09:14:32 -05:00
|
|
|
@template, @options = template, options
|
2011-04-25 03:20:07 -04:00
|
|
|
@theme = @options[:theme] ? "#{@options[:theme]}/" : ''
|
2013-11-23 11:04:11 -05:00
|
|
|
@window_options.merge! @options
|
|
|
|
@window_options[:current_page] = @options[:current_page] = PageProxy.new @window_options, @options[:current_page], nil
|
|
|
|
|
2012-06-20 00:25:14 -04:00
|
|
|
@last = nil
|
2011-05-03 09:50:35 -04:00
|
|
|
# initialize the output_buffer for Context
|
2011-02-27 09:25:16 -05:00
|
|
|
@output_buffer = ActionView::OutputBuffer.new
|
|
|
|
end
|
|
|
|
|
|
|
|
# render given block as a view template
|
|
|
|
def render(&block)
|
2012-05-25 02:31:53 -04:00
|
|
|
instance_eval(&block) if @options[:total_pages] > 1
|
2011-02-27 09:25:16 -05:00
|
|
|
@output_buffer
|
|
|
|
end
|
|
|
|
|
|
|
|
# enumerate each page providing PageProxy object as the block parameter
|
2011-12-11 09:12:55 -05:00
|
|
|
# Because of performance reason, this doesn't actually enumerate all pages but pages that are seemingly relevant to the paginator.
|
|
|
|
# "Relevant" pages are:
|
|
|
|
# * pages inside the left outer window plus one for showing the gap tag
|
|
|
|
# * pages inside the inner window plus one on the left plus one on the right for showing the gap tags
|
|
|
|
# * pages inside the right outer window plus one for showing the gap tag
|
2011-07-27 10:35:38 -04:00
|
|
|
def each_relevant_page
|
|
|
|
return to_enum(:each_relevant_page) unless block_given?
|
|
|
|
|
2013-11-23 11:04:11 -05:00
|
|
|
relevant_pages(@window_options).each do |i|
|
|
|
|
yield PageProxy.new(@window_options, i, @last)
|
2011-07-27 10:35:38 -04:00
|
|
|
end
|
|
|
|
end
|
2011-12-11 09:12:55 -05:00
|
|
|
alias each_page each_relevant_page
|
|
|
|
|
|
|
|
def relevant_pages(options)
|
|
|
|
left_window_plus_one = 1.upto(options[:left] + 1).to_a
|
2012-05-25 02:31:53 -04:00
|
|
|
right_window_plus_one = (options[:total_pages] - options[:right]).upto(options[:total_pages]).to_a
|
2011-12-11 09:12:55 -05:00
|
|
|
inside_window_plus_each_sides = (options[:current_page] - options[:window] - 1).upto(options[:current_page] + options[:window] + 1).to_a
|
|
|
|
|
2012-05-25 02:31:53 -04:00
|
|
|
(left_window_plus_one + inside_window_plus_each_sides + right_window_plus_one).uniq.sort.reject {|x| (x < 1) || (x > options[:total_pages])}
|
2011-12-11 09:12:55 -05:00
|
|
|
end
|
|
|
|
private :relevant_pages
|
2011-07-27 10:35:38 -04:00
|
|
|
|
2011-02-27 09:25:16 -05:00
|
|
|
def page_tag(page)
|
|
|
|
@last = Page.new @template, @options.merge(:page => page)
|
|
|
|
end
|
|
|
|
|
|
|
|
%w[first_page prev_page next_page last_page gap].each do |tag|
|
|
|
|
eval <<-DEF
|
|
|
|
def #{tag}_tag
|
|
|
|
@last = #{tag.classify}.new @template, @options
|
|
|
|
end
|
|
|
|
DEF
|
2011-02-16 09:24:31 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def to_s #:nodoc:
|
2011-04-25 02:53:43 -04:00
|
|
|
subscriber = ActionView::LogSubscriber.log_subscribers.detect {|ls| ls.is_a? ActionView::LogSubscriber}
|
|
|
|
|
2013-03-15 11:53:25 -04:00
|
|
|
# There is a logging subscriber
|
|
|
|
# and we don't want it to log render_partial
|
|
|
|
# It is threadsafe, but might not repress logging
|
|
|
|
# consistently in a high-load environment
|
|
|
|
if subscriber
|
2013-05-13 10:49:32 -04:00
|
|
|
unless defined? subscriber.render_partial_with_logging
|
|
|
|
class << subscriber
|
2013-03-15 11:53:25 -04:00
|
|
|
alias_method :render_partial_with_logging, :render_partial
|
|
|
|
attr_accessor :render_without_logging
|
|
|
|
# ugly hack to make a renderer where
|
|
|
|
# we can turn logging on or off
|
|
|
|
def render_partial(event)
|
|
|
|
render_partial_with_logging(event) unless render_without_logging
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2011-02-06 07:21:13 -05:00
|
|
|
|
2013-03-15 11:53:25 -04:00
|
|
|
subscriber.render_without_logging = true
|
2013-11-23 11:04:11 -05:00
|
|
|
ret = super @window_options.merge :paginator => self
|
2013-03-15 11:53:25 -04:00
|
|
|
subscriber.render_without_logging = false
|
2011-04-25 02:53:43 -04:00
|
|
|
|
2013-03-15 11:53:25 -04:00
|
|
|
ret
|
|
|
|
else
|
2013-11-23 11:04:11 -05:00
|
|
|
super @window_options.merge :paginator => self
|
2011-02-06 07:21:13 -05:00
|
|
|
end
|
|
|
|
end
|
2011-02-27 09:25:16 -05:00
|
|
|
|
|
|
|
# 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
|
|
|
|
|
|
|
|
# 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?
|
2012-05-25 02:31:53 -04:00
|
|
|
@page == @options[:total_pages]
|
2011-02-27 09:25:16 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
# the previous page or not
|
|
|
|
def prev?
|
|
|
|
@page == @options[:current_page] - 1
|
|
|
|
end
|
|
|
|
|
|
|
|
# the next page or not
|
|
|
|
def next?
|
|
|
|
@page == @options[:current_page] + 1
|
|
|
|
end
|
|
|
|
|
|
|
|
# within the left outer window or not
|
|
|
|
def left_outer?
|
|
|
|
@page <= @options[:left]
|
|
|
|
end
|
|
|
|
|
|
|
|
# within the right outer window or not
|
|
|
|
def right_outer?
|
2012-05-25 02:31:53 -04:00
|
|
|
@options[:total_pages] - @page < @options[:right]
|
2011-02-27 09:25:16 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
# inside the inner window or not
|
|
|
|
def inside_window?
|
2011-02-27 10:17:09 -05:00
|
|
|
(@options[:current_page] - @page).abs <= @options[:window]
|
2011-02-27 09:25:16 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
# The last rendered tag was "truncated" or not
|
|
|
|
def was_truncated?
|
|
|
|
@last.is_a? Gap
|
|
|
|
end
|
|
|
|
|
|
|
|
def to_i
|
|
|
|
number
|
|
|
|
end
|
|
|
|
|
|
|
|
def to_s
|
|
|
|
number.to_s
|
|
|
|
end
|
|
|
|
|
2011-02-27 10:17:09 -05:00
|
|
|
def +(other)
|
|
|
|
to_i + other.to_i
|
|
|
|
end
|
|
|
|
|
|
|
|
def -(other)
|
|
|
|
to_i - other.to_i
|
|
|
|
end
|
|
|
|
|
2011-02-27 09:25:16 -05:00
|
|
|
def <=>(other)
|
|
|
|
to_i <=> other.to_i
|
|
|
|
end
|
|
|
|
end
|
2011-02-05 09:09:07 -05:00
|
|
|
end
|
2011-02-05 09:05:40 -05:00
|
|
|
end
|
|
|
|
end
|