taylorbarstow-simple_pagination 0.1.1 → 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
data/VERSION.yml
CHANGED
@@ -3,8 +3,9 @@ module SimplePagination
|
|
3
3
|
##
|
4
4
|
# Create a new PageCollection object. The following options are available.
|
5
5
|
# [:current_offset] The zero-based offset of the first record in the current page of results
|
6
|
-
# [:current_page] The one-based offset of the current page of results
|
7
|
-
# [:
|
6
|
+
# [:current_page] The one-based offset of the current page of results (alternative to :current_offset)
|
7
|
+
# [:total_records] The total number of results across all pages
|
8
|
+
# [:total_pages] The total number of pages (alternative to :total_records)
|
8
9
|
# [:page_size] The page size
|
9
10
|
#
|
10
11
|
# You can choose between :current_offset and :current_page based on your
|
@@ -17,7 +18,7 @@ module SimplePagination
|
|
17
18
|
def initialize(options={})
|
18
19
|
check_options(options)
|
19
20
|
@pages, @page_size = {}, options[:page_size]
|
20
|
-
@length = options[:total_pages] || options[:total_records] / @page_size
|
21
|
+
@length = options[:total_pages] || (options[:total_records] / @page_size.to_f).ceil
|
21
22
|
@current_page = options[:current_page] || (options[:current_offset]/@page_size)+1
|
22
23
|
end
|
23
24
|
|
@@ -58,7 +59,7 @@ module SimplePagination
|
|
58
59
|
def check_options(options)
|
59
60
|
raise ArgumentError, "requires :current_offset or :current_page" unless options[:current_offset] || options[:current_page]
|
60
61
|
raise ArgumentError, "requires :page_size" unless options[:page_size]
|
61
|
-
raise ArgumentError, "requires :
|
62
|
+
raise ArgumentError, "requires :total_pages or :total_records" unless options[:total_pages] or options[:total_records]
|
62
63
|
end
|
63
64
|
|
64
65
|
def out_of_bounds?(i)
|
@@ -25,26 +25,40 @@ class PageCollectionTest < Test::Unit::TestCase
|
|
25
25
|
end
|
26
26
|
end
|
27
27
|
|
28
|
-
context "
|
29
|
-
setup do
|
30
|
-
@collection = PageCollection.new(:total_pages => 10, :page_size => 10, :current_offset => 0)
|
31
|
-
end
|
32
|
-
|
28
|
+
context "page_size" do
|
33
29
|
should "provide page_size accessor" do
|
30
|
+
@collection = PageCollection.new(:total_pages => 10, :page_size => 10, :current_offset => 0)
|
34
31
|
assert_equal 10, @collection.page_size
|
35
32
|
end
|
36
|
-
|
37
|
-
|
38
|
-
|
33
|
+
end
|
34
|
+
|
35
|
+
context "length" do
|
36
|
+
context "when given total_pages" do
|
37
|
+
should "be equal to total_pages" do
|
38
|
+
@collection = PageCollection.new(:total_pages => 10, :page_size => 10, :current_offset => 0)
|
39
|
+
assert_equal 10, @collection.length
|
40
|
+
end
|
39
41
|
end
|
40
42
|
|
41
|
-
|
42
|
-
|
43
|
-
|
43
|
+
context "when given total_records" do
|
44
|
+
should "be zero when total_records is zero" do
|
45
|
+
@collection = PageCollection.new(:total_records => 0, :page_size => 10, :current_offset => 0)
|
46
|
+
assert_equal 0, @collection.length
|
47
|
+
end
|
48
|
+
|
49
|
+
should "be one if total_records is less than page_size results" do
|
50
|
+
@collection = PageCollection.new(:total_records => 5, :page_size => 10, :current_offset => 0)
|
51
|
+
assert_equal 1, @collection.length
|
52
|
+
end
|
53
|
+
|
54
|
+
should "be ceil(total_records / page_size)" do
|
55
|
+
@collection = PageCollection.new(:total_records => 21, :page_size => 10, :current_offset => 0)
|
56
|
+
assert_equal 3, @collection.length
|
57
|
+
end
|
44
58
|
end
|
45
59
|
end
|
46
60
|
|
47
|
-
context "accessing a
|
61
|
+
context "accessing a numbered page" do
|
48
62
|
setup do
|
49
63
|
@collection = PageCollection.new(:total_pages => 10, :page_size => 10, :current_page=>1)
|
50
64
|
end
|
@@ -69,31 +83,50 @@ class PageCollectionTest < Test::Unit::TestCase
|
|
69
83
|
end
|
70
84
|
end
|
71
85
|
|
72
|
-
context "
|
73
|
-
|
74
|
-
|
86
|
+
context "current page" do
|
87
|
+
context "when given current_page" do
|
88
|
+
should "provide page with index == current_page" do
|
89
|
+
@collection = PageCollection.new(:total_pages => 10, :page_size => 10, :current_page => 3)
|
90
|
+
@collection.expects(:[]).with(3)
|
91
|
+
@collection.current
|
92
|
+
end
|
75
93
|
end
|
76
94
|
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
95
|
+
context "when given current_offset" do
|
96
|
+
should "provide page 1 if current_offset is zero" do
|
97
|
+
@collection = PageCollection.new(:total_pages => 10, :page_size => 10, :current_offset => 0)
|
98
|
+
@collection.expects(:[]).with(1)
|
99
|
+
@collection.current
|
100
|
+
end
|
101
|
+
|
102
|
+
should "provide page with index == floor(current_offset / page_size) + 1" do
|
103
|
+
@collection = PageCollection.new(:total_pages => 10, :page_size => 10, :current_offset => 21)
|
104
|
+
@collection.expects(:[]).with(3)
|
105
|
+
@collection.current
|
82
106
|
end
|
83
107
|
end
|
84
|
-
|
85
|
-
|
108
|
+
end
|
109
|
+
|
110
|
+
context "first page" do
|
111
|
+
should "provide page 1" do
|
112
|
+
@collection = PageCollection.new(:total_pages => 10, :page_size => 10, :current_page => 5)
|
86
113
|
@collection.expects(:[]).with(1)
|
87
114
|
@collection.first
|
88
115
|
end
|
116
|
+
end
|
117
|
+
|
118
|
+
context "last page" do
|
119
|
+
# this is not an exhaustive test but I shouldn't need to make it so since
|
120
|
+
# the tests for length above do this...
|
89
121
|
|
90
|
-
should "
|
122
|
+
should "provide page with index == length" do
|
123
|
+
@collection = PageCollection.new(:total_pages => 10, :page_size => 10, :current_page => 5)
|
91
124
|
@collection.expects(:[]).with(10)
|
92
125
|
@collection.last
|
93
126
|
|
94
|
-
|
95
|
-
|
96
|
-
|
127
|
+
@collection = PageCollection.new(:total_records => 21, :page_size => 10, :current_offset => 0)
|
128
|
+
@collection.expects(:[]).with(3)
|
129
|
+
@collection.last
|
97
130
|
end
|
98
131
|
end
|
99
132
|
end
|