Class: Kaminari::Helpers::Paginator

Inherits:
Tag
  • Object
show all
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

Constructor Details

#initialize(template, window: nil, outer_window: Kaminari.config.outer_window, left: Kaminari.config.left, right: Kaminari.config.right, inner_window: Kaminari.config.window, **options) ⇒ Paginator

:nodoc:



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/kaminari/helpers/paginator.rb', line 10

def initialize(template, window: nil, outer_window: Kaminari.config.outer_window, left: Kaminari.config.left, right: Kaminari.config.right, inner_window: Kaminari.config.window, **options) #:nodoc:
  @window_options = {window: window || inner_window, left: left.zero? ? outer_window : left, right: right.zero? ? outer_window : right}

  @template, @options, @theme, @views_prefix, @last = template, options, options[:theme], options[:views_prefix], nil
  @window_options.merge! @options
  @window_options[:current_page] = @options[:current_page] = PageProxy.new(@window_options, @options[:current_page], nil)

  #XXX Using parent template's buffer class for rendering each partial here. This might cause problems if the handler mismatches
  @output_buffer = if defined?(::ActionView::OutputBuffer)
    ::ActionView::OutputBuffer.new
  elsif template.instance_variable_get(:@output_buffer)
    template.instance_variable_get(:@output_buffer).class.new
  else
    ActiveSupport::SafeBuffer.new
  end
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



81
82
83
# File 'lib/kaminari/helpers/paginator.rb', line 81

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



43
44
45
46
47
48
49
# File 'lib/kaminari/helpers/paginator.rb', line 43

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



61
62
63
# File 'lib/kaminari/helpers/paginator.rb', line 61

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

#render(&block) ⇒ Object

render given block as a view template



28
29
30
31
32
33
34
35
# File 'lib/kaminari/helpers/paginator.rb', line 28

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

  # This allows for showing fall-back HTML when there's only one page:
  #
  #   <%= paginate(@search_results) || "Showing all search results" %>
  @output_buffer.presence
end

#to_sObject

:nodoc:



73
74
75
76
77
78
# File 'lib/kaminari/helpers/paginator.rb', line 73

def to_s #:nodoc:
  Thread.current[:kaminari_rendering] = true
  super @window_options.merge paginator: self
ensure
  Thread.current[:kaminari_rendering] = false
end