folio-pagination-legacy 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (38) hide show
  1. data/.gitignore +17 -0
  2. data/.travis.yml +3 -0
  3. data/Gemfile +16 -0
  4. data/Guardfile +5 -0
  5. data/LICENSE.txt +22 -0
  6. data/README.md +235 -0
  7. data/Rakefile +11 -0
  8. data/folio-pagination-legacy.gemspec +30 -0
  9. data/lib/folio-pagination-legacy.rb +3 -0
  10. data/lib/folio.rb +84 -0
  11. data/lib/folio/core_ext/enumerable.rb +51 -0
  12. data/lib/folio/invalid_page.rb +5 -0
  13. data/lib/folio/ordinal.rb +33 -0
  14. data/lib/folio/ordinal/page.rb +107 -0
  15. data/lib/folio/page.rb +86 -0
  16. data/lib/folio/per_page.rb +15 -0
  17. data/lib/folio/rails.rb +21 -0
  18. data/lib/folio/version.rb +3 -0
  19. data/lib/folio/will_paginate/active_record.rb +71 -0
  20. data/lib/folio/will_paginate/array.rb +10 -0
  21. data/lib/folio/will_paginate/collection.rb +19 -0
  22. data/lib/folio/will_paginate/view_helpers.rb +59 -0
  23. data/lib/folio/will_paginate/view_helpers/action_view.rb +10 -0
  24. data/lib/folio/will_paginate/view_helpers/link_renderer.rb +53 -0
  25. data/test/folio/core_ext/enumerable_test.rb +55 -0
  26. data/test/folio/ordinal/page_test.rb +173 -0
  27. data/test/folio/ordinal_test.rb +57 -0
  28. data/test/folio/page_test.rb +198 -0
  29. data/test/folio/per_page_test.rb +49 -0
  30. data/test/folio/rails_test.rb +11 -0
  31. data/test/folio/version_test.rb +8 -0
  32. data/test/folio/will_paginate/active_record_test.rb +113 -0
  33. data/test/folio/will_paginate/collection_test.rb +43 -0
  34. data/test/folio/will_paginate/view_helpers/link_renderer_test.rb +160 -0
  35. data/test/folio/will_paginate/view_helpers_test.rb +49 -0
  36. data/test/folio_test.rb +192 -0
  37. data/test/setup/active_record.rb +13 -0
  38. metadata +133 -0
@@ -0,0 +1,10 @@
1
+ begin
2
+ require 'will_paginate/view_helpers/action_view'
3
+ rescue LoadError
4
+ raise "folio-pagination's actionview support requires will_paginate"
5
+ end
6
+
7
+ require 'folio/will_paginate/view_helpers'
8
+
9
+ # no overrides specific to action view necessary. just including the general
10
+ # view_helper overrides as above is sufficient.
@@ -0,0 +1,53 @@
1
+ require 'active_support'
2
+ require 'will_paginate/view_helpers'
3
+
4
+ module Folio
5
+ module WillPaginate
6
+ module ViewHelpers
7
+ module LinkRenderer
8
+ def prepare_with_folio(collection, options, template)
9
+ # only include page_links if we're in a collection with ordinal
10
+ # pages; otherwise stick to just prev/next.
11
+ options = options.merge(page_links: false) unless collection.ordinal_pages?
12
+ prepare_without_folio(collection, options, template)
13
+ end
14
+
15
+ def windowed_links_with_folio
16
+ links = windowed_links_without_folio
17
+ unless @collection.last_page
18
+ # the last page is not known, so add a trailing gap
19
+ links << gap_marker
20
+ end
21
+ links
22
+ end
23
+
24
+ def total_pages_with_folio
25
+ # the collection may not have a known last page. if so, there must be
26
+ # a next page; count that as the last known page. it's ok to use
27
+ # these page identifiers as a page count because (after fixing
28
+ # LinkRenderer) it's only called when ordinal_pages is true.
29
+ @collection.last_page || @collection.next_page
30
+ end
31
+
32
+ def rel_value_with_folio(page)
33
+ # don't check against mathed out values, just check the values on the
34
+ # collection
35
+ rels = []
36
+ rels << 'prev' if page == @collection.previous_page
37
+ rels << 'next' if page == @collection.next_page
38
+ rels << 'start' if page == @collection.first_page
39
+ rels.empty? ? nil : rels.join(' ')
40
+ end
41
+
42
+ def self.included(klass)
43
+ [:prepare, :windowed_links, :total_pages, :rel_value].each do |method|
44
+ klass.send(:alias_method, :"#{method}_without_folio", method)
45
+ klass.send(:alias_method, method, :"#{method}_with_folio")
46
+ end
47
+ end
48
+
49
+ ::WillPaginate::LinkRenderer.send :include, self
50
+ end
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,55 @@
1
+ require 'minitest/autorun'
2
+ require 'folio/core_ext/enumerable'
3
+
4
+ describe Array do
5
+ it "should be an ordinal folio" do
6
+ assert_respond_to [], :build_page
7
+ [].build_page.first_page.must_equal 1
8
+ end
9
+
10
+ describe "per_page" do
11
+ it "should have per_page attribute as expected on instances" do
12
+ assert_respond_to [], :per_page
13
+ assert_respond_to [], :per_page=
14
+ end
15
+
16
+ it "should not have per_page attribute on classes" do
17
+ Array.wont_respond_to :per_page
18
+ Array.wont_respond_to :per_page=
19
+ end
20
+
21
+ it "should default to Folio.per_page" do
22
+ was = Folio.per_page
23
+ Folio.per_page = 100
24
+ [].per_page.must_equal 100
25
+ Folio.per_page = was
26
+ end
27
+ end
28
+
29
+ describe "paginate" do
30
+ before do
31
+ @ary = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
32
+ end
33
+
34
+ it "should return a page" do
35
+ page = @ary.paginate
36
+ assert_respond_to page, :current_page
37
+ end
38
+
39
+ it "should return appropriate slice" do
40
+ @ary.paginate(page: 2, per_page: 4).must_equal [5, 6, 7, 8]
41
+ end
42
+
43
+ it "should return partial slice at end" do
44
+ @ary.paginate(page: 3, per_page: 4).must_equal [9, 10]
45
+ end
46
+
47
+ it "should raise InvalidPage after end even if size unknown" do
48
+ lambda{ @ary.paginate(page: 3, per_page: 5, total_entries: nil) }.must_raise Folio::InvalidPage
49
+ end
50
+
51
+ it "should return empty first page if empty" do
52
+ [].paginate.must_equal []
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,173 @@
1
+ require 'minitest/autorun'
2
+ require 'folio/ordinal'
3
+
4
+ describe Folio::Ordinal::Page do
5
+ before do
6
+ @page = Folio::Ordinal::Page.create
7
+ @page.current_page = 1
8
+ @page.per_page = 10
9
+ @page.total_entries = 30
10
+ end
11
+
12
+ it "should have ordinal_pages=true" do
13
+ @page.ordinal_pages.must_equal true
14
+ @page.ordinal_pages?.must_equal true
15
+ end
16
+
17
+ it "should have first_page=1" do
18
+ @page.first_page.must_equal 1
19
+ end
20
+
21
+ it "should force current_page to an integer" do
22
+ @page.current_page = "3"
23
+ @page.current_page.must_equal 3
24
+ end
25
+
26
+ describe "when total pages known" do
27
+ it "should have last_page=total_pages" do
28
+ @page.last_page.must_equal @page.total_pages
29
+
30
+ @page.total_entries = 0
31
+ @page.last_page.must_equal @page.total_pages
32
+
33
+ @page.total_entries = nil
34
+ @page.last_page.must_equal @page.total_pages
35
+ end
36
+
37
+ it "should have next_page=current_page+1 when not at end" do
38
+ @page.current_page = 2
39
+ @page.next_page.must_equal 3
40
+ end
41
+
42
+ it "should still have next_page=current_page+1 when not at end, despite set value" do
43
+ @page.current_page = 2
44
+ @page.next_page = nil
45
+ @page.next_page.must_equal 3
46
+ end
47
+
48
+ it "should have next_page=nil when at end" do
49
+ @page.current_page = 3
50
+ @page.next_page.must_be_nil
51
+ end
52
+
53
+ it "should still have next_page=nil when at end, despite set value" do
54
+ @page.current_page = 3
55
+ @page.next_page = 4
56
+ @page.next_page.must_be_nil
57
+ end
58
+ end
59
+
60
+ describe "when total pages not known" do
61
+ before do
62
+ @page.total_entries = nil
63
+ end
64
+
65
+ it "should have last_page=nil if next_page is known or assumed" do
66
+ # @page.next_page unset
67
+ @page.last_page.must_be_nil
68
+
69
+ # @page.next_page explicitly non-nil
70
+ @page.next_page = 2
71
+ @page.last_page.must_be_nil
72
+ end
73
+
74
+ it "should have last_page=current_page if next_page is explicitly nil" do
75
+ @page.next_page = nil
76
+ @page.last_page.must_equal @page.current_page
77
+ end
78
+
79
+ it "should have next_page=current_page+1 when not explicitly set" do
80
+ @page.current_page = 2
81
+ @page.next_page.must_equal 3
82
+ end
83
+
84
+ it "should force non-nil set next_page to an integer" do
85
+ @page.next_page = "4"
86
+ @page.next_page.must_equal 4
87
+ end
88
+
89
+ it "should not force nil set next_page to an integer" do
90
+ @page.next_page = nil
91
+ @page.next_page.must_be_nil
92
+ end
93
+
94
+ it "should have next_page=set value when explicitly set" do
95
+ @page.next_page = nil
96
+ @page.current_page = 2
97
+ @page.next_page.must_be_nil
98
+ end
99
+ end
100
+
101
+ it "should have previous_page=nil when at beginning" do
102
+ @page.current_page = 1
103
+ @page.previous_page.must_be_nil
104
+ end
105
+
106
+ it "should have previous_page=current_page-1 when not at beginning" do
107
+ @page.current_page = 3
108
+ @page.previous_page.must_equal 2
109
+ end
110
+
111
+ describe "out_of_bounds?" do
112
+ it "should be false if in first_page..last_page range" do
113
+ @page.current_page = 1
114
+ @page.out_of_bounds?.must_equal false
115
+
116
+ @page.current_page = 3
117
+ @page.out_of_bounds?.must_equal false
118
+ end
119
+
120
+ it "should be true if negative page" do
121
+ @page.current_page = -1
122
+ @page.out_of_bounds?.must_equal true
123
+ end
124
+
125
+ it "should be true if zero page" do
126
+ @page.current_page = 0
127
+ @page.out_of_bounds?.must_equal true
128
+ end
129
+
130
+ it "should be true after known last_page" do
131
+ @page.current_page = 4
132
+ @page.out_of_bounds?.must_equal true
133
+ end
134
+
135
+ it "should not care about large numbers when total_pages not known" do
136
+ @page.total_entries = nil
137
+ @page.current_page = 50
138
+ @page.out_of_bounds?.must_equal false
139
+ end
140
+ end
141
+
142
+ describe "decorate" do
143
+ before do
144
+ @page = Folio::Ordinal::Page.decorate([])
145
+ end
146
+
147
+ it "should add page methods to the object" do
148
+ assert_respond_to @page, :current_page
149
+ end
150
+
151
+ it "should add ordinal page methods to the object" do
152
+ @page.first_page.must_equal 1
153
+ end
154
+
155
+ it "should preserve other methods on the object" do
156
+ assert_respond_to @page, :each
157
+ end
158
+ end
159
+
160
+ describe "create" do
161
+ before do
162
+ @page = Folio::Ordinal::Page.create
163
+ end
164
+
165
+ it "should be an Array at heart" do
166
+ @page.must_be :is_a?, Array
167
+ end
168
+
169
+ it "should be decorated as an ordinal page" do
170
+ @page.first_page.must_equal 1
171
+ end
172
+ end
173
+ end
@@ -0,0 +1,57 @@
1
+ require 'minitest/autorun'
2
+ require 'folio/ordinal'
3
+
4
+ describe Folio::Ordinal do
5
+ before do
6
+ klass = Class.new do
7
+ def build_page
8
+ Folio::Page.create
9
+ end
10
+
11
+ def fill_page(page)
12
+ page
13
+ end
14
+
15
+ include Folio::Ordinal
16
+ end
17
+ @folio = klass.new
18
+ end
19
+
20
+ describe "paginate" do
21
+ it "should decorate the result of build_page" do
22
+ @page = @folio.paginate
23
+ @page.is_a?(Folio::Ordinal::Page).must_equal true
24
+ end
25
+
26
+ describe "bounds checking" do
27
+ before do
28
+ @folio.per_page = 10
29
+ end
30
+
31
+ it "should raise on non-integer page" do
32
+ lambda{ @folio.paginate(page: "non-integer") }.must_raise Folio::InvalidPage
33
+ end
34
+
35
+ it "should raise on negative page" do
36
+ lambda{ @folio.paginate(page: -1) }.must_raise Folio::InvalidPage
37
+ end
38
+
39
+ it "should raise on page of 0" do
40
+ lambda{ @folio.paginate(page: 0) }.must_raise Folio::InvalidPage
41
+ end
42
+
43
+ it "should raise on page greater than known last_page" do
44
+ lambda{ @folio.paginate(page: 4, total_entries: 30) }.must_raise Folio::InvalidPage
45
+ end
46
+
47
+ it "should not raise on page number between first_page and known last_page" do
48
+ @folio.paginate(page: 1, total_entries: 30)
49
+ @folio.paginate(page: 3, total_entries: 30)
50
+ end
51
+
52
+ it "should not raise on large page if last_page unknown" do
53
+ @folio.paginate(page: 100)
54
+ end
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,198 @@
1
+ require 'minitest/autorun'
2
+ require 'folio/page'
3
+ require 'folio'
4
+
5
+ describe Folio::Page do
6
+ before do
7
+ @page = Folio::Page.create
8
+ end
9
+
10
+ describe "ordinal_pages" do
11
+ it "should have ordinal_pages accessors" do
12
+ assert_respond_to @page, :ordinal_pages
13
+ assert_respond_to @page, :ordinal_pages=
14
+ assert_respond_to @page, :ordinal_pages?
15
+ end
16
+
17
+ it "should mutate with ordinal_pages=" do
18
+ @page.ordinal_pages = true
19
+ @page.ordinal_pages.must_equal true
20
+ end
21
+
22
+ it "should alias reader as predicate" do
23
+ @page.ordinal_pages = true
24
+ @page.ordinal_pages?.must_equal true
25
+ end
26
+ end
27
+
28
+ describe "current_page" do
29
+ it "should have current_page accessors" do
30
+ assert_respond_to @page, :current_page
31
+ assert_respond_to @page, :current_page=
32
+ end
33
+
34
+ it "should mutate with current_page=" do
35
+ @page.current_page = 3
36
+ @page.current_page.must_equal 3
37
+ end
38
+ end
39
+
40
+ describe "per_page" do
41
+ it "should have per_page accessors" do
42
+ assert_respond_to @page, :per_page
43
+ assert_respond_to @page, :per_page=
44
+ end
45
+
46
+ it "should mutate with per_page=" do
47
+ @page.per_page = 3
48
+ @page.per_page.must_equal 3
49
+ end
50
+
51
+ it "should default to Folio.per_page" do
52
+ was = Folio.per_page
53
+ Folio.per_page = 100
54
+ @page.per_page.must_equal Folio.per_page
55
+ Folio.per_page = was
56
+ end
57
+ end
58
+
59
+ describe "first_page" do
60
+ it "should have first_page accessors" do
61
+ assert_respond_to @page, :first_page
62
+ assert_respond_to @page, :first_page=
63
+ end
64
+
65
+ it "should mutate with first_page=" do
66
+ @page.first_page = 3
67
+ @page.first_page.must_equal 3
68
+ end
69
+ end
70
+
71
+ describe "last_page" do
72
+ it "should have last_page accessors" do
73
+ assert_respond_to @page, :last_page
74
+ assert_respond_to @page, :last_page=
75
+ end
76
+
77
+ it "should mutate with last_page= when next_page is non-nil" do
78
+ @page.next_page = 3
79
+ @page.last_page = 5
80
+ @page.last_page.must_equal 5
81
+ end
82
+
83
+ it "should default last_page=nil if unset and next_page is non-nil" do
84
+ @page.current_page = 2
85
+ @page.next_page = 3
86
+ @page.last_page.must_be_nil
87
+ end
88
+
89
+ it "should default last_page=current_page if unset and next_page is nil" do
90
+ end
91
+
92
+ it "should have last_page=current_page if next_page is nil, even if explicitly set" do
93
+ @page.current_page = 2
94
+ @page.last_page.must_equal 2
95
+
96
+ @page.last_page = 4
97
+ @page.last_page.must_equal 2
98
+
99
+ @page.last_page = nil
100
+ @page.last_page.must_equal 2
101
+ end
102
+ end
103
+
104
+ describe "next_page" do
105
+ it "should have next_page accessors" do
106
+ assert_respond_to @page, :next_page
107
+ assert_respond_to @page, :next_page=
108
+ end
109
+
110
+ it "should mutate with next_page=" do
111
+ @page.next_page = 3
112
+ @page.next_page.must_equal 3
113
+ end
114
+ end
115
+
116
+ describe "previous_page" do
117
+ it "should have previous_page accessors" do
118
+ assert_respond_to @page, :previous_page
119
+ assert_respond_to @page, :previous_page=
120
+ end
121
+
122
+ it "should mutate with previous_page=" do
123
+ @page.previous_page = 3
124
+ @page.previous_page.must_equal 3
125
+ end
126
+ end
127
+
128
+ describe "total_entries" do
129
+ it "should have total_entries accessors" do
130
+ assert_respond_to @page, :total_entries
131
+ assert_respond_to @page, :total_entries=
132
+ end
133
+
134
+ it "should mutate with total_entries=" do
135
+ @page.total_entries = 3
136
+ @page.total_entries.must_equal 3
137
+ end
138
+ end
139
+
140
+ describe "total_pages" do
141
+ before do
142
+ @page.total_entries = 10
143
+ @page.per_page = 5
144
+ end
145
+
146
+ it "should be nil if total_entries is nil" do
147
+ @page.total_entries = nil
148
+ @page.total_pages.must_be_nil
149
+ end
150
+
151
+ it "should be nil if per_page is 0" do
152
+ @page.per_page = 0
153
+ @page.total_pages.must_be_nil
154
+ end
155
+
156
+ it "should be 1 if total_entries is 0" do
157
+ @page.total_entries = 0
158
+ @page.total_pages.must_equal 1
159
+ end
160
+
161
+ it "should be total_entries/per_page if evenly divisible" do
162
+ @page.total_pages.must_equal 2
163
+ end
164
+
165
+ it "should round up if not evenly divisible" do
166
+ @page.per_page = 3
167
+ @page.total_pages.must_equal 4
168
+ end
169
+ end
170
+
171
+ describe "decorate" do
172
+ before do
173
+ @page = Folio::Page.decorate([])
174
+ end
175
+
176
+ it "should add page methods to the object" do
177
+ assert_respond_to @page, :current_page
178
+ end
179
+
180
+ it "should preserve other methods on the object" do
181
+ assert_respond_to @page, :each
182
+ end
183
+ end
184
+
185
+ describe "create" do
186
+ before do
187
+ @page = Folio::Page.create
188
+ end
189
+
190
+ it "should be an Array at heart" do
191
+ @page.must_be :is_a?, Array
192
+ end
193
+
194
+ it "should be decorated as a page" do
195
+ assert_respond_to @page, :current_page
196
+ end
197
+ end
198
+ end