paginary 0.0.1.pre1 → 0.0.1.pre2
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +1 -0
- data/Rakefile +2 -2
- data/VERSION +1 -1
- data/lib/paginary/helpers/page_builder.rb +29 -6
- data/lib/paginary/helpers/pagination_helper.rb +3 -2
- data/lib/paginary/relation/paginated.rb +6 -3
- data/paginary.gemspec +6 -6
- data/test/unit/helpers/page_builder_test.rb +40 -0
- data/test/unit/helpers/pagination_helper_test.rb +7 -0
- data/test/unit/relation/paginated_test.rb +10 -0
- metadata +8 -7
data/.gitignore
CHANGED
data/Rakefile
CHANGED
@@ -10,9 +10,9 @@ Jeweler::Tasks.new do |spec|
|
|
10
10
|
|
11
11
|
spec.authors = ["Rolf Timmermans"]
|
12
12
|
spec.email = "r.timmermans@voormedia.com"
|
13
|
-
spec.homepage = "http://
|
13
|
+
spec.homepage = "http://github.com/voormedia/paginary"
|
14
14
|
|
15
|
-
spec.
|
15
|
+
spec.add_runtime_dependency "rails", "~> 3.0"
|
16
16
|
spec.add_development_dependency "sqlite3-ruby"
|
17
17
|
end
|
18
18
|
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.0.1.
|
1
|
+
0.0.1.pre2
|
@@ -5,10 +5,12 @@ module Paginary
|
|
5
5
|
alias_method :items, :relation
|
6
6
|
|
7
7
|
delegate :content_tag, :link_to, :params, :translate, :to => :template
|
8
|
+
delegate :current_page, :page_count, :first_page?, :last_page?, :to => :relation
|
8
9
|
|
9
10
|
def initialize(template, relation, options = {})
|
10
11
|
@template = template
|
11
12
|
@param_name = options.delete(:param) || :page
|
13
|
+
@link_range = options.delete(:link_range) || 10
|
12
14
|
@relation = relation.paginated? ? relation : relation.paginate(params[@param_name], options)
|
13
15
|
end
|
14
16
|
|
@@ -17,11 +19,11 @@ module Paginary
|
|
17
19
|
end
|
18
20
|
|
19
21
|
def previous_url
|
20
|
-
page_url
|
22
|
+
page_url current_page - 1
|
21
23
|
end
|
22
24
|
|
23
25
|
def next_url
|
24
|
-
page_url
|
26
|
+
page_url current_page + 1
|
25
27
|
end
|
26
28
|
|
27
29
|
def links
|
@@ -30,14 +32,18 @@ module Paginary
|
|
30
32
|
end
|
31
33
|
|
32
34
|
def page_links
|
33
|
-
|
34
|
-
|
35
|
+
page_numbers.collect do |page|
|
36
|
+
page_link(page)
|
35
37
|
end.inject(:+)
|
36
38
|
end
|
39
|
+
|
40
|
+
def page_link(page)
|
41
|
+
link_to content_tag(:span, page), page_url(page), :class => "page#{current_page == page ? " selected" : ""}"
|
42
|
+
end
|
37
43
|
|
38
44
|
def previous_link
|
39
45
|
text = content_tag(:span, translate("previous", :default => "< Previous"))
|
40
|
-
unless
|
46
|
+
unless first_page?
|
41
47
|
link_to text, previous_url, :class => "previous"
|
42
48
|
else
|
43
49
|
content_tag :span, text, :class => "previous disabled"
|
@@ -46,12 +52,29 @@ module Paginary
|
|
46
52
|
|
47
53
|
def next_link
|
48
54
|
text = content_tag(:span, translate("previous", :default => "Next >"))
|
49
|
-
unless
|
55
|
+
unless last_page?
|
50
56
|
link_to text, next_url, :class => "next"
|
51
57
|
else
|
52
58
|
content_tag :span, text, :class => "next disabled"
|
53
59
|
end
|
54
60
|
end
|
61
|
+
|
62
|
+
protected
|
63
|
+
|
64
|
+
def page_numbers
|
65
|
+
start = current_page - @link_range
|
66
|
+
finish = current_page + @link_range
|
67
|
+
|
68
|
+
if start < 1
|
69
|
+
finish = [finish + 1 - start, page_count].min
|
70
|
+
start = 1
|
71
|
+
elsif finish > page_count
|
72
|
+
start = [start + page_count - finish, 1].max
|
73
|
+
finish = page_count
|
74
|
+
end
|
75
|
+
|
76
|
+
(start..finish).to_a
|
77
|
+
end
|
55
78
|
end
|
56
79
|
end
|
57
80
|
end
|
@@ -1,8 +1,9 @@
|
|
1
1
|
module Paginary
|
2
2
|
module Helpers
|
3
3
|
module PaginationHelper
|
4
|
-
def paginate(relation,
|
5
|
-
|
4
|
+
def paginate(relation, options = {}, &block)
|
5
|
+
builder = options.delete(:builder) || PageBuilder
|
6
|
+
capture(builder.new(self, relation, options), &block)
|
6
7
|
end
|
7
8
|
end
|
8
9
|
end
|
@@ -8,14 +8,17 @@ module Paginary
|
|
8
8
|
def paginate!(*args)
|
9
9
|
options = args.extract_options!
|
10
10
|
self.items_per_page = options[:per_page] || 50
|
11
|
-
self.current_page = args.first
|
11
|
+
self.current_page = args.first || 1
|
12
12
|
self.limit_value = items_per_page
|
13
13
|
self.offset_value = items_per_page * (current_page - 1)
|
14
14
|
end
|
15
15
|
|
16
16
|
def current_page=(page)
|
17
|
-
|
18
|
-
|
17
|
+
number = page.to_i
|
18
|
+
unless number.to_s == page.to_s && number.between?(1, page_count)
|
19
|
+
raise ActiveRecord::RecordNotFound, "unknown page #{page}, expected 1..#{page_count}"
|
20
|
+
end
|
21
|
+
@current_page = number
|
19
22
|
end
|
20
23
|
|
21
24
|
def paginated?
|
data/paginary.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{paginary}
|
8
|
-
s.version = "0.0.1.
|
8
|
+
s.version = "0.0.1.pre2"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new("> 1.3.1") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Rolf Timmermans"]
|
12
|
-
s.date = %q{2010-
|
12
|
+
s.date = %q{2010-11-12}
|
13
13
|
s.description = %q{Simple, view-based pagination for Rails, built on top of Active Record 3 awesomeness.}
|
14
14
|
s.email = %q{r.timmermans@voormedia.com}
|
15
15
|
s.extra_rdoc_files = [
|
@@ -54,7 +54,7 @@ Gem::Specification.new do |s|
|
|
54
54
|
"test/unit/relation/paginated_test.rb",
|
55
55
|
"test/unit/relation/query_methods_test.rb"
|
56
56
|
]
|
57
|
-
s.homepage = %q{http://
|
57
|
+
s.homepage = %q{http://github.com/voormedia/paginary}
|
58
58
|
s.rdoc_options = ["--charset=UTF-8"]
|
59
59
|
s.require_paths = ["lib"]
|
60
60
|
s.rubyforge_project = %q{paginary}
|
@@ -82,14 +82,14 @@ Gem::Specification.new do |s|
|
|
82
82
|
s.specification_version = 3
|
83
83
|
|
84
84
|
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
85
|
-
s.
|
85
|
+
s.add_runtime_dependency(%q<rails>, ["~> 3.0"])
|
86
86
|
s.add_development_dependency(%q<sqlite3-ruby>, [">= 0"])
|
87
87
|
else
|
88
|
-
s.add_dependency(%q<rails>, ["
|
88
|
+
s.add_dependency(%q<rails>, ["~> 3.0"])
|
89
89
|
s.add_dependency(%q<sqlite3-ruby>, [">= 0"])
|
90
90
|
end
|
91
91
|
else
|
92
|
-
s.add_dependency(%q<rails>, ["
|
92
|
+
s.add_dependency(%q<rails>, ["~> 3.0"])
|
93
93
|
s.add_dependency(%q<sqlite3-ruby>, [">= 0"])
|
94
94
|
end
|
95
95
|
end
|
@@ -20,6 +20,12 @@ class PaginationHelperTest < ActionView::TestCase
|
|
20
20
|
builder
|
21
21
|
end
|
22
22
|
|
23
|
+
def assert_page_link_chunks(chunks, actual_dom)
|
24
|
+
to_link = lambda { |i| %Q{<a href="/widgets?page=#{i}" class="page#{params[:page] == i ? " selected" : ""}"><span>#{i}</span></a>} }
|
25
|
+
expected_dom = chunks.collect { |chunk| chunk.to_a.map(&to_link).join } * %Q{<span class="omitted">...</span>}
|
26
|
+
assert_dom_equal expected_dom, actual_dom
|
27
|
+
end
|
28
|
+
|
23
29
|
# Items ====================================================================
|
24
30
|
test "items should return paginated relation" do
|
25
31
|
assert_equal_relation @relation.paginate, builder(@relation).items
|
@@ -65,4 +71,38 @@ class PaginationHelperTest < ActionView::TestCase
|
|
65
71
|
test "previous_link should return disabled link to previous page if current page is first" do
|
66
72
|
assert_dom_equal %Q{<span class="previous disabled"><span>< Previous</span></span>}, builder(@relation).previous_link
|
67
73
|
end
|
74
|
+
|
75
|
+
# Chunks ===================================================================
|
76
|
+
test "page_link_ranges should return full range when page count is at maximum" do
|
77
|
+
assert_equal (1..11).to_a, builder(@relation, :per_page => 17, :link_range => 5).send(:page_numbers)
|
78
|
+
end
|
79
|
+
|
80
|
+
test "page_link_ranges should return full range when page count is below maximum" do
|
81
|
+
assert_equal (1..10).to_a, builder(@relation, :per_page => 18, :link_range => 5).send(:page_numbers)
|
82
|
+
end
|
83
|
+
|
84
|
+
test "page_link_ranges should return long beginning range and short closing range when current page is near beginning" do
|
85
|
+
params[:page] = 2
|
86
|
+
assert_equal (1..11).to_a, builder(@relation, :per_page => 1, :link_range => 5).send(:page_numbers)
|
87
|
+
end
|
88
|
+
|
89
|
+
test "page_link_ranges should return short beginning range and long closing range when current page is near end" do
|
90
|
+
params[:page] = 175
|
91
|
+
assert_equal (167..177).to_a, builder(@relation, :per_page => 1, :link_range => 5).send(:page_numbers)
|
92
|
+
end
|
93
|
+
|
94
|
+
test "page_link_ranges should return short beginning and closing range and long middle range when current page is somewhere in the middle" do
|
95
|
+
params[:page] = 155
|
96
|
+
assert_equal (150..160).to_a, builder(@relation, :per_page => 1, :link_range => 5).send(:page_numbers)
|
97
|
+
end
|
98
|
+
|
99
|
+
test "page_links should return page links with omission near the end when page is first" do
|
100
|
+
params[:page] = 1
|
101
|
+
assert_page_link_chunks [1..11], builder(@relation, :per_page => 1, :link_range => 5).page_links
|
102
|
+
end
|
103
|
+
|
104
|
+
test "page_links should return page links with omission near the end and beginning when page is somewhere in the middle" do
|
105
|
+
params[:page] = 100
|
106
|
+
assert_page_link_chunks [95..105], builder(@relation, :per_page => 1, :link_range => 5).page_links
|
107
|
+
end
|
68
108
|
end
|
@@ -21,6 +21,13 @@ class PaginationHelperTest < ActionView::TestCase
|
|
21
21
|
assert_kind_of Paginary::Helpers::PageBuilder, builder
|
22
22
|
end
|
23
23
|
|
24
|
+
test "paginate should accept builder class" do
|
25
|
+
custom_builder = Class.new(Paginary::Helpers::PageBuilder)
|
26
|
+
builder = nil
|
27
|
+
paginate(@relation, :builder => custom_builder) { |b| builder = b }
|
28
|
+
assert_kind_of custom_builder, builder
|
29
|
+
end
|
30
|
+
|
24
31
|
test "paginate should query database for total number of items only once" do
|
25
32
|
num = count_queries do
|
26
33
|
paginate(@relation) { |page| page.links; page.items.to_a }
|
@@ -40,6 +40,10 @@ class PaginatedTest < ActiveRecord::TestCase
|
|
40
40
|
end
|
41
41
|
|
42
42
|
# Page validation ==========================================================
|
43
|
+
test "paginate should set current page if given page is an integer string" do
|
44
|
+
assert_equal 2, @relation.paginate("2").current_page
|
45
|
+
end
|
46
|
+
|
43
47
|
test "paginate should raise error if given page is less than one" do
|
44
48
|
assert_raises ActiveRecord::RecordNotFound do
|
45
49
|
@relation.paginate(0)
|
@@ -52,6 +56,12 @@ class PaginatedTest < ActiveRecord::TestCase
|
|
52
56
|
end
|
53
57
|
end
|
54
58
|
|
59
|
+
test "paginate should raise error if given page is not well formatted" do
|
60
|
+
assert_raises ActiveRecord::RecordNotFound do
|
61
|
+
@relation.paginate("1.5")
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
55
65
|
# Page calculation =========================================================
|
56
66
|
test "page_count should return 1 if there are no items" do
|
57
67
|
assert_equal 1, @relation.where(:deleted => true).paginate.page_count
|
metadata
CHANGED
@@ -6,8 +6,8 @@ version: !ruby/object:Gem::Version
|
|
6
6
|
- 0
|
7
7
|
- 0
|
8
8
|
- 1
|
9
|
-
-
|
10
|
-
version: 0.0.1.
|
9
|
+
- pre2
|
10
|
+
version: 0.0.1.pre2
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Rolf Timmermans
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2010-
|
18
|
+
date: 2010-11-12 00:00:00 +01:00
|
19
19
|
default_executable:
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|
@@ -24,12 +24,13 @@ dependencies:
|
|
24
24
|
requirement: &id001 !ruby/object:Gem::Requirement
|
25
25
|
none: false
|
26
26
|
requirements:
|
27
|
-
- -
|
27
|
+
- - ~>
|
28
28
|
- !ruby/object:Gem::Version
|
29
29
|
segments:
|
30
|
+
- 3
|
30
31
|
- 0
|
31
|
-
version: "0"
|
32
|
-
type: :
|
32
|
+
version: "3.0"
|
33
|
+
type: :runtime
|
33
34
|
version_requirements: *id001
|
34
35
|
- !ruby/object:Gem::Dependency
|
35
36
|
name: sqlite3-ruby
|
@@ -91,7 +92,7 @@ files:
|
|
91
92
|
- test/unit/relation/paginated_test.rb
|
92
93
|
- test/unit/relation/query_methods_test.rb
|
93
94
|
has_rdoc: true
|
94
|
-
homepage: http://
|
95
|
+
homepage: http://github.com/voormedia/paginary
|
95
96
|
licenses: []
|
96
97
|
|
97
98
|
post_install_message:
|