just_paginate 0.1.1 → 0.2.1
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +14 -9
- data/lib/just_paginate.rb +7 -6
- data/test/just_paginate_test.rb +7 -5
- metadata +52 -63
data/README.md
CHANGED
@@ -6,7 +6,7 @@ just_paginate is a framework-agnostic approach to creating paginated
|
|
6
6
|
webpages. It has no external dependencies and works with any web
|
7
7
|
framework.
|
8
8
|
|
9
|
-
Consists of two main methods, paginate and page_navigation.
|
9
|
+
Consists of two main methods, paginate and page_navigation.
|
10
10
|
|
11
11
|
JustPaginate.paginate helps you slice up your collections and, given a
|
12
12
|
page number, entities per page and totoal entity count, gives you the
|
@@ -37,21 +37,26 @@ Say we have a Rails app, with an index page of paginated Projects. You
|
|
37
37
|
could do this in the controller, to select the 20 projects for the
|
38
38
|
current page:
|
39
39
|
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
40
|
+
```rb
|
41
|
+
page = JustPaginate.page_value(params[:page])
|
42
|
+
project_count = Project.count
|
43
|
+
projects, total_pages = JustPaginate.paginate(page, 20, project_count) do |index_range|
|
44
|
+
Project.all.slice(index_range)
|
45
|
+
end
|
46
|
+
```
|
45
47
|
|
46
48
|
And in the index.html.erb file, to generate the page navigation:
|
47
49
|
|
48
|
-
|
49
|
-
|
50
|
+
```erb
|
51
|
+
<%= JustPaginate.page_navigation(page, total_pages) { |page_no| "/projects/?page=#{page_no}" } -%>
|
52
|
+
```
|
50
53
|
|
51
54
|
INSTALL:
|
52
55
|
========
|
53
56
|
|
54
|
-
|
57
|
+
```sh
|
58
|
+
sudo gem install just_paginate
|
59
|
+
```
|
55
60
|
|
56
61
|
Or simply stick just_paginate into your Gemfile and use Bundler to
|
57
62
|
pull it down.
|
data/lib/just_paginate.rb
CHANGED
@@ -23,8 +23,7 @@
|
|
23
23
|
# SOFTWARE.
|
24
24
|
#++
|
25
25
|
module JustPaginate
|
26
|
-
|
27
|
-
VERSION = "0.1.1"
|
26
|
+
VERSION = "0.2.1"
|
28
27
|
|
29
28
|
# TODO make sure negative numbers, non-integers etc are just converted to page 1.
|
30
29
|
def self.page_value(page)
|
@@ -49,17 +48,19 @@ module JustPaginate
|
|
49
48
|
def self.index_range(curr_page, per_page, total_entry_count)
|
50
49
|
start_index = ((curr_page-1)*per_page)
|
51
50
|
end_index = (start_index+per_page)-1
|
51
|
+
error_prefix = "Page #{curr_page} out of bounds"
|
52
52
|
|
53
53
|
if total_entry_count == 0
|
54
|
+
raise RangeError.new("#{error_prefix}, collection has 0 entries") if start_index != 0
|
54
55
|
return 0..0
|
55
56
|
end
|
56
57
|
|
57
|
-
if
|
58
|
-
|
59
|
-
|
58
|
+
if curr_page < 1 || start_index > (total_entry_count - 1)
|
59
|
+
page_count = (total_entry_count.to_f / per_page).ceil
|
60
|
+
raise RangeError.new("#{error_prefix} from #{page_count} pages")
|
60
61
|
end
|
61
62
|
|
62
|
-
if end_index>total_entry_count
|
63
|
+
if end_index > total_entry_count
|
63
64
|
end_index = total_entry_count
|
64
65
|
end
|
65
66
|
|
data/test/just_paginate_test.rb
CHANGED
@@ -94,14 +94,16 @@ describe JustPaginate do
|
|
94
94
|
end
|
95
95
|
end
|
96
96
|
|
97
|
-
it "
|
97
|
+
it "raises exception when out of bounds" do
|
98
98
|
assert_equal 2..3, JustPaginate.index_range(2,2,4)
|
99
|
-
|
100
|
-
|
99
|
+
assert_raises(RangeError) { JustPaginate.index_range(3,2,4) }
|
100
|
+
assert_raises(RangeError) { JustPaginate.index_range(4,2,4) }
|
101
|
+
assert_raises(RangeError) { JustPaginate.index_range(0,2,4) }
|
102
|
+
assert_raises(RangeError) { JustPaginate.index_range(10,30,0) }
|
101
103
|
end
|
102
104
|
|
103
|
-
it "returns 0-0 range for empty collection" do
|
104
|
-
assert_equal 0..0, JustPaginate.index_range(
|
105
|
+
it "returns 0-0 range for first page in empty collection" do
|
106
|
+
assert_equal 0..0, JustPaginate.index_range(1,2,0)
|
105
107
|
end
|
106
108
|
|
107
109
|
describe "The frontend pagination html helper" do
|
metadata
CHANGED
@@ -1,61 +1,56 @@
|
|
1
|
-
--- !ruby/object:Gem::Specification
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
2
|
name: just_paginate
|
3
|
-
version: !ruby/object:Gem::Version
|
4
|
-
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.2.1
|
5
5
|
prerelease:
|
6
|
-
segments:
|
7
|
-
- 0
|
8
|
-
- 1
|
9
|
-
- 1
|
10
|
-
version: 0.1.1
|
11
6
|
platform: ruby
|
12
|
-
authors:
|
7
|
+
authors:
|
13
8
|
- Gitorious AS
|
14
9
|
autorequire:
|
15
10
|
bindir: bin
|
16
11
|
cert_chain: []
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
- !ruby/object:Gem::Dependency
|
12
|
+
date: 2013-09-04 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
21
15
|
name: minitest
|
22
|
-
|
23
|
-
requirement: &id001 !ruby/object:Gem::Requirement
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
24
17
|
none: false
|
25
|
-
requirements:
|
18
|
+
requirements:
|
26
19
|
- - ~>
|
27
|
-
- !ruby/object:Gem::Version
|
28
|
-
|
29
|
-
segments:
|
30
|
-
- 2
|
31
|
-
- 0
|
32
|
-
version: "2.0"
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '2.0'
|
33
22
|
type: :development
|
34
|
-
version_requirements: *id001
|
35
|
-
- !ruby/object:Gem::Dependency
|
36
|
-
name: rake
|
37
23
|
prerelease: false
|
38
|
-
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ~>
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '2.0'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: rake
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
39
33
|
none: false
|
40
|
-
requirements:
|
41
|
-
- -
|
42
|
-
- !ruby/object:Gem::Version
|
43
|
-
|
44
|
-
segments:
|
45
|
-
- 0
|
46
|
-
version: "0"
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
47
38
|
type: :development
|
48
|
-
|
49
|
-
|
50
|
-
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
description: Framework-agnostic support for paginating collections of things, and
|
47
|
+
linking to paginated things in your webpage
|
48
|
+
email:
|
51
49
|
- support@gitorious.org
|
52
50
|
executables: []
|
53
|
-
|
54
51
|
extensions: []
|
55
|
-
|
56
52
|
extra_rdoc_files: []
|
57
|
-
|
58
|
-
files:
|
53
|
+
files:
|
59
54
|
- .gitignore
|
60
55
|
- Gemfile
|
61
56
|
- README.md
|
@@ -66,36 +61,30 @@ files:
|
|
66
61
|
- test/test_helper.rb
|
67
62
|
homepage: https://gitorious.org/gitorious/just_paginate
|
68
63
|
licenses: []
|
69
|
-
|
70
64
|
post_install_message:
|
71
65
|
rdoc_options: []
|
72
|
-
|
73
|
-
require_paths:
|
66
|
+
require_paths:
|
74
67
|
- lib
|
75
|
-
required_ruby_version: !ruby/object:Gem::Requirement
|
68
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
76
69
|
none: false
|
77
|
-
requirements:
|
78
|
-
- -
|
79
|
-
- !ruby/object:Gem::Version
|
80
|
-
|
81
|
-
|
82
|
-
- 0
|
83
|
-
version: "0"
|
84
|
-
required_rubygems_version: !ruby/object:Gem::Requirement
|
70
|
+
requirements:
|
71
|
+
- - ! '>='
|
72
|
+
- !ruby/object:Gem::Version
|
73
|
+
version: '0'
|
74
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
85
75
|
none: false
|
86
|
-
requirements:
|
87
|
-
- -
|
88
|
-
- !ruby/object:Gem::Version
|
89
|
-
|
90
|
-
segments:
|
91
|
-
- 0
|
92
|
-
version: "0"
|
76
|
+
requirements:
|
77
|
+
- - ! '>='
|
78
|
+
- !ruby/object:Gem::Version
|
79
|
+
version: '0'
|
93
80
|
requirements: []
|
94
|
-
|
95
81
|
rubyforge_project: just_paginate
|
96
82
|
rubygems_version: 1.8.25
|
97
83
|
signing_key:
|
98
84
|
specification_version: 3
|
99
|
-
summary: Framework-agnostic support for paginating collections of things, and linking
|
100
|
-
|
101
|
-
|
85
|
+
summary: Framework-agnostic support for paginating collections of things, and linking
|
86
|
+
to paginated things in your webpage
|
87
|
+
test_files:
|
88
|
+
- test/just_paginate_test.rb
|
89
|
+
- test/test_helper.rb
|
90
|
+
has_rdoc:
|