This blog might move to Typo.
Stay tuned.
Tuesday, March 4, 2008
Thursday, January 17, 2008
Trying the Paginator Gem
I needed to find a pagination solution for my Rails 2.0 application, since pagination is no longer supported by Rails in that release. I looked at the various paginations options and deciced to try the Paginator gem.
I wrote a short little script to figure out how the gem worked. The script and it's results are provided below:
Here are the results from running the script:
I wrote a short little script to figure out how the gem worked. The script and it's results are provided below:
# try_paginator.rb
# -- code to get a feel for how the Paginator gem works
#
require 'rubygems'
require 'paginator'
# print each page
puts "\n Printing the items on each page"
bunch_o_data = (1..37).to_a
pager = Paginator.new(bunch_o_data.size, 10) do |offset, per_page|
puts "Executing Paginator Block: offset #{offset}, #{per_page}"
bunch_o_data[offset,per_page]
end
pager.each do |page|
puts "Page ##{page.number}"
page.each do |item|
puts item
end
end
# print items on page 3
puts "\n Printing the items on page 3"
page = pager.page(3)
page.each do |item|
puts item
end
# print items on the next page
puts "\n Printing items on next page: page 4"
page = page.next if page.next?
page.each do |item|
puts item
end
# try to go to next page from the last page
puts "\n Printing items on next page: page 4 again"
page = page.next if page.next?
page.each do |item|
puts item
end
Here are the results from running the script:
ruby try_paginator.rb
Printing the items on each page
Page #1
Executing Paginator Block: offset 0, 10
1
2
3
4
5
6
7
8
9
10
Page #2
Executing Paginator Block: offset 10, 10
11
12
13
14
15
16
17
18
19
20
Page #3
Executing Paginator Block: offset 20, 10
21
22
23
24
25
26
27
28
29
30
Page #4
Executing Paginator Block: offset 30, 10
31
32
33
34
35
36
37
Printing the items on page 3
Executing Paginator Block: offset 20, 10
21
22
23
24
25
26
27
28
29
30
Printing items on next page: page 4
Executing Paginator Block: offset 30, 10
31
32
33
34
35
36
37
Printing items on next page: page 4 again
31
32
33
34
35
36
37
Tuesday, October 9, 2007
Subscribe to:
Posts (Atom)
