just_paginate 0.0.7 → 0.0.8

Sign up to get free protection for your applications and to get access to all the features.
data/lib/just_paginate.rb CHANGED
@@ -1,70 +1,57 @@
1
1
  # -*- coding: utf-8 -*-
2
2
  module JustPaginate
3
3
 
4
- VERSION = "0.0.7"
4
+ VERSION = "0.0.8"
5
5
 
6
6
  def self.page_value(page)
7
7
  if page.nil?
8
8
  1
9
- else
9
+ else
10
10
  page.to_i
11
11
  end
12
12
  end
13
13
 
14
14
  def self.paginate(curr_page, per_page, total_entry_count, &selection_strategy)
15
- raise "Pagination just supplies index range, expects a selection strategy" if selection_strategy.nil?
16
-
17
- entries = yield(index_range(curr_page, per_page, total_entry_count)) || []
15
+ raise "Pagination just supplies index range, expects a selection strategy" if selection_strategy.nil?
16
+
17
+ entries = yield(index_range(curr_page, per_page, total_entry_count)) || []
18
18
  return entries, total_page_number(total_entry_count, per_page)
19
19
  end
20
20
 
21
21
  def self.total_page_number(total_entry_count, per_page)
22
22
  (total_entry_count.to_f / per_page).ceil
23
23
  end
24
-
24
+
25
25
  def self.index_range(curr_page, per_page, total_entry_count)
26
26
  start_index = ((curr_page-1)*per_page)
27
27
  end_index = (start_index+per_page)-1
28
-
28
+
29
29
  if(start_index>total_entry_count)
30
30
  raise "Pagination is out of bounds, beyond total entry size"
31
31
  end
32
-
32
+
33
33
  if end_index>total_entry_count
34
34
  end_index = total_entry_count
35
35
  end
36
-
36
+
37
37
  Range.new(start_index, end_index)
38
38
  end
39
39
 
40
- def self.page_navigation(curr_page, total_page_count, &page_link_constructor)
40
+ def self.beyond_page_range?(curr_page, per_page, total_entry_count)
41
+ start_index = ((curr_page-1)*per_page)
42
+ end_index = (start_index+per_page)-1
43
+ start_index > total_entry_count
44
+ end
45
+
46
+ def self.page_navigation(curr_page, total_page_count, &page_link_constructor)
41
47
  links = page_links(curr_page.to_i, total_page_count, &page_link_constructor)
42
48
  return "<div class='pagination'>#{links}</div>"
43
49
  end
44
50
 
45
- # Create gitorous-style-pagination until we move to Bootstrap
46
- # <div class="pagination">
47
- # <a href="/projects?page=24" class="prev_page" rel="prev">Previous</a>
48
- # <a href="/projects?page=1" rel="start">1</a>
49
- # <span class="gap">…</span>
50
- # <a href="/projects?page=21">21</a>
51
- # <a href="/projects?page=22">22</a>
52
- # <a href="/projects?page=23">23</a>
53
- # <a href="/projects?page=24" rel="prev">24</a> <span class="current">25</span>
54
- # <a href="/projects?page=26" rel="next">26</a>
55
- # <a href="/projects?page=27">27</a>
56
- # <a href="/projects?page=28">28</a>
57
- # <a href="/projects?page=29">29</a>
58
- # <span class="gap">…</span>
59
- # <a href="/projects?page=1446">1446</a>
60
- # <a href="/projects?page=26" class="next_page" rel="next">Next</a>
61
- # </div>
62
-
63
-
64
51
  def self.page_links(curr_page, total_page_count, &page_link_constructor)
65
52
  page_labels(curr_page, total_page_count).map do |label|
66
53
  page_element = ""
67
-
54
+
68
55
  if label == "..."
69
56
  page_element = "<span class='gap'>#{label}</span>"
70
57
  elsif label == "<"
@@ -84,14 +71,14 @@ module JustPaginate
84
71
 
85
72
  end.join(" ")
86
73
  end
87
-
74
+
88
75
  def self.page_labels(current, total)
89
76
  max_visible = 10
90
77
  labels = []
91
78
 
92
79
  if total <= max_visible
93
80
  1.upto(total).each {|n| labels.push(n.to_s)}
94
- else
81
+ else
95
82
  if current > max_visible
96
83
  labels = ["<", "1", "..."]
97
84
  else
@@ -106,10 +93,10 @@ module JustPaginate
106
93
 
107
94
  if (current <= (total-max_visible))
108
95
  labels.concat ["...", "#{total}", ">"]
109
- end
96
+ end
110
97
  end
111
98
 
112
99
  return labels
113
- end
114
-
100
+ end
101
+
115
102
  end
@@ -1,8 +1,8 @@
1
1
  require File.dirname(__FILE__) + '/test_helper.rb'
2
2
 
3
3
  class JustPaginateTest < Test::Unit::TestCase
4
-
5
- context "The backend pagination function" do
4
+
5
+ context "The backend pagination function" do
6
6
  should "basically work like this" do
7
7
  paged_collection = [1,2,3,4,5,6,7,8,9,10]
8
8
 
@@ -26,12 +26,18 @@ class JustPaginateTest < Test::Unit::TestCase
26
26
  JustPaginate.index_range(7,2,4)
27
27
  end
28
28
  end
29
-
29
+
30
+ should "provide predicate to check if pagination would exceed total pagecount" do
31
+ assert JustPaginate.beyond_page_range?(7,2,4)
32
+ assert !JustPaginate.beyond_page_range?(1,20,100)
33
+ end
34
+
35
+
30
36
  should "calculate correct total page count" do
31
37
  assert_equal 25, JustPaginate.total_page_number(500, 20)
32
38
  assert_equal 25, JustPaginate.total_page_number(498, 20)
33
39
  end
34
-
40
+
35
41
  should "correctly apply the supplied selection strategy" do
36
42
  ran = false
37
43
  sliced_entries, page_count = JustPaginate.paginate(1, 5, 10) do |index_range|
@@ -41,7 +47,7 @@ class JustPaginateTest < Test::Unit::TestCase
41
47
  end
42
48
  assert ran, "selection block didn't run"
43
49
  end
44
-
50
+
45
51
  should "calculate correct index ranges" do
46
52
  assert_equal 0..1, JustPaginate.index_range(1,2,4)
47
53
  assert_equal 2..3, JustPaginate.index_range(2,2,4)
@@ -64,7 +70,7 @@ class JustPaginateTest < Test::Unit::TestCase
64
70
  assert_equal 480..499, JustPaginate.index_range(25,20,500)
65
71
  end
66
72
  end
67
-
73
+
68
74
  context "The frontend pagination html helper" do
69
75
  should "basically work like this" do
70
76
  generated = JustPaginate.page_navigation(1, 10) { |page_no| "/projects/index?page=#{page_no}" }
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: just_paginate
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.7
4
+ version: 0.0.8
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-10-03 00:00:00.000000000Z
12
+ date: 2012-11-08 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: minitest
16
- requirement: &22540540 !ruby/object:Gem::Requirement
16
+ requirement: &19002240 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '0'
22
22
  type: :development
23
23
  prerelease: false
24
- version_requirements: *22540540
24
+ version_requirements: *19002240
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: shoulda
27
- requirement: &22539900 !ruby/object:Gem::Requirement
27
+ requirement: &19001720 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: '0'
33
33
  type: :development
34
34
  prerelease: false
35
- version_requirements: *22539900
35
+ version_requirements: *19001720
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: shoulda-context
38
- requirement: &22539340 !ruby/object:Gem::Requirement
38
+ requirement: &19001220 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ! '>='
@@ -43,7 +43,7 @@ dependencies:
43
43
  version: '0'
44
44
  type: :development
45
45
  prerelease: false
46
- version_requirements: *22539340
46
+ version_requirements: *19001220
47
47
  description: Framework-agnostic support for paginating collections of things, and
48
48
  linking to paginated things in your webpage
49
49
  email: