just_paginate 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,5 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
5
+ /.keeptesting
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in keeptesting.gemspec
4
+ gemspec
@@ -0,0 +1,30 @@
1
+
2
+ DESCRIPTION:
3
+ ===========
4
+
5
+ just_paginate is a framework-agnostic approach to creating paginated webpages.
6
+
7
+
8
+ TODO
9
+
10
+
11
+
12
+ EXAMPLES:
13
+ ======
14
+
15
+
16
+
17
+ INSTALL:
18
+ ========
19
+
20
+ `sudo gem install just_paginate`
21
+
22
+ (Or simply stick just_paginate into your Gemfile and use Bundler to pull it down)
23
+
24
+
25
+ LICENSE:
26
+ ========
27
+
28
+ just_paginate is free software licensed under the
29
+ [GNU Affero General Public License (AGPL)](http://www.gnu.org/licenses/agpl-3.0.html). just_paginate
30
+ is developed as part of the Gitorious project.
@@ -0,0 +1,7 @@
1
+ require "bundler/gem_tasks"
2
+ require 'rake/testtask'
3
+
4
+ Rake::TestTask.new do |i|
5
+ i.test_files = FileList['test/test_*.rb']
6
+ #i.verbose = true
7
+ end
@@ -0,0 +1,27 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "just_paginate/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "just_paginate"
7
+ s.version = JustPaginate::VERSION
8
+ s.authors = ["Gitorious AS"]
9
+ s.email = ["support@gitorious.org"]
10
+ s.homepage = "https://gitorious.org/gitorious/just_paginate"
11
+ s.summary = %q{Framework-agnostic support for paginating collections of things, and linking to paginated things in your webpage}
12
+ s.description = %q{Framework-agnostic support for paginating collections of things, and linking to paginated things in your webpage}
13
+
14
+ s.rubyforge_project = "just_paginate"
15
+
16
+ s.files = `git ls-files`.split("\n")
17
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
+ s.require_paths = ["lib"]
20
+
21
+ # specify any dependencies here; for example:
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"
27
+ end
@@ -0,0 +1,92 @@
1
+ require "just_paginate/version"
2
+ require "just_paginate/common"
3
+
4
+ module JustPaginate
5
+
6
+ def self.page_value(page)
7
+ if page.nil?
8
+ 1
9
+ else
10
+ page.to_i
11
+ end
12
+ end
13
+
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
+ total_page_number = total_entry_count / per_page
17
+ entries = yield(index_range(curr_page, per_page, total_entry_count)) || []
18
+ return entries, total_page_number
19
+ end
20
+
21
+ def self.index_range(curr_page, per_page, total_entry_count)
22
+ start_index = ((curr_page-1)*per_page)
23
+ end_index = (start_index+per_page)-1
24
+
25
+ if(start_index>total_entry_count)
26
+ raise "Pagination is out of bounds, beyond total entry size"
27
+ end
28
+
29
+ if end_index>total_entry_count
30
+ end_index=(total_entry_count-1)
31
+ end
32
+
33
+ Range.new(start_index, end_index)
34
+ end
35
+
36
+ def self.page_navigation(curr_page, total_page_count, &page_link_constructor)
37
+ links = page_links(curr_page.to_i, total_page_count, &page_link_constructor)
38
+ return "<style>.pagelink{font-size: 20px; margin-right: 5px;}</style><center>#{links}</center>"
39
+ end
40
+
41
+ def self.page_links(curr_page, total_page_count, &page_link_constructor)
42
+ page_labels(curr_page, total_page_count).map do |label|
43
+ page_element = ""
44
+
45
+ if label == "..."
46
+ page_element = "<span class='pagelink'>#{label}</span>"
47
+ elsif label == "<"
48
+ page_url = yield(curr_page-1)
49
+ page_element = "<a class='pagelink' href='#{page_url}'>#{label}</a>"
50
+ elsif label == ">"
51
+ page_url = yield(curr_page+1)
52
+ page_element = "<a class='pagelink' href='#{page_url}'>#{label}</a>"
53
+ else
54
+ page_url = yield(label)
55
+ if label.to_i == curr_page
56
+ page_element = "<span class='pagelink'>#{label}</span>"
57
+ else
58
+ page_element = "<a class='pagelink' href='#{page_url}'>#{label}</a>"
59
+ end
60
+ end
61
+
62
+ end.join(" ")
63
+ end
64
+
65
+ def self.page_labels(current, total)
66
+ max_visible = 10
67
+ labels = []
68
+
69
+ if total <= max_visible
70
+ 1.upto(total).each {|n| labels.push(n.to_s)}
71
+ else
72
+ if current > max_visible
73
+ labels = ["<", "1", "..."]
74
+ else
75
+ current = 1
76
+ end
77
+
78
+ if (current > (total-max_visible))
79
+ current = (total-max_visible)+1
80
+ end
81
+
82
+ current.upto(current+(max_visible-1)).each {|n| labels.push(n.to_s)}
83
+
84
+ if (current <= (total-max_visible))
85
+ labels.concat ["...", "#{total}", ">"]
86
+ end
87
+ end
88
+
89
+ return labels
90
+ end
91
+
92
+ end
@@ -0,0 +1,6 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ module Keeptesting
4
+
5
+
6
+ end
@@ -0,0 +1,3 @@
1
+ module JustPaginate
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,5 @@
1
+ require 'test/unit'
2
+ require 'shoulda'
3
+ require 'shoulda-context'
4
+
5
+ require File.dirname(__FILE__) + '/../lib/just_paginate'
@@ -0,0 +1,91 @@
1
+ require File.dirname(__FILE__) + '/test_helper.rb'
2
+
3
+ class JustPaginateTest < Test::Unit::TestCase
4
+
5
+ context "The backend pagination function" do
6
+ should "basically work like this" do
7
+ paged_collection = [1,2,3,4,5,6,7,8,9,10]
8
+
9
+ entries, page_count = JustPaginate.paginate(1, 5, 100) do |index_range|
10
+ paged_collection.slice(index_range)
11
+ end
12
+
13
+ assert_equal 5, entries.size
14
+ assert_equal 20, page_count
15
+ assert_equal [1,2,3,4,5], entries
16
+ end
17
+
18
+ should "blow up if no selection strategy block is present" do
19
+ assert_raises RuntimeError do
20
+ JustPaginate.paginate(1, 20, 100)
21
+ end
22
+ end
23
+
24
+ should "complain if given page extends past collection bounds" do
25
+ assert_raises RuntimeError do
26
+ JustPaginate.index_range(7,2,4)
27
+ end
28
+ end
29
+
30
+ should "correctly apply the supplied selection strategy" do
31
+ ran = false
32
+ sliced_entries, page_count = JustPaginate.paginate(1, 5, 10) do |index_range|
33
+ assert index_range.class == Range
34
+ assert_equal 0..4, index_range
35
+ ran = true
36
+ end
37
+ assert ran, "selection block didn't run"
38
+ end
39
+
40
+ should "calculate correct index ranges" do
41
+ assert_equal 0..1, JustPaginate.index_range(1,2,4)
42
+ assert_equal 2..3, JustPaginate.index_range(2,2,4)
43
+
44
+ assert_equal 0..2, JustPaginate.index_range(1,3,9)
45
+ assert_equal 3..5, JustPaginate.index_range(2,3,9)
46
+ assert_equal 6..8, JustPaginate.index_range(3,3,9)
47
+
48
+ assert_equal 0..2, JustPaginate.index_range(1,3,9)
49
+ assert_equal 3..5, JustPaginate.index_range(2,3,9)
50
+ assert_equal 6..8, JustPaginate.index_range(3,3,9)
51
+
52
+ assert_equal 0..19, JustPaginate.index_range(1,20,100)
53
+ assert_equal 60..79, JustPaginate.index_range(4,20,100)
54
+ assert_equal 60..79, JustPaginate.index_range(4,20,95)
55
+ assert_equal 80..99, JustPaginate.index_range(5,20,100)
56
+ assert_equal 80..94, JustPaginate.index_range(5,20,95)
57
+ end
58
+ end
59
+
60
+ context "The frontend pagination html helper" do
61
+ should "basically work like this" do
62
+ generated = JustPaginate.page_navigation(1, 10) { |page_no| "/projects/index?page=#{page_no}" }
63
+ # TODO write this and following tests after I've manually browser-reload-iterated something stable'
64
+ end
65
+
66
+ should "do page nav labels, truncation and quicklinks correctly" do
67
+ assert_correct_paging_labels "1 2 3 4 5 6 7 8 9 10", 1, 10
68
+
69
+ assert_correct_paging_labels "1 2 3 4 5 6 7 8 9 10 ... 50 >", 1, 50
70
+ assert_correct_paging_labels "1 2 3 4 5 6 7 8 9 10 ... 50 >", 5, 50
71
+ assert_correct_paging_labels "1 2 3 4 5 6 7 8 9 10 ... 50 >", 10, 50
72
+ assert_correct_paging_labels "1 2 3 4 5 6 7 8 9 10 ... 545 >", 1, 545
73
+
74
+ assert_correct_paging_labels "< 1 ... 11 12 13 14 15 16 17 18 19 20 ... 50 >", 11, 50
75
+ assert_correct_paging_labels "< 1 ... 12 13 14 15 16 17 18 19 20 21 ... 50 >", 12, 50
76
+ assert_correct_paging_labels "< 1 ... 21 22 23 24 25 26 27 28 29 30 ... 50 >", 21, 50
77
+ assert_correct_paging_labels "< 1 ... 40 41 42 43 44 45 46 47 48 49 ... 50 >", 40, 50
78
+
79
+ assert_correct_paging_labels "< 1 ... 41 42 43 44 45 46 47 48 49 50", 41, 50
80
+ assert_correct_paging_labels "< 1 ... 41 42 43 44 45 46 47 48 49 50", 45, 50
81
+ assert_correct_paging_labels "< 1 ... 41 42 43 44 45 46 47 48 49 50", 50, 50
82
+ end
83
+ end
84
+
85
+ def assert_correct_paging_labels(expected_joined, page, total)
86
+ p = JustPaginate
87
+ actual = p.page_labels(page, total).join(" ")
88
+ assert_equal expected_joined, actual, "labels didn't match expected paging labels'"
89
+ end
90
+
91
+ end
metadata ADDED
@@ -0,0 +1,90 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: just_paginate
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Gitorious AS
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-10-03 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: minitest
16
+ requirement: &22415940 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: *22415940
25
+ - !ruby/object:Gem::Dependency
26
+ name: shoulda
27
+ requirement: &22415460 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :development
34
+ prerelease: false
35
+ version_requirements: *22415460
36
+ - !ruby/object:Gem::Dependency
37
+ name: shoulda-context
38
+ requirement: &22415020 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ type: :development
45
+ prerelease: false
46
+ version_requirements: *22415020
47
+ description: Framework-agnostic support for paginating collections of things, and
48
+ linking to paginated things in your webpage
49
+ email:
50
+ - support@gitorious.org
51
+ executables: []
52
+ extensions: []
53
+ extra_rdoc_files: []
54
+ files:
55
+ - .gitignore
56
+ - Gemfile
57
+ - README.md
58
+ - Rakefile
59
+ - just_paginate.gemspec
60
+ - lib/just_paginate.rb
61
+ - lib/just_paginate/common.rb
62
+ - lib/just_paginate/version.rb
63
+ - test/test_helper.rb
64
+ - test/test_just_paginate.rb
65
+ homepage: https://gitorious.org/gitorious/just_paginate
66
+ licenses: []
67
+ post_install_message:
68
+ rdoc_options: []
69
+ require_paths:
70
+ - lib
71
+ required_ruby_version: !ruby/object:Gem::Requirement
72
+ none: false
73
+ requirements:
74
+ - - ! '>='
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
77
+ required_rubygems_version: !ruby/object:Gem::Requirement
78
+ none: false
79
+ requirements:
80
+ - - ! '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ requirements: []
84
+ rubyforge_project: just_paginate
85
+ rubygems_version: 1.8.15
86
+ signing_key:
87
+ specification_version: 3
88
+ summary: Framework-agnostic support for paginating collections of things, and linking
89
+ to paginated things in your webpage
90
+ test_files: []