pagination 0.3.2 → 0.3.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.3.2
1
+ 0.3.3
@@ -46,17 +46,23 @@ module Pagination
46
46
  # based on the current page.
47
47
  #
48
48
  # If we have 100 pages for example and we're at page 50,
49
- # this would simple return
49
+ # this would simply return
50
50
  #
51
- # [50, 51, 52, 53, 54, 55, 56, 57, 58, 59]
51
+ # [45, 46, 47, 48, 49, 50, 51, 52, 53, 54]
52
52
  #
53
53
  # When we're at page 1, it displays 1 to 10.
54
54
  #
55
55
  # You can pass in a number to limit the total displayed pages.
56
- # 1 2 3 4 5
57
- def displayed_pages(limit = 10)
58
- lower = [page, [pages.last - limit, 0].max + 1].min
59
- upper = [page + limit - 1, pages.last].min
56
+ def displayed_pages(limit = 10, left_offset = -5, right_offset = 4)
57
+ lower, upper = nil, nil
58
+
59
+ if page + left_offset < 1 || page + right_offset > pages.last
60
+ lower = [page, [pages.last - limit, 0].max + 1].min
61
+ upper = [page + limit - 1, pages.last].min
62
+ else
63
+ lower = page + left_offset
64
+ upper = page + right_offset
65
+ end
60
66
 
61
67
  (lower..upper).to_a
62
68
  end
@@ -73,6 +79,10 @@ module Pagination
73
79
  def each(&block)
74
80
  collection.each(&block)
75
81
  end
82
+
83
+ def total_pages
84
+ (total / per_page.to_f).ceil
85
+ end
76
86
 
77
87
  protected
78
88
  def collection
@@ -80,12 +90,7 @@ module Pagination
80
90
  end
81
91
 
82
92
  def pages
83
- 1..last_page
93
+ 1..total_pages
84
94
  end
85
-
86
- def last_page
87
- (total / per_page.to_f).ceil
88
- end
89
-
90
95
  end
91
96
  end
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{pagination}
8
- s.version = "0.3.2"
8
+ s.version = "0.3.3"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Cyril David"]
12
- s.date = %q{2010-06-04}
12
+ s.date = %q{2010-06-10}
13
13
  s.default_executable = %q{pagination}
14
14
  s.description = %q{Trying to make the pagination world a better place}
15
15
  s.email = %q{cyx.ucron@gmail.com}
@@ -71,34 +71,59 @@ class TestPagination < Test::Unit::TestCase
71
71
  end
72
72
  end
73
73
 
74
- context "given we have 100 pages and we're at page 1" do
74
+ context "given we have 100 pages and we're at pages 1 to 6" do
75
+ should "have 1 to 10 of displayed_pages" do
76
+ (1..6).each do |p|
77
+ collection = Pagination.paginate((1..100).to_a, :per_page => p)
78
+ assert_equal (1..10).to_a, collection.displayed_pages
79
+ end
80
+ end
81
+ end
82
+
83
+ context "given we have 100 pages and we're at page 7" do
75
84
  setup do
76
- @collection = Pagination.paginate((1..100).to_a, :per_page => 1)
85
+ @collection = Pagination.paginate((1..100).to_a, :per_page => 1, :page => 7)
77
86
  end
78
87
 
79
- should "have 1 to 10 of displayed_pages" do
80
- assert_equal (1..10).to_a, @collection.displayed_pages
88
+ should "have 2 to 11 of displayed_pages" do
89
+ assert_equal (2..11).to_a, @collection.displayed_pages
81
90
  end
82
91
  end
83
92
 
93
+
84
94
  context "given we have 100 pages and we're at page 50" do
85
95
  setup do
86
96
  @collection = Pagination.paginate((1..100).to_a, :per_page => 1, :page => 50)
87
97
  end
88
98
 
89
- should "have 50 to 59 of displayed_pages" do
90
- assert_equal (50..59).to_a, @collection.displayed_pages
99
+ should "have 45 to 54 of displayed_pages" do
100
+ assert_equal (45..54).to_a, @collection.displayed_pages
101
+ end
102
+ end
103
+
104
+ context "given we have 100 pages and we're at page 94" do
105
+ should "start with 89 and end with 98" do
106
+ collection = Pagination.paginate((1..100).to_a, :per_page => 1, :page => 94)
107
+ assert_equal (89..98).to_a, collection.displayed_pages
108
+ end
109
+ end
110
+
111
+ context "given we have 100 pages and we're at page 95" do
112
+ should "start with 90 and end with 99" do
113
+ collection = Pagination.paginate((1..100).to_a, :per_page => 1, :page => 95)
114
+ assert_equal (90..99).to_a, collection.displayed_pages
91
115
  end
92
116
  end
93
117
 
94
- context "given we have 100 pages and we're at pages 91 up to 99" do
95
- should "start with the 91 end with 100" do
96
- (91..99).each do |p|
118
+ context "given we have 100 pages and we're at pages 96 up to 100" do
119
+ should "start with 91 and end with 100" do
120
+ (96..100).each do |p|
97
121
  collection = Pagination.paginate((1..100).to_a, :per_page => 1, :page => p)
98
122
  assert_equal (91..100).to_a, collection.displayed_pages
99
123
  end
100
124
  end
101
125
  end
126
+
102
127
 
103
128
  context "given we have 5 pages and we're at page 2 up to 5" do
104
129
  should "always display the 5 pages" do
@@ -33,23 +33,23 @@ class TestPaginationTemplate < Test::Unit::TestCase
33
33
 
34
34
  should "have a ul > li.next-link with page=2" do
35
35
  assert_equal 1,
36
- doc.search('div.pagination > a.next-link[href="?page=2"]').length
36
+ doc.search('nav.pagination > a.next-link[href="?page=2"]').length
37
37
  end
38
38
 
39
39
  should "display the first page as the current page" do
40
- assert_equal 1, doc.search('div.pagination > ul > li > span').length
40
+ assert_equal 1, doc.search('nav.pagination > nav.page-numbers > ul > li > span').length
41
41
 
42
- assert_equal '1', doc.search('div.pagination > ul > li > span').text
42
+ assert_equal '1', doc.search('nav.pagination > nav.page-numbers > ul > li > span').text
43
43
  end
44
44
 
45
45
  should "display pages 2 to 5 as links" do
46
46
  (2..5).each do |page|
47
47
  assert_equal 1,
48
- doc.search(%(div.pagination > ul > li >
48
+ doc.search(%(nav.pagination > nav.page-numbers > ul > li >
49
49
  a[href="?page=#{page}"])).length
50
50
 
51
51
  assert_equal page.to_s,
52
- doc.search(%(div.pagination > ul > li >
52
+ doc.search(%(nav.pagination > nav.page-numbers > ul > li >
53
53
  a[href="?page=#{page}"])).text
54
54
  end
55
55
  end
@@ -1,19 +1,24 @@
1
1
  - if items.render?
2
- %div.pagination
2
+ %nav.pagination
3
3
  - if items.prev_page
4
- %a.prev-link{:href => ("?page=%s" % items.prev_page)}
4
+ %a.prev-link{:href => ("?page=%d" % items.prev_page)}
5
5
  %span &larr; Previous
6
-
7
- %ul
8
- - items.displayed_pages.each do |page|
9
- - if items.current?(page)
10
- %li.active
11
- %span= page
12
- - else
13
- %li
14
- %a{:href => ("?page=%s" % page)}= page
15
-
6
+ %nav.page-numbers
7
+ %span.current-page
8
+ %span
9
+ %strong
10
+ Page
11
+ = items.page
12
+ of
13
+ = items.total_pages
14
+ %ul
15
+ - items.displayed_pages.each do |page|
16
+ - if items.current?(page)
17
+ %li.active
18
+ %span= page
19
+ - else
20
+ %li
21
+ %a{:href => ("?page=%d" % page)}= page
16
22
  - if items.next_page
17
- %a.next-link{:href => ("?page=%s" % items.next_page)}
23
+ %a.next-link{:href => ("?page=%d" % items.next_page)}
18
24
  %span Next &rarr;
19
-
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 3
8
- - 2
9
- version: 0.3.2
8
+ - 3
9
+ version: 0.3.3
10
10
  platform: ruby
11
11
  authors:
12
12
  - Cyril David
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-06-04 00:00:00 +08:00
17
+ date: 2010-06-10 00:00:00 +08:00
18
18
  default_executable: pagination
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency