Class: Ramaze::Pager
- 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
-
#limit ⇒ Object
readonly
To be used with Og queries.
-
#page ⇒ Object
readonly
The current page.
-
#page_count ⇒ Object
readonly
The total number of pages.
-
#total_count ⇒ Object
readonly
Total count of items.
Instance Method Summary collapse
-
#each(&block) ⇒ Object
Returns each element for the current page.
-
#each_with_index(&block) ⇒ Object
Iterator Returns 1-based index.
-
#empty? ⇒ Boolean
Is the pager empty, ie has one page only?.
-
#first_page ⇒ Object
Return the first page index.
-
#first_page? ⇒ Boolean
Is the first page displayed?.
-
#initialize(request, limit, total_count, key = trait[:key]) ⇒ Pager
constructor
Create a new Pager object.
-
#last_page ⇒ Object
Return the last page index.
-
#last_page? ⇒ Boolean
Is the last page displayed?.
-
#navigation ⇒ Object
Override this method in your application if needed.
-
#navigation? ⇒ Boolean
Returns true if a navigation is necessary (meaning there is more than one page).
-
#next_page ⇒ Object
Return the index of the next page.
-
#offset ⇒ Object
Returns the index of the first element to go into the current page.
-
#prev_page ⇒ Object
Return the index of the previous page.
-
#size ⇒ Object
Returns the amount of all elements in all pages.
Methods included from Helper::Link
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
#limit ⇒ Object (readonly)
To be used with Og queries.
102 103 104 |
# File 'lib/ramaze/helper/pager.rb', line 102 def limit @limit end |
#page ⇒ Object (readonly)
The current page.
98 99 100 |
# File 'lib/ramaze/helper/pager.rb', line 98 def page @page end |
#page_count ⇒ Object (readonly)
The total number of pages.
106 107 108 |
# File 'lib/ramaze/helper/pager.rb', line 106 def page_count @page_count end |
#total_count ⇒ Object (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?
183 184 185 |
# File 'lib/ramaze/helper/pager.rb', line 183 def empty? @page_count < 2 end |
#first_page ⇒ Object
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?
140 141 142 |
# File 'lib/ramaze/helper/pager.rb', line 140 def first_page? @page == 1 end |
#last_page ⇒ Object
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?
152 153 154 |
# File 'lib/ramaze/helper/pager.rb', line 152 def last_page? @page == @page_count end |
#navigation ⇒ Object
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 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 |
#navigation? ⇒ Boolean
Returns true if a navigation is necessary (meaning there is more than one page)
190 191 192 |
# File 'lib/ramaze/helper/pager.rb', line 190 def !empty? end |
#next_page ⇒ Object
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 |
#offset ⇒ Object
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_page ⇒ Object
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 |
#size ⇒ Object
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 |