Class: Ramaze::Pager

Inherits:
Object show all
Includes:
Helper::Link
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

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.



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

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.



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

def limit
  @limit
end

#pageObject (readonly)

The current page.



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

def page
  @page
end

#page_countObject (readonly)

The total number of pages.



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

def page_count
  @page_count
end

#total_countObject (readonly)

Total count of items.



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

def total_count
  @total_count
end

Instance Method Details

#each(&block) ⇒ Object

Returns each element for the current page



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

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

#each_with_index(&block) ⇒ Object

Iterator Returns 1-based index.



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

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)


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

def empty?
  @page_count < 2
end

#first_pageObject

Return the first page index.



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

def first_page
  1
end

#first_page?Boolean

Is the first page displayed?

Returns:

  • (Boolean)


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

def first_page?
  @page == 1
end

#last_pageObject

Return the last page index.



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

def last_page
  return @page_count
end

#last_page?Boolean

Is the last page displayed?

Returns:

  • (Boolean)


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

def last_page?
  @page == @page_count
end

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



204
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
# File 'lib/ramaze/helper/pager.rb', line 204

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)


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

def navigation?
  !empty?
end

#next_pageObject

Return the index of the next page.



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

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

#offsetObject

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



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

def offset
  @start_idx
end

#prev_pageObject

Return the index of the previous page.



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

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

#sizeObject

Returns the amount of all elements in all pages.



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

def size
  @total_count
end