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

Inherits:
Object
  • Object
show all
Includes:
Ramaze::Helper
Defined in:
lib/ramaze/helper/paginate.rb

Overview

Provides easy pagination and navigation

Defined Under Namespace

Classes: ArrayPager

Constant Summary

Constants included from Ramaze::Helper

Nitroform, Tagz

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Paginator.



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

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

Instance Method Details

#current_pageObject



153
# File 'lib/ramaze/helper/paginate.rb', line 153

def current_page; @pager.current_page; end

#each(&block) ⇒ Object



150
# File 'lib/ramaze/helper/paginate.rb', line 150

def each(&block) @pager.each(&block) end

#first_page?Boolean

Returns:

  • (Boolean)


151
# File 'lib/ramaze/helper/paginate.rb', line 151

def first_page?; @pager.first_page?; end

#last_pageObject



154
# File 'lib/ramaze/helper/paginate.rb', line 154

def last_page; @pager.last_page; end

#last_page?Boolean

Returns:

  • (Boolean)


155
# File 'lib/ramaze/helper/paginate.rb', line 155

def last_page?; @pager.last_page?; end


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
138
# File 'lib/ramaze/helper/paginate.rb', line 102

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)


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

def needed?
  @pager.page_count > 1
end

#next_pageObject



156
# File 'lib/ramaze/helper/paginate.rb', line 156

def next_page; @pager.next_page; end

#page_countObject

these methods are actually on the pager, but we def them here for convenience (method_missing in helper is evil and even slower)



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

def page_count; @pager.page_count end

#prev_pageObject



152
# File 'lib/ramaze/helper/paginate.rb', line 152

def prev_page; @pager.prev_page; end