Class: Ramaze::Pager
- 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
-
#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.
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
#limit ⇒ Object (readonly)
To be used with Og queries.
101 102 103 |
# File 'lib/ramaze/helper/pager.rb', line 101 def limit @limit end |
#page ⇒ Object (readonly)
The current page.
97 98 99 |
# File 'lib/ramaze/helper/pager.rb', line 97 def page @page end |
#page_count ⇒ Object (readonly)
The total number of pages.
105 106 107 |
# File 'lib/ramaze/helper/pager.rb', line 105 def page_count @page_count end |
#total_count ⇒ Object (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?
182 183 184 |
# File 'lib/ramaze/helper/pager.rb', line 182 def empty? @page_count < 2 end |
#first_page ⇒ Object
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?
139 140 141 |
# File 'lib/ramaze/helper/pager.rb', line 139 def first_page? @page == 1 end |
#last_page ⇒ Object
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?
151 152 153 |
# File 'lib/ramaze/helper/pager.rb', line 151 def last_page? @page == @page_count end |
#navigation ⇒ Object
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 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)
189 190 191 |
# File 'lib/ramaze/helper/pager.rb', line 189 def !empty? end |
#next_page ⇒ Object
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 |
#offset ⇒ Object
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_page ⇒ Object
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 |
#size ⇒ Object
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 |