Class: Kaminari::Helpers::Paginator

Inherits:
Tag
  • Object
show all
Includes:
ActionView::Context
Defined in:
lib/kaminari/helpers/paginator.rb

Overview

The main container tag

Defined Under Namespace

Classes: PageProxy

Instance Method Summary collapse

Methods inherited from Tag

#page_url_for, #partial_path

Constructor Details

#initialize(template, options) ⇒ Paginator

:nodoc:



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/kaminari/helpers/paginator.rb', line 14

def initialize(template, options) #:nodoc:
  #FIXME for compatibility. remove num_pages at some time in the future
  options[:total_pages] ||= options[:num_pages]
  options[:num_pages] ||= options[:total_pages]

  @window_options = {}.tap do |h|
    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
  end
  @template, @options = template, options
  @theme = @options[:theme]
  @views_prefix = @options[:views_prefix]
  @window_options.merge! @options
  @window_options[:current_page] = @options[:current_page] = PageProxy.new(@window_options, @options[:current_page], nil)

  @last = nil
  # initialize the output_buffer for Context
  @output_buffer = ActionView::OutputBuffer.new
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object (private)

delegates view helper methods to @template



111
112
113
# File 'lib/kaminari/helpers/paginator.rb', line 111

def method_missing(name, *args, &block)
  @template.respond_to?(name) ? @template.send(name, *args, &block) : super
end

Instance Method Details

#each_relevant_pageObject Also known as: each_page

enumerate each page providing PageProxy object as the block parameter 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



50
51
52
53
54
55
56
# File 'lib/kaminari/helpers/paginator.rb', line 50

def each_relevant_page
  return to_enum(:each_relevant_page) unless block_given?

  relevant_pages(@window_options).each do |page|
    yield PageProxy.new(@window_options, page, @last)
  end
end

#page_tag(page) ⇒ Object



68
69
70
# File 'lib/kaminari/helpers/paginator.rb', line 68

def page_tag(page)
  @last = Page.new @template, @options.merge(:page => page)
end

#render(&block) ⇒ Object

render given block as a view template



39
40
41
42
# File 'lib/kaminari/helpers/paginator.rb', line 39

def render(&block)
  instance_eval(&block) if @options[:total_pages] > 1
  @output_buffer
end

#to_sObject

:nodoc:



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/kaminari/helpers/paginator.rb', line 80

def to_s #:nodoc:
  subscriber = ActionView::LogSubscriber.log_subscribers.detect {|ls| ls.is_a? ActionView::LogSubscriber}

  # 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
    unless defined? subscriber.render_partial_with_logging
      class << subscriber
        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

    subscriber.render_without_logging = true
    ret = super @window_options.merge :paginator => self
    subscriber.render_without_logging = false

    ret
  else
    super @window_options.merge :paginator => self
  end
end