just_paginate 0.1 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +2 -0
- data/Gemfile +2 -1
- data/Rakefile +32 -4
- data/just_paginate.gemspec +1 -6
- data/lib/just_paginate.rb +61 -2
- data/test/{test_just_paginate.rb → just_paginate_test.rb} +41 -17
- data/test/test_helper.rb +26 -5
- metadata +9 -35
data/.gitignore
CHANGED
data/Gemfile
CHANGED
data/Rakefile
CHANGED
@@ -1,7 +1,35 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
# --
|
3
|
+
# The MIT License (MIT)
|
4
|
+
#
|
5
|
+
# Copyright (C) 2013 Gitorious AS
|
6
|
+
#
|
7
|
+
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
8
|
+
# of this software and associated documentation files (the "Software"), to deal
|
9
|
+
# in the Software without restriction, including without limitation the rights
|
10
|
+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
11
|
+
# copies of the Software, and to permit persons to whom the Software is
|
12
|
+
# furnished to do so, subject to the following conditions:
|
13
|
+
#
|
14
|
+
# The above copyright notice and this permission notice shall be included in all
|
15
|
+
# copies or substantial portions of the Software.
|
16
|
+
#
|
17
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
18
|
+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
19
|
+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
20
|
+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
21
|
+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
22
|
+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
23
|
+
# SOFTWARE.
|
24
|
+
#++
|
25
|
+
require "rake/testtask"
|
26
|
+
require "ci/reporter/rake/minitest"
|
1
27
|
require "bundler/gem_tasks"
|
2
|
-
require 'rake/testtask'
|
3
28
|
|
4
|
-
Rake::TestTask.new do |
|
5
|
-
|
6
|
-
|
29
|
+
Rake::TestTask.new("test") do |test|
|
30
|
+
test.libs << "test"
|
31
|
+
test.pattern = "test/*_test.rb"
|
32
|
+
test.verbose = true
|
7
33
|
end
|
34
|
+
|
35
|
+
task :default => :test
|
data/just_paginate.gemspec
CHANGED
@@ -18,11 +18,6 @@ Gem::Specification.new do |s|
|
|
18
18
|
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
19
19
|
s.require_paths = ["lib"]
|
20
20
|
|
21
|
-
|
22
|
-
# s.add_development_dependency "rspec"
|
23
|
-
|
24
|
-
s.add_development_dependency "minitest"
|
25
|
-
s.add_development_dependency "shoulda"
|
26
|
-
s.add_development_dependency "shoulda-context"
|
21
|
+
s.add_development_dependency "minitest", "~> 2.0"
|
27
22
|
s.add_development_dependency "rake"
|
28
23
|
end
|
data/lib/just_paginate.rb
CHANGED
@@ -1,7 +1,30 @@
|
|
1
|
-
# -*- coding: utf-8
|
1
|
+
# -*- coding: utf-8 -*
|
2
|
+
# --
|
3
|
+
# The MIT License (MIT)
|
4
|
+
#
|
5
|
+
# Copyright (C) 2013 Gitorious AS
|
6
|
+
#
|
7
|
+
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
8
|
+
# of this software and associated documentation files (the "Software"), to deal
|
9
|
+
# in the Software without restriction, including without limitation the rights
|
10
|
+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
11
|
+
# copies of the Software, and to permit persons to whom the Software is
|
12
|
+
# furnished to do so, subject to the following conditions:
|
13
|
+
#
|
14
|
+
# The above copyright notice and this permission notice shall be included in all
|
15
|
+
# copies or substantial portions of the Software.
|
16
|
+
#
|
17
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
18
|
+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
19
|
+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
20
|
+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
21
|
+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
22
|
+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
23
|
+
# SOFTWARE.
|
24
|
+
#++
|
2
25
|
module JustPaginate
|
3
26
|
|
4
|
-
VERSION = "0.1"
|
27
|
+
VERSION = "0.1.1"
|
5
28
|
|
6
29
|
# TODO make sure negative numbers, non-integers etc are just converted to page 1.
|
7
30
|
def self.page_value(page)
|
@@ -62,6 +85,7 @@ module JustPaginate
|
|
62
85
|
end
|
63
86
|
end
|
64
87
|
|
88
|
+
# Depends on Bootstrap styling/widgets
|
65
89
|
def self.page_links(curr_page, total_page_count, &page_link_constructor)
|
66
90
|
page_labels(curr_page, total_page_count).map do |label|
|
67
91
|
page_element = ""
|
@@ -86,6 +110,41 @@ module JustPaginate
|
|
86
110
|
end.join(" ")
|
87
111
|
end
|
88
112
|
|
113
|
+
# Not dependent on bootstrap styling/widgets
|
114
|
+
def self.page_navigation_non_bootstrap(curr_page, total_page_count, &page_link_constructor)
|
115
|
+
if total_page_count > 1
|
116
|
+
links = page_links_non_bootstrap(curr_page.to_i, total_page_count, &page_link_constructor)
|
117
|
+
return "<div class='pagination'>#{links}</div>"
|
118
|
+
else
|
119
|
+
""
|
120
|
+
end
|
121
|
+
end
|
122
|
+
|
123
|
+
# Not dependent on bootstrap styling/widgets
|
124
|
+
def self.page_links_non_bootstrap(curr_page, total_page_count, &page_link_constructor)
|
125
|
+
page_labels(curr_page, total_page_count).map do |label|
|
126
|
+
page_element = ""
|
127
|
+
|
128
|
+
if label == "..."
|
129
|
+
page_element = "<span class='gap'>#{label}</span>"
|
130
|
+
elsif label == "<"
|
131
|
+
page_url = yield(curr_page-1)
|
132
|
+
page_element = "<a rel='prev' href='#{page_url}'>#{label}</a>"
|
133
|
+
elsif label == ">"
|
134
|
+
page_url = yield(curr_page+1)
|
135
|
+
page_element = "<a rel='next' href='#{page_url}'>#{label}</a>"
|
136
|
+
else
|
137
|
+
page_url = yield(label)
|
138
|
+
if label.to_i == curr_page
|
139
|
+
page_element = "<span class='current'>#{label}</span>"
|
140
|
+
else
|
141
|
+
page_element = "<a href='#{page_url}'>#{label}</a>"
|
142
|
+
end
|
143
|
+
end
|
144
|
+
|
145
|
+
end.join(" ")
|
146
|
+
end
|
147
|
+
|
89
148
|
def self.page_labels(current, total)
|
90
149
|
max_visible = 10
|
91
150
|
labels = []
|
@@ -1,9 +1,33 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
1
|
+
# encoding: utf-8
|
2
|
+
# --
|
3
|
+
# The MIT License (MIT)
|
4
|
+
#
|
5
|
+
# Copyright (C) 2013 Gitorious AS
|
6
|
+
#
|
7
|
+
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
8
|
+
# of this software and associated documentation files (the "Software"), to deal
|
9
|
+
# in the Software without restriction, including without limitation the rights
|
10
|
+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
11
|
+
# copies of the Software, and to permit persons to whom the Software is
|
12
|
+
# furnished to do so, subject to the following conditions:
|
13
|
+
#
|
14
|
+
# The above copyright notice and this permission notice shall be included in all
|
15
|
+
# copies or substantial portions of the Software.
|
16
|
+
#
|
17
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
18
|
+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
19
|
+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
20
|
+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
21
|
+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
22
|
+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
23
|
+
# SOFTWARE.
|
24
|
+
#++
|
25
|
+
require "test_helper"
|
26
|
+
require "just_paginate"
|
27
|
+
|
28
|
+
describe JustPaginate do
|
29
|
+
describe "The backend pagination function" do
|
30
|
+
it "basically works like this" do
|
7
31
|
paged_collection = [1,2,3,4,5,6,7,8,9,10]
|
8
32
|
|
9
33
|
entries, page_count = JustPaginate.paginate(1, 5, 100) do |index_range|
|
@@ -15,29 +39,29 @@ class JustPaginateTest < Test::Unit::TestCase
|
|
15
39
|
assert_equal [1,2,3,4,5], entries
|
16
40
|
end
|
17
41
|
|
18
|
-
|
42
|
+
it "blows up if no selection strategy block is present" do
|
19
43
|
assert_raises RuntimeError do
|
20
44
|
JustPaginate.paginate(1, 20, 100)
|
21
45
|
end
|
22
46
|
end
|
23
47
|
|
24
|
-
|
48
|
+
it "provides predicate to check if pagination would exceed total pagecount" do
|
25
49
|
assert JustPaginate.page_out_of_bounds?(7,2,4)
|
26
50
|
assert !JustPaginate.page_out_of_bounds?(1,20,100)
|
27
51
|
end
|
28
52
|
|
29
|
-
|
53
|
+
it "states that pages below page 1 are out of bounds" do
|
30
54
|
assert JustPaginate.page_out_of_bounds?(-2,2,4)
|
31
55
|
assert JustPaginate.page_out_of_bounds?(-1,2,4)
|
32
56
|
assert JustPaginate.page_out_of_bounds?(0,2,4)
|
33
57
|
end
|
34
58
|
|
35
|
-
|
59
|
+
it "calculates correct total page count" do
|
36
60
|
assert_equal 25, JustPaginate.total_page_number(500, 20)
|
37
61
|
assert_equal 25, JustPaginate.total_page_number(498, 20)
|
38
62
|
end
|
39
63
|
|
40
|
-
|
64
|
+
it "correctly applies the supplied selection strategy" do
|
41
65
|
ran = false
|
42
66
|
sliced_entries, page_count = JustPaginate.paginate(1, 5, 10) do |index_range|
|
43
67
|
assert index_range.class == Range
|
@@ -47,7 +71,7 @@ class JustPaginateTest < Test::Unit::TestCase
|
|
47
71
|
assert ran, "selection block didn't run"
|
48
72
|
end
|
49
73
|
|
50
|
-
|
74
|
+
it "calculates correct index ranges" do
|
51
75
|
assert_equal 0..1, JustPaginate.index_range(1,2,4)
|
52
76
|
assert_equal 2..3, JustPaginate.index_range(2,2,4)
|
53
77
|
|
@@ -70,23 +94,23 @@ class JustPaginateTest < Test::Unit::TestCase
|
|
70
94
|
end
|
71
95
|
end
|
72
96
|
|
73
|
-
|
97
|
+
it "just falls back to largest possible page if given page extends past collection bounds" do
|
74
98
|
assert_equal 2..3, JustPaginate.index_range(2,2,4)
|
75
99
|
assert_equal 2..3, JustPaginate.index_range(3,2,4)
|
76
100
|
assert_equal 2..3, JustPaginate.index_range(4,2,4)
|
77
101
|
end
|
78
102
|
|
79
|
-
|
103
|
+
it "returns 0-0 range for empty collection" do
|
80
104
|
assert_equal 0..0, JustPaginate.index_range(2,2,0)
|
81
105
|
end
|
82
106
|
|
83
|
-
|
84
|
-
|
107
|
+
describe "The frontend pagination html helper" do
|
108
|
+
it "basically works like this" do
|
85
109
|
generated = JustPaginate.page_navigation(1, 10) { |page_no| "/projects/index?page=#{page_no}" }
|
86
110
|
# TODO write this and following tests after I've manually browser-reload-iterated something stable'
|
87
111
|
end
|
88
112
|
|
89
|
-
|
113
|
+
it "does page nav labels, truncation and quicklinks correctly" do
|
90
114
|
assert_correct_paging_labels "", 0, 0
|
91
115
|
assert_correct_paging_labels "", 1, 0
|
92
116
|
assert_correct_paging_labels "", 2, 0
|
data/test/test_helper.rb
CHANGED
@@ -1,5 +1,26 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
1
|
+
# encoding: utf-8
|
2
|
+
# --
|
3
|
+
# The MIT License (MIT)
|
4
|
+
#
|
5
|
+
# Copyright (C) 2013 Gitorious AS
|
6
|
+
#
|
7
|
+
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
8
|
+
# of this software and associated documentation files (the "Software"), to deal
|
9
|
+
# in the Software without restriction, including without limitation the rights
|
10
|
+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
11
|
+
# copies of the Software, and to permit persons to whom the Software is
|
12
|
+
# furnished to do so, subject to the following conditions:
|
13
|
+
#
|
14
|
+
# The above copyright notice and this permission notice shall be included in all
|
15
|
+
# copies or substantial portions of the Software.
|
16
|
+
#
|
17
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
18
|
+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
19
|
+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
20
|
+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
21
|
+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
22
|
+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
23
|
+
# SOFTWARE.
|
24
|
+
#++
|
25
|
+
require "bundler/setup"
|
26
|
+
require "minitest/autorun"
|
metadata
CHANGED
@@ -1,12 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: just_paginate
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 25
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 1
|
9
|
-
|
9
|
+
- 1
|
10
|
+
version: 0.1.1
|
10
11
|
platform: ruby
|
11
12
|
authors:
|
12
13
|
- Gitorious AS
|
@@ -14,7 +15,7 @@ autorequire:
|
|
14
15
|
bindir: bin
|
15
16
|
cert_chain: []
|
16
17
|
|
17
|
-
date: 2013-
|
18
|
+
date: 2013-08-27 00:00:00 Z
|
18
19
|
dependencies:
|
19
20
|
- !ruby/object:Gem::Dependency
|
20
21
|
name: minitest
|
@@ -22,16 +23,17 @@ dependencies:
|
|
22
23
|
requirement: &id001 !ruby/object:Gem::Requirement
|
23
24
|
none: false
|
24
25
|
requirements:
|
25
|
-
- -
|
26
|
+
- - ~>
|
26
27
|
- !ruby/object:Gem::Version
|
27
28
|
hash: 3
|
28
29
|
segments:
|
30
|
+
- 2
|
29
31
|
- 0
|
30
|
-
version: "0"
|
32
|
+
version: "2.0"
|
31
33
|
type: :development
|
32
34
|
version_requirements: *id001
|
33
35
|
- !ruby/object:Gem::Dependency
|
34
|
-
name:
|
36
|
+
name: rake
|
35
37
|
prerelease: false
|
36
38
|
requirement: &id002 !ruby/object:Gem::Requirement
|
37
39
|
none: false
|
@@ -44,34 +46,6 @@ dependencies:
|
|
44
46
|
version: "0"
|
45
47
|
type: :development
|
46
48
|
version_requirements: *id002
|
47
|
-
- !ruby/object:Gem::Dependency
|
48
|
-
name: shoulda-context
|
49
|
-
prerelease: false
|
50
|
-
requirement: &id003 !ruby/object:Gem::Requirement
|
51
|
-
none: false
|
52
|
-
requirements:
|
53
|
-
- - ">="
|
54
|
-
- !ruby/object:Gem::Version
|
55
|
-
hash: 3
|
56
|
-
segments:
|
57
|
-
- 0
|
58
|
-
version: "0"
|
59
|
-
type: :development
|
60
|
-
version_requirements: *id003
|
61
|
-
- !ruby/object:Gem::Dependency
|
62
|
-
name: rake
|
63
|
-
prerelease: false
|
64
|
-
requirement: &id004 !ruby/object:Gem::Requirement
|
65
|
-
none: false
|
66
|
-
requirements:
|
67
|
-
- - ">="
|
68
|
-
- !ruby/object:Gem::Version
|
69
|
-
hash: 3
|
70
|
-
segments:
|
71
|
-
- 0
|
72
|
-
version: "0"
|
73
|
-
type: :development
|
74
|
-
version_requirements: *id004
|
75
49
|
description: Framework-agnostic support for paginating collections of things, and linking to paginated things in your webpage
|
76
50
|
email:
|
77
51
|
- support@gitorious.org
|
@@ -88,8 +62,8 @@ files:
|
|
88
62
|
- Rakefile
|
89
63
|
- just_paginate.gemspec
|
90
64
|
- lib/just_paginate.rb
|
65
|
+
- test/just_paginate_test.rb
|
91
66
|
- test/test_helper.rb
|
92
|
-
- test/test_just_paginate.rb
|
93
67
|
homepage: https://gitorious.org/gitorious/just_paginate
|
94
68
|
licenses: []
|
95
69
|
|