paginator 1.1.1 → 1.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +17 -0
- data/.ruby-version +1 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +64 -0
- data/Rakefile +7 -17
- data/lib/paginator.rb +10 -139
- data/lib/paginator/page.rb +82 -0
- data/lib/paginator/pager.rb +59 -0
- data/lib/paginator/version.rb +3 -0
- data/paginator.gemspec +22 -0
- data/{test/test_paginator.rb → spec/paginator_spec.rb} +47 -54
- metadata +59 -57
- data/History.txt +0 -43
- data/Manifest.txt +0 -7
- data/README.txt +0 -84
- data/bin/paginator +0 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 412dd437adc3fd2e1f4346d8f4a1625922b61a51
|
4
|
+
data.tar.gz: e73eb679ec867386d7fca8b010da284e5bb0fa29
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: c2552a2b8740b47ace8e8d45d7b1ec31dc1141224abf1a68d849d6fad64beda5d6834d736305aeac921ccb14ba6e12f13eb0ac8472d7ed8be88a2704c29a8941
|
7
|
+
data.tar.gz: 5483016b70e70910c977c30b59a75d05c9029fefbde45a3aab2c3ca7036d236a930aff0c383d2f015d0c400c6f7e1fb3dfdb910fdfebfb3f958a9a9ca4531e96
|
data/.gitignore
ADDED
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
2.0.0-p247
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2006-2013 Bruce Williams
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,64 @@
|
|
1
|
+
Paginator
|
2
|
+
=========
|
3
|
+
|
4
|
+
Paginator is a simple pagination class that provides a generic interface suitable
|
5
|
+
for use in any Ruby program.
|
6
|
+
|
7
|
+
Paginator doesn't make any assumptions as to how data is retrieved; you just
|
8
|
+
have to provide it with the total number of objects and a way to pull a specific
|
9
|
+
set of objects based on the offset and number of objects per page.
|
10
|
+
|
11
|
+
Examples
|
12
|
+
--------
|
13
|
+
|
14
|
+
In both of these examples I'm using a `PER_PAGE` constant (the number of
|
15
|
+
items per page), but it's merely for labeling purposes.
|
16
|
+
|
17
|
+
You could, of course, just pass in the number of items per page
|
18
|
+
directly to the initializer without assigning it somewhere beforehand.
|
19
|
+
|
20
|
+
### In Plain Ruby
|
21
|
+
|
22
|
+
```ruby
|
23
|
+
bunch_o_data = (1..60).to_a
|
24
|
+
pager = Paginator.create(bunch_o_data.size, PER_PAGE) do |offset, per_page|
|
25
|
+
bunch_o_data[offset,per_page]
|
26
|
+
end
|
27
|
+
pager.each do |page|
|
28
|
+
puts "Page ##{page.number}"
|
29
|
+
page.each do |item|
|
30
|
+
puts item
|
31
|
+
end
|
32
|
+
end
|
33
|
+
```
|
34
|
+
|
35
|
+
### In Rails
|
36
|
+
|
37
|
+
Nothing changes with Paginator; you just use ActiveRecord from within
|
38
|
+
the block you provide.
|
39
|
+
|
40
|
+
A controller action:
|
41
|
+
|
42
|
+
```ruby
|
43
|
+
def index
|
44
|
+
@pager = Paginator.create(Foo.count, PER_PAGE) do |offset, per_page|
|
45
|
+
Foo.limit(per_page).offset(offset)
|
46
|
+
end
|
47
|
+
@page = @pager.page(params[:page])
|
48
|
+
end
|
49
|
+
```
|
50
|
+
|
51
|
+
In your view:
|
52
|
+
|
53
|
+
```erb
|
54
|
+
<% @page.each do |foo| %>
|
55
|
+
<%# Show something for each item %>
|
56
|
+
<% end %>
|
57
|
+
<%= @page.number %>
|
58
|
+
<%= link_to("Prev", foos_url(page: @page.prev.number)) if @page.prev? %>
|
59
|
+
<%= link_to("Next", foos_url(page: @page.next.number)) if @page.next? %>
|
60
|
+
```
|
61
|
+
|
62
|
+
### License
|
63
|
+
|
64
|
+
See `LICENSE.txt`.
|
data/Rakefile
CHANGED
@@ -1,21 +1,11 @@
|
|
1
|
-
|
1
|
+
require "bundler/gem_tasks"
|
2
2
|
|
3
|
-
require '
|
4
|
-
require 'hoe'
|
5
|
-
require './lib/paginator.rb'
|
3
|
+
require 'rake/testtask'
|
6
4
|
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
Paginator doesn't make any assumptions as to how data is retrieved; you just
|
12
|
-
have to provide it with the total number of objects and a way to pull a specific
|
13
|
-
set of objects based on the offset and number of objects per page.
|
14
|
-
EOD
|
15
|
-
p.url = "http://paginator.rubyforge.org"
|
16
|
-
p.changes = p.paragraphs_of('History.txt', 0..1).join("\n\n")
|
17
|
-
p.email = %q{bruce@codefluency.com}
|
18
|
-
p.author = ["Bruce Williams"]
|
5
|
+
Rake::TestTask.new do |t|
|
6
|
+
t.libs << "test"
|
7
|
+
t.test_files = FileList['spec/*_spec.rb']
|
8
|
+
t.verbose = true
|
19
9
|
end
|
20
10
|
|
21
|
-
|
11
|
+
task :default => :test
|
data/lib/paginator.rb
CHANGED
@@ -1,143 +1,14 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
class Paginator
|
4
|
-
|
5
|
-
VERSION = '1.1.1'
|
6
|
-
|
7
|
-
include Enumerable
|
1
|
+
module Paginator
|
8
2
|
|
9
3
|
class ArgumentError < ::ArgumentError; end
|
10
4
|
class MissingCountError < ArgumentError; end
|
11
|
-
class MissingSelectError < ArgumentError; end
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
# * A total count of the number of objects to paginate
|
19
|
-
# * The number of objects in each page
|
20
|
-
# * A block that returns the array of items
|
21
|
-
# * The block is passed the item offset
|
22
|
-
# (and the number of items to show per page, for
|
23
|
-
# convenience, if the arity is 2)
|
24
|
-
def initialize(count, per_page, &select)
|
25
|
-
@count, @per_page = count, per_page
|
26
|
-
unless select
|
27
|
-
raise MissingSelectError, "Must provide block to select data for each page"
|
28
|
-
end
|
29
|
-
@select = select
|
30
|
-
end
|
31
|
-
|
32
|
-
# Total number of pages
|
33
|
-
def number_of_pages
|
34
|
-
(@count / @per_page).to_i + (@count % @per_page > 0 ? 1 : 0)
|
35
|
-
end
|
36
|
-
|
37
|
-
# First page object
|
38
|
-
def first
|
39
|
-
page 1
|
40
|
-
end
|
41
|
-
|
42
|
-
# Last page object
|
43
|
-
def last
|
44
|
-
page number_of_pages
|
45
|
-
end
|
46
|
-
|
47
|
-
def each
|
48
|
-
1.upto(number_of_pages) do |number|
|
49
|
-
yield page(number)
|
50
|
-
end
|
51
|
-
end
|
52
|
-
|
53
|
-
# Retrieve page object by number
|
54
|
-
def page(number)
|
55
|
-
number = (n = number.to_i) > 0 ? n : 1
|
56
|
-
Page.new(self, number, lambda {
|
57
|
-
offset = (number - 1) * @per_page
|
58
|
-
args = [offset]
|
59
|
-
args << @per_page if @select.arity == 2
|
60
|
-
@select.call(*args)
|
61
|
-
})
|
62
|
-
end
|
63
|
-
|
64
|
-
# Page object
|
65
|
-
#
|
66
|
-
# Retrieves items for a page and provides metadata about the position
|
67
|
-
# of the page in the paginator
|
68
|
-
class Page
|
69
|
-
|
70
|
-
include Enumerable
|
71
|
-
|
72
|
-
attr_reader :number, :pager
|
73
|
-
|
74
|
-
def initialize(pager, number, select) #:nodoc:
|
75
|
-
@pager, @number = pager, number
|
76
|
-
@offset = (number - 1) * pager.per_page
|
77
|
-
@select = select
|
78
|
-
end
|
79
|
-
|
80
|
-
# Retrieve the items for this page
|
81
|
-
# * Caches
|
82
|
-
def items
|
83
|
-
@items ||= @select.call
|
84
|
-
end
|
85
|
-
|
86
|
-
# Does this page have any items?
|
87
|
-
def empty?
|
88
|
-
items.empty?
|
89
|
-
end
|
90
|
-
|
91
|
-
# Checks to see if there's a page before this one
|
92
|
-
def prev?
|
93
|
-
@number > 1
|
94
|
-
end
|
95
|
-
|
96
|
-
# # Get previous page (if possible)
|
97
|
-
def prev
|
98
|
-
@pager.page(@number - 1) if prev?
|
99
|
-
end
|
100
|
-
|
101
|
-
# Checks to see if there's a page after this one
|
102
|
-
def next?
|
103
|
-
@number < @pager.number_of_pages
|
104
|
-
end
|
105
|
-
|
106
|
-
# Get next page (if possible)
|
107
|
-
def next
|
108
|
-
@pager.page(@number + 1) if next?
|
109
|
-
end
|
110
|
-
|
111
|
-
# The "item number" of the first item on this page
|
112
|
-
def first_item_number
|
113
|
-
1 + @offset
|
114
|
-
end
|
115
|
-
|
116
|
-
# The "item number" of the last item on this page
|
117
|
-
def last_item_number
|
118
|
-
if next?
|
119
|
-
@offset + @pager.per_page
|
120
|
-
else
|
121
|
-
@pager.count
|
122
|
-
end
|
123
|
-
end
|
124
|
-
|
125
|
-
def ==(other) #:nodoc:
|
126
|
-
@pager == other.pager && self.number == other.number
|
127
|
-
end
|
128
|
-
|
129
|
-
def each(&block)
|
130
|
-
items.each(&block)
|
131
|
-
end
|
132
|
-
|
133
|
-
def method_missing(meth, *args, &block) #:nodoc:
|
134
|
-
if @pager.respond_to?(meth)
|
135
|
-
@pager.__send__(meth, *args, &block)
|
136
|
-
else
|
137
|
-
super
|
138
|
-
end
|
139
|
-
end
|
140
|
-
|
5
|
+
class MissingSelectError < ArgumentError; end
|
6
|
+
|
7
|
+
autoload :Pager, 'paginator/pager'
|
8
|
+
autoload :Page, 'paginator/page'
|
9
|
+
|
10
|
+
def self.create(count, per_page, &select)
|
11
|
+
Pager.new(count, per_page, &select)
|
141
12
|
end
|
142
|
-
|
143
|
-
end
|
13
|
+
|
14
|
+
end
|
@@ -0,0 +1,82 @@
|
|
1
|
+
module Paginator
|
2
|
+
|
3
|
+
# Page object
|
4
|
+
#
|
5
|
+
# Retrieves items for a page and provides metadata about the position
|
6
|
+
# of the page in the paginator
|
7
|
+
class Page
|
8
|
+
|
9
|
+
include Enumerable
|
10
|
+
|
11
|
+
attr_reader :number, :pager
|
12
|
+
|
13
|
+
def initialize(pager, number, select) #:nodoc:
|
14
|
+
@pager, @number = pager, number
|
15
|
+
@offset = (number - 1) * pager.per_page
|
16
|
+
@select = select
|
17
|
+
end
|
18
|
+
|
19
|
+
# Retrieve the items for this page
|
20
|
+
# * Caches
|
21
|
+
def items
|
22
|
+
@items ||= @select.call
|
23
|
+
end
|
24
|
+
|
25
|
+
# Does this page have any items?
|
26
|
+
def empty?
|
27
|
+
items.empty?
|
28
|
+
end
|
29
|
+
|
30
|
+
# Checks to see if there's a page before this one
|
31
|
+
def prev?
|
32
|
+
@number > 1
|
33
|
+
end
|
34
|
+
|
35
|
+
# Get previous page (if possible)
|
36
|
+
def prev
|
37
|
+
@pager.page(@number - 1) if prev?
|
38
|
+
end
|
39
|
+
|
40
|
+
# Checks to see if there's a page after this one
|
41
|
+
def next?
|
42
|
+
@number < @pager.number_of_pages
|
43
|
+
end
|
44
|
+
|
45
|
+
# Get next page (if possible)
|
46
|
+
def next
|
47
|
+
@pager.page(@number + 1) if next?
|
48
|
+
end
|
49
|
+
|
50
|
+
# The "item number" of the first item on this page
|
51
|
+
def first_item_number
|
52
|
+
1 + @offset
|
53
|
+
end
|
54
|
+
|
55
|
+
# The "item number" of the last item on this page
|
56
|
+
def last_item_number
|
57
|
+
if next?
|
58
|
+
@offset + @pager.per_page
|
59
|
+
else
|
60
|
+
@pager.count
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
def ==(other) #:nodoc:
|
65
|
+
@pager == other.pager && self.number == other.number
|
66
|
+
end
|
67
|
+
|
68
|
+
def each(&block)
|
69
|
+
items.each(&block)
|
70
|
+
end
|
71
|
+
|
72
|
+
def method_missing(meth, *args, &block) #:nodoc:
|
73
|
+
if @pager.respond_to?(meth)
|
74
|
+
@pager.__send__(meth, *args, &block)
|
75
|
+
else
|
76
|
+
super
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
end
|
81
|
+
|
82
|
+
end
|
@@ -0,0 +1,59 @@
|
|
1
|
+
module Paginator
|
2
|
+
|
3
|
+
class Pager
|
4
|
+
include Enumerable
|
5
|
+
|
6
|
+
attr_reader :per_page, :count
|
7
|
+
|
8
|
+
# Instantiate a new Paginator object
|
9
|
+
#
|
10
|
+
# Provide:
|
11
|
+
# * A total count of the number of objects to paginate
|
12
|
+
# * The number of objects in each page
|
13
|
+
# * A block that returns the array of items
|
14
|
+
# * The block is passed the item offset
|
15
|
+
# (and the number of items to show per page, for
|
16
|
+
# convenience, if the arity is 2)
|
17
|
+
def initialize(count, per_page, &select)
|
18
|
+
@count, @per_page = count, per_page
|
19
|
+
unless select
|
20
|
+
raise MissingSelectError, "Must provide block to select data for each page"
|
21
|
+
end
|
22
|
+
@select = select
|
23
|
+
end
|
24
|
+
|
25
|
+
# Total number of pages
|
26
|
+
def number_of_pages
|
27
|
+
(@count / @per_page).to_i + (@count % @per_page > 0 ? 1 : 0)
|
28
|
+
end
|
29
|
+
|
30
|
+
# First page object
|
31
|
+
def first
|
32
|
+
page 1
|
33
|
+
end
|
34
|
+
|
35
|
+
# Last page object
|
36
|
+
def last
|
37
|
+
page number_of_pages
|
38
|
+
end
|
39
|
+
|
40
|
+
def each(&block)
|
41
|
+
return enum_for(:each) unless block_given?
|
42
|
+
1.upto(number_of_pages) do |number|
|
43
|
+
yield page(number)
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
# Retrieve page object by number
|
48
|
+
def page(number)
|
49
|
+
number = (n = number.to_i) > 0 ? n : 1
|
50
|
+
Page.new(self, number, lambda {
|
51
|
+
offset = (number - 1) * @per_page
|
52
|
+
args = [offset]
|
53
|
+
args << @per_page if @select.arity == 2
|
54
|
+
@select.call(*args)
|
55
|
+
})
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
end
|
data/paginator.gemspec
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'paginator/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "paginator"
|
8
|
+
spec.version = Paginator::VERSION
|
9
|
+
spec.authors = ["Bruce Williams"]
|
10
|
+
spec.email = ["bruce.williams@livingsocial.com"]
|
11
|
+
spec.summary = 'A generic paginator object for use in any Ruby program'
|
12
|
+
spec.description = "Paginator doesn't make any assumptions as to how data is retrieved; you just have to provide it with tee total number of objects and a way to pull a specific set of objects based on the offset and number of objects per page."
|
13
|
+
spec.homepage = "http://github.com/bruce/paginator"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files`.split($/)
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.3"
|
22
|
+
end
|
@@ -1,66 +1,68 @@
|
|
1
|
-
require '
|
1
|
+
require 'minitest/autorun'
|
2
|
+
require 'minitest/spec'
|
2
3
|
require 'paginator'
|
3
4
|
|
4
|
-
|
5
|
+
PER_PAGE = 10
|
5
6
|
|
6
|
-
|
7
|
+
describe Paginator do
|
7
8
|
|
8
|
-
|
9
|
+
before do
|
9
10
|
@data = (0..43).to_a
|
10
|
-
@pager = Paginator.
|
11
|
-
@data[offset,per_page]
|
11
|
+
@pager = Paginator.create(@data.size, PER_PAGE) do |offset, per_page|
|
12
|
+
@data[offset, per_page]
|
12
13
|
end
|
13
14
|
end
|
14
|
-
|
15
|
-
|
15
|
+
|
16
|
+
it "needs a block" do
|
16
17
|
assert_raises(Paginator::MissingSelectError) do
|
17
|
-
@pager = Paginator.
|
18
|
+
@pager = Paginator.create(@data.size, PER_PAGE)
|
18
19
|
end
|
19
|
-
end
|
20
|
-
|
21
|
-
def test_can_get_last_page_from_page_object
|
22
|
-
assert_equal @pager.last, @pager.page(2).last
|
23
20
|
end
|
24
|
-
|
25
|
-
|
26
|
-
assert_equal @pager.
|
21
|
+
|
22
|
+
it "can get the last page" do
|
23
|
+
assert_equal @pager.last, @pager.page(5)
|
27
24
|
end
|
28
25
|
|
29
|
-
|
26
|
+
it "can get the first page" do
|
27
|
+
assert_equal @pager.first, @pager.page(1)
|
28
|
+
end
|
29
|
+
|
30
|
+
it "does not exceed per_page" do
|
30
31
|
@pager.each do |page|
|
31
32
|
assert page.items.size <= PER_PAGE
|
32
33
|
end
|
33
34
|
end
|
34
|
-
|
35
|
-
|
35
|
+
|
36
|
+
it "only has less items on the last page" do
|
36
37
|
@pager.each do |page|
|
37
38
|
if page != @pager.last
|
38
39
|
assert page.items.size <= PER_PAGE
|
39
40
|
else
|
40
41
|
assert page.items.size < PER_PAGE
|
41
42
|
end
|
42
|
-
end
|
43
|
+
end
|
43
44
|
end
|
44
|
-
|
45
|
-
|
45
|
+
|
46
|
+
it "returns the correct first page" do
|
46
47
|
assert_equal @pager.page(1).number, @pager.first.number
|
47
48
|
end
|
48
|
-
|
49
|
-
|
49
|
+
|
50
|
+
|
51
|
+
it "returns the correct last page" do
|
50
52
|
assert_equal @pager.page(5).number, @pager.last.number
|
51
53
|
end
|
52
|
-
|
53
|
-
|
54
|
+
|
55
|
+
it "has no next page when on the last page" do
|
54
56
|
assert !@pager.last.next?
|
55
57
|
assert !@pager.last.next
|
56
58
|
end
|
57
59
|
|
58
|
-
|
60
|
+
it "has no previous page on the first page" do
|
59
61
|
assert !@pager.first.prev?
|
60
62
|
assert !@pager.first.prev
|
61
63
|
end
|
62
|
-
|
63
|
-
|
64
|
+
|
65
|
+
it "is enumerable" do
|
64
66
|
@pager.each do |page|
|
65
67
|
assert page
|
66
68
|
page.each do |item|
|
@@ -72,14 +74,14 @@ class PaginatorTest < Test::Unit::TestCase
|
|
72
74
|
assert_equal page.items, page.inject([]) {|list, item| list << item }
|
73
75
|
end
|
74
76
|
end
|
75
|
-
|
76
|
-
|
77
|
+
|
78
|
+
it "supports each.with_index" do
|
77
79
|
page_offset = 0
|
78
|
-
@pager.
|
80
|
+
@pager.each.with_index do |page, page_index|
|
79
81
|
assert page
|
80
82
|
assert_equal page_offset, page_index
|
81
83
|
item_offset = 0
|
82
|
-
page.
|
84
|
+
page.each.with_index do |item, item_index|
|
83
85
|
assert item
|
84
86
|
assert_equal item_offset, item_index
|
85
87
|
item_offset += 1
|
@@ -87,28 +89,28 @@ class PaginatorTest < Test::Unit::TestCase
|
|
87
89
|
page_offset += 1
|
88
90
|
end
|
89
91
|
end
|
90
|
-
|
91
|
-
|
92
|
+
|
93
|
+
it "has the correct number of pages" do
|
92
94
|
assert_equal 5, @pager.number_of_pages
|
93
95
|
end
|
94
|
-
|
95
|
-
|
96
|
-
pager = Paginator.
|
96
|
+
|
97
|
+
it "yields per_page for a block with an arity of 2" do
|
98
|
+
pager = Paginator.create(20, 2) do |offset, per_page|
|
97
99
|
assert_equal 2, per_page
|
98
100
|
end
|
99
101
|
pager.page(1).items
|
100
102
|
end
|
101
103
|
|
102
|
-
|
103
|
-
pager = Paginator.
|
104
|
+
it "does not yield per_page for a block of arity 1" do
|
105
|
+
pager = Paginator.create(20, 2) do |offset|
|
104
106
|
assert_equal 0, offset
|
105
107
|
end
|
106
108
|
pager.page(1).items
|
107
109
|
end
|
108
|
-
|
109
|
-
|
110
|
+
|
111
|
+
it "has pages that know the first and last page numbers" do
|
110
112
|
items = (1..11).to_a
|
111
|
-
pager = Paginator.
|
113
|
+
pager = Paginator.create(items.size,3) do |offset, per_page|
|
112
114
|
items[offset, per_page]
|
113
115
|
end
|
114
116
|
page = pager.page(1)
|
@@ -119,19 +121,10 @@ class PaginatorTest < Test::Unit::TestCase
|
|
119
121
|
assert_equal 6, page.last_item_number
|
120
122
|
page = pager.page(3)
|
121
123
|
assert_equal 7, page.first_item_number
|
122
|
-
assert_equal 9, page.last_item_number
|
124
|
+
assert_equal 9, page.last_item_number
|
123
125
|
page = pager.page(4)
|
124
126
|
assert_equal 10, page.first_item_number
|
125
|
-
assert_equal 11, page.last_item_number
|
127
|
+
assert_equal 11, page.last_item_number
|
126
128
|
end
|
127
129
|
|
128
130
|
end
|
129
|
-
|
130
|
-
class PaginatorTestWithMathN < PaginatorTest
|
131
|
-
|
132
|
-
def setup
|
133
|
-
require 'mathn'
|
134
|
-
super
|
135
|
-
end
|
136
|
-
|
137
|
-
end
|
metadata
CHANGED
@@ -1,71 +1,73 @@
|
|
1
|
-
--- !ruby/object:Gem::Specification
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
2
|
name: paginator
|
3
|
-
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.2.0
|
5
5
|
platform: ruby
|
6
|
-
authors:
|
6
|
+
authors:
|
7
7
|
- Bruce Williams
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
11
|
+
date: 2013-07-02 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ~>
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.3'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ~>
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.3'
|
27
|
+
description: Paginator doesn't make any assumptions as to how data is retrieved; you
|
28
|
+
just have to provide it with tee total number of objects and a way to pull a specific
|
29
|
+
set of objects based on the offset and number of objects per page.
|
30
|
+
email:
|
31
|
+
- bruce.williams@livingsocial.com
|
32
|
+
executables: []
|
29
33
|
extensions: []
|
30
|
-
|
31
|
-
|
32
|
-
-
|
33
|
-
-
|
34
|
-
-
|
35
|
-
|
36
|
-
-
|
37
|
-
- Manifest.txt
|
38
|
-
- README.txt
|
34
|
+
extra_rdoc_files: []
|
35
|
+
files:
|
36
|
+
- .gitignore
|
37
|
+
- .ruby-version
|
38
|
+
- Gemfile
|
39
|
+
- LICENSE.txt
|
40
|
+
- README.md
|
39
41
|
- Rakefile
|
40
|
-
- bin/paginator
|
41
42
|
- lib/paginator.rb
|
42
|
-
-
|
43
|
-
|
44
|
-
|
43
|
+
- lib/paginator/page.rb
|
44
|
+
- lib/paginator/pager.rb
|
45
|
+
- lib/paginator/version.rb
|
46
|
+
- paginator.gemspec
|
47
|
+
- spec/paginator_spec.rb
|
48
|
+
homepage: http://github.com/bruce/paginator
|
49
|
+
licenses:
|
50
|
+
- MIT
|
51
|
+
metadata: {}
|
45
52
|
post_install_message:
|
46
|
-
rdoc_options:
|
47
|
-
|
48
|
-
- README.txt
|
49
|
-
require_paths:
|
53
|
+
rdoc_options: []
|
54
|
+
require_paths:
|
50
55
|
- lib
|
51
|
-
required_ruby_version: !ruby/object:Gem::Requirement
|
52
|
-
requirements:
|
53
|
-
- -
|
54
|
-
- !ruby/object:Gem::Version
|
55
|
-
version:
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
version: "0"
|
62
|
-
version:
|
56
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
57
|
+
requirements:
|
58
|
+
- - '>='
|
59
|
+
- !ruby/object:Gem::Version
|
60
|
+
version: '0'
|
61
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
62
|
+
requirements:
|
63
|
+
- - '>='
|
64
|
+
- !ruby/object:Gem::Version
|
65
|
+
version: '0'
|
63
66
|
requirements: []
|
64
|
-
|
65
|
-
|
66
|
-
rubygems_version: 1.2.0
|
67
|
+
rubyforge_project:
|
68
|
+
rubygems_version: 2.0.3
|
67
69
|
signing_key:
|
68
|
-
specification_version:
|
70
|
+
specification_version: 4
|
69
71
|
summary: A generic paginator object for use in any Ruby program
|
70
|
-
test_files:
|
71
|
-
-
|
72
|
+
test_files:
|
73
|
+
- spec/paginator_spec.rb
|
data/History.txt
DELETED
@@ -1,43 +0,0 @@
|
|
1
|
-
== 1.1.0 / 2007-10-12
|
2
|
-
|
3
|
-
* Paginator now mixes in Enumerable (supporting inject, etc, on pages)
|
4
|
-
* Page now mixes in Enumerable (supporting inject, etc, on items)
|
5
|
-
|
6
|
-
== 1.0.9 / 2007-02-17
|
7
|
-
|
8
|
-
* Modified Pager#number_of_pages to support mathn's monkeypatching of Fixnum#/
|
9
|
-
* Thanks, Paul King!
|
10
|
-
|
11
|
-
== 1.0.8 / 2006-11-02
|
12
|
-
|
13
|
-
* Added <tt>Page#first_item_number</tt> and <tt>Page#last_item_number</tt>
|
14
|
-
* Thanks, Quannon Au!
|
15
|
-
|
16
|
-
== 1.0.6 / 2006-11-02
|
17
|
-
|
18
|
-
* Added each_with_index to Paginator and Page classes
|
19
|
-
|
20
|
-
== 1.0.5 / 2006-10-27
|
21
|
-
|
22
|
-
* Fix to Rakefile (addresses #6304)
|
23
|
-
|
24
|
-
== 1.0.4 / 2006-10-27
|
25
|
-
|
26
|
-
* Documentation fix
|
27
|
-
|
28
|
-
== 1.0.3 / 2006-10-26
|
29
|
-
|
30
|
-
* Documentation fix
|
31
|
-
|
32
|
-
== 1.0.2 / 2006-10-25
|
33
|
-
|
34
|
-
* If the block passed during Paginator has an arity of 1, the number
|
35
|
-
of items per page will not be yielded to the block.
|
36
|
-
|
37
|
-
== 1.0.1 / 2006-10-24
|
38
|
-
|
39
|
-
* Made Paginator#page more accepting, and set it to default to 1
|
40
|
-
|
41
|
-
== 1.0.0 / 2006-10-23
|
42
|
-
|
43
|
-
* Initial release
|
data/Manifest.txt
DELETED
data/README.txt
DELETED
@@ -1,84 +0,0 @@
|
|
1
|
-
paginator
|
2
|
-
by Bruce Williams
|
3
|
-
http://codefluency.com
|
4
|
-
|
5
|
-
== DESCRIPTION:
|
6
|
-
|
7
|
-
Paginator is a simple pagination class that provides a generic interface suitable
|
8
|
-
for use in any Ruby program.
|
9
|
-
|
10
|
-
== FEATURES/PROBLEMS:
|
11
|
-
|
12
|
-
Paginator doesn't make any assumptions as to how data is retrieved; you just
|
13
|
-
have to provide it with the total number of objects and a way to pull a specific
|
14
|
-
set of objects based on the offset and number of objects per page.
|
15
|
-
|
16
|
-
== SYNOPSIS:
|
17
|
-
|
18
|
-
In both of these examples I'm using a PER_PAGE constant (the number of items per page), but it's merely for labeling purposes.
|
19
|
-
|
20
|
-
You could, of course, just pass in the number of items per page directly to the initializer without assigning it somewhere beforehand.
|
21
|
-
|
22
|
-
=== In a Rails Application
|
23
|
-
|
24
|
-
def index
|
25
|
-
@pager = ::Paginator.new(Foo.count, PER_PAGE) do |offset, per_page|
|
26
|
-
Foo.find(:all, :limit => per_page, :offset => offset)
|
27
|
-
end
|
28
|
-
@page = @pager.page(params[:page])
|
29
|
-
# respond_to here if you want it
|
30
|
-
end
|
31
|
-
|
32
|
-
# In your view
|
33
|
-
<% @page.each do |foo| %>
|
34
|
-
<%# Show something for each item %>
|
35
|
-
<% end %>
|
36
|
-
<%= @page.number %>
|
37
|
-
<%= link_to("Prev", foos_url(:page => @page.prev.number)) if @page.prev? %>
|
38
|
-
<%= link_to("Next", foos_url(:page => @page.next.number)) if @page.next? %>
|
39
|
-
|
40
|
-
=== Anything else
|
41
|
-
|
42
|
-
bunch_o_data = (1..60).to_a
|
43
|
-
pager = Paginator.new(bunch_o_data.size, PER_PAGE) do |offset, per_page|
|
44
|
-
bunch_o_data[offset,per_page]
|
45
|
-
end
|
46
|
-
pager.each do |page|
|
47
|
-
puts "Page ##{page.number}"
|
48
|
-
page.each do |item|
|
49
|
-
puts item
|
50
|
-
end
|
51
|
-
end
|
52
|
-
|
53
|
-
== REQUIREMENTS:
|
54
|
-
|
55
|
-
None.
|
56
|
-
|
57
|
-
== INSTALL:
|
58
|
-
|
59
|
-
No special instructions.
|
60
|
-
|
61
|
-
== LICENSE:
|
62
|
-
|
63
|
-
(The MIT License)
|
64
|
-
|
65
|
-
Copyright (c) 2006-2007 Bruce Williams (http://codefluency.com)
|
66
|
-
|
67
|
-
Permission is hereby granted, free of charge, to any person obtaining
|
68
|
-
a copy of this software and associated documentation files (the
|
69
|
-
'Software'), to deal in the Software without restriction, including
|
70
|
-
without limitation the rights to use, copy, modify, merge, publish,
|
71
|
-
distribute, sublicense, and/or sell copies of the Software, and to
|
72
|
-
permit persons to whom the Software is furnished to do so, subject to
|
73
|
-
the following conditions:
|
74
|
-
|
75
|
-
The above copyright notice and this permission notice shall be
|
76
|
-
included in all copies or substantial portions of the Software.
|
77
|
-
|
78
|
-
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
|
79
|
-
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
80
|
-
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
81
|
-
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
82
|
-
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
83
|
-
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
84
|
-
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/bin/paginator
DELETED
File without changes
|