Class: Ramaze::Helper::Paginate::Paginator

Inherits:
Object
  • Object
show all
Includes:
CGI, Link
Defined in:
lib/ramaze/helper/paginate.rb

Overview

Provides easy pagination and navigation

Defined Under Namespace

Classes: ArrayPager

Instance Method Summary collapse

Methods included from CGI

#h, #html_escape, #html_unescape, #url_decode, #url_encode

Methods included from Link

#A, #R, #Rs, #breadcrumbs

Constructor Details

#initialize(data = [], page = 1, limit = 10, var = 'pager') ⇒ Paginator

Returns a new instance of Paginator.



68
69
70
71
72
73
# File 'lib/ramaze/helper/paginate.rb', line 68

def initialize(data = [], page = 1, limit = 10, var = 'pager')
  @data, @page, @limit, @var = data, page, limit, var
  @pager = pager_for(data)
  @page = @page > page_count ? page_count : @page
  @pager = pager_for(data)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *args, &block) ⇒ Object

Forward everything to the inner @pager



147
148
149
# File 'lib/ramaze/helper/paginate.rb', line 147

def method_missing(meth, *args, &block)
  @pager.send(meth, *args, &block)
end

Instance Method Details



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/ramaze/helper/paginate.rb', line 101

def navigation(limit = 8)
  out = [ g.div(:class => :pager) ]

  if first_page?
    out << g.span(:class => 'first grey'){ h('<<') }
    out << g.span(:class => 'previous grey'){ h('<') }
  else
    out << link(1, '<<', :class => :first)
    out << link(prev_page, '<', :class => :previous)
  end

  lower = limit ? (current_page - limit) : 1
  lower = lower < 1 ? 1 : lower

  (lower...current_page).each do |n|
    out << link(n)
  end

  out << link(current_page, current_page, :class => :current)

  if last_page?
    out << g.span(:class => 'next grey'){ h('>') }
    out << g.span(:class => 'last grey'){ h('>>') }
  elsif next_page
    higher = limit ? (next_page + limit) : page_count
    higher = [higher, page_count].min
    (next_page..higher).each do |n|
      out << link(n)
    end

    out << link(next_page, '>', :class => :next)
    out << link(page_count, '>>', :class => :last)
  end

  out << '</div>'
  out.map{|e| e.to_s}.join("\n")
end

#needed?Boolean

Useful to omit pager if it’s of no use.

Returns:

  • (Boolean)


141
142
143
# File 'lib/ramaze/helper/paginate.rb', line 141

def needed?
  @pager.page_count > 1
end