Class: Ramaze::Pager

Inherits:
Object show all
Includes:
Helper::Link, Traited
Defined in:
lib/ramaze/helper/pager.rb

Overview

Displays a collection of entitities in multiple pages.

Design

This pager is carefully designed for scaleability. It stores only the items for one page. The key parameter is needed, multiple pagers can coexist in a single page. The pager leverages the SQL LIMIT option to optimize database interaction.

Example

class MyController
  def index
    objs = (0..200).to_a
    @entries, @pager = paginate(objs, :limit => 20)
  end
end

<html>
  <head><title>Pager</title></head>
  <body>
    <?r if pager.navigation? ?>
      <div class="pager">#{@pager.navigation}</div>
    <?r end ?>
    <ul>
    <?r @entries.each do |entry| ?>
      <li>#{entry}</li>
    <?r end ?>
    </ul>
  </body>
</html>

Styling

The following classes can be used for styling with CSS (provided you put the pager in a element with class ‘pager’ like shown above):

.pager {}
.pager .first {}
.pager .previous {}
.pager .next {}
.pager .last {}
.pager ul {}
.pager li {}
.pager li.active {}

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Helper::Link

#breadcrumbs, #route_location

Constructor Details

#initialize(request, limit, total_count, key = trait[:key]) ⇒ Pager

Create a new Pager object.

request

Ramaze::Request object providing access to GET parameters

limit

how many elements go to one page

total_count

total element count

key

key used for getting the current page from GET paramaters

Note: You never have to create this class yourself, use the ‘paginate()` convenience method from the Helper::Pager.



122
123
124
125
126
127
128
129
130
# File 'lib/ramaze/helper/pager.rb', line 122

def initialize(request, limit, total_count, key = trait[:key])
  raise 'limit should be > 0' unless limit > 0

  @request, @key = request, key
  @page = (request.params[key] || 1).to_i
  @limit = limit
  set_count(total_count)
  @start_idx = (@page - 1) * limit
end

Instance Attribute Details

#limitObject (readonly)

To be used with Og queries.



102
103
104
# File 'lib/ramaze/helper/pager.rb', line 102

def limit
  @limit
end

#pageObject (readonly)

The current page.



98
99
100
# File 'lib/ramaze/helper/pager.rb', line 98

def page
  @page
end

#page_countObject (readonly)

The total number of pages.



106
107
108
# File 'lib/ramaze/helper/pager.rb', line 106

def page_count
  @page_count
end

#total_countObject (readonly)

Total count of items.



110
111
112
# File 'lib/ramaze/helper/pager.rb', line 110

def total_count
  @total_count
end

Instance Method Details

#each(&block) ⇒ Object

Returns each element for the current page



170
171
172
# File 'lib/ramaze/helper/pager.rb', line 170

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

#each_with_index(&block) ⇒ Object

Iterator Returns 1-based index.



177
178
179
# File 'lib/ramaze/helper/pager.rb', line 177

def each_with_index(&block)
  @page_items.each_with_index(&block)
end

#empty?Boolean

Is the pager empty, ie has one page only?

Returns:

  • (Boolean)


183
184
185
# File 'lib/ramaze/helper/pager.rb', line 183

def empty?
  @page_count < 2
end

#first_pageObject

Return the first page index.



134
135
136
# File 'lib/ramaze/helper/pager.rb', line 134

def first_page
  1
end

#first_page?Boolean

Is the first page displayed?

Returns:

  • (Boolean)


140
141
142
# File 'lib/ramaze/helper/pager.rb', line 140

def first_page?
  @page == 1
end

#last_pageObject

Return the last page index.



146
147
148
# File 'lib/ramaze/helper/pager.rb', line 146

def last_page
  return @page_count
end

#last_page?Boolean

Is the last page displayed?

Returns:

  • (Boolean)


152
153
154
# File 'lib/ramaze/helper/pager.rb', line 152

def last_page?
  @page == @page_count
end

Override this method in your application if needed. – TODO: better markup. ++



205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
# File 'lib/ramaze/helper/pager.rb', line 205

def navigation
  nav = ""

  unless first_page?
    nav << %{
      <div class="first"><a href="#{link_first_page}">First</a></div>
      <div class="previous"><a href="#{link_prev_page}">Previous</a></div>
    }
  end

  unless last_page?
    nav << %{
      <div class="last"><a href="#{link_last_page}">Last</a></div>
      <div class="next"><a href="#{link_next_page}">Next</a></div>
    }
  end

  nav << %{<ul>}

  for i in nav_range()
    if i == @page
      nav << %{<li class="active">#{i}</li>}
    else
      nav << %{<li><a href="#{target_uri(i)}">#{i}</a></li>}
    end
  end

  nav << %{</ul>}

  return nav
end

Returns true if a navigation is necessary (meaning there is more than one page)

Returns:

  • (Boolean)


190
191
192
# File 'lib/ramaze/helper/pager.rb', line 190

def navigation?
  !empty?
end

#next_pageObject

Return the index of the next page.



164
165
166
# File 'lib/ramaze/helper/pager.rb', line 164

def next_page
  [@page + 1, @page_count].min
end

#offsetObject

Returns the index of the first element to go into the current page



249
250
251
# File 'lib/ramaze/helper/pager.rb', line 249

def offset
  @start_idx
end

#prev_pageObject

Return the index of the previous page.



158
159
160
# File 'lib/ramaze/helper/pager.rb', line 158

def prev_page
  [@page - 1, 1].max
end

#sizeObject

Returns the amount of all elements in all pages.



196
197
198
# File 'lib/ramaze/helper/pager.rb', line 196

def size
  @total_count
end