paginate-simple 0.1.0 → 0.1.1
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/README.rdoc +72 -1
- data/VERSION +1 -1
- data/lib/paginate-simple.rb +1 -3
- data/paginate-simple.gemspec +1 -1
- data/test/paginate_simple.spec +41 -37
- metadata +2 -2
data/README.rdoc
CHANGED
@@ -1,6 +1,77 @@
|
|
1
1
|
= paginate-simple
|
2
|
+
The idea for building this gem came up when i was looking for a way to paginate my posts
|
3
|
+
in a simple sinatra blog that i was building. I needed some kind of pagination that was
|
4
|
+
not coupled with any of the existing ORM's.
|
5
|
+
The abstraction of a paginator should be simple.
|
2
6
|
|
3
|
-
|
7
|
+
It should be able to paginate some collection of data by providing a
|
8
|
+
1. total number of items
|
9
|
+
2. the page
|
10
|
+
3. and number of items per page
|
11
|
+
|
12
|
+
It should be able to answer on the following
|
13
|
+
1. How many items should i skip (offset)
|
14
|
+
2. How many items should i get (per_page)
|
15
|
+
3. Is there a next page and if there is give me the next page.
|
16
|
+
4. Is there a previous page and if there is give me the previous page.
|
17
|
+
5. It should be able to give you the first page and the last page.
|
18
|
+
6. It should be able to provide you a way to iterate over the existing pages.
|
19
|
+
|
20
|
+
In the previous version of the gem i have created a PaginateSimple module to work as a
|
21
|
+
singleton but in this version since it should be a pagination logic i have changed that
|
22
|
+
behavior in order to provide a way to extend and change the functionality of the module
|
23
|
+
and its usage.
|
24
|
+
|
25
|
+
Usage:
|
26
|
+
In order to create a singleton pagination object we need to create a class Paginator that
|
27
|
+
will extend this module and change some of its built in logic like this:
|
28
|
+
class Paginator
|
29
|
+
extend PaginateSimple
|
30
|
+
|
31
|
+
def self.current_page(page)
|
32
|
+
super page.to_i # allow strings to be passed like "1" as well
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
If we need to have more than one Paginator we will include the module instead and
|
37
|
+
then instantiate the object as needed.
|
38
|
+
class Paginator
|
39
|
+
include PaginateSimple
|
40
|
+
|
41
|
+
def current_page(page)
|
42
|
+
super page.to_i # allow strings to be passed like "1" as well
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
@paginator = Paginator.new
|
47
|
+
@paginator.config :total => 100 , :page => 1, :per_page => 10
|
48
|
+
|
49
|
+
@another_paginator = Paginator.new
|
50
|
+
@another_paginator.config :total => 100 , :page => 1, :per_page => 3
|
51
|
+
|
52
|
+
By default no helper for the paginator exists. But to write one is quite simple.
|
53
|
+
Here is the example of a paginator helper that i use in my blog.
|
54
|
+
|
55
|
+
require 'sinatra/base'
|
56
|
+
|
57
|
+
module Sinatra
|
58
|
+
module PaginationHelper
|
59
|
+
def paginate(resource)
|
60
|
+
data = []
|
61
|
+
data << '<div class="pagination">'
|
62
|
+
data << "<a href =\"/#{resource}/?page=#{Paginator.previous_page}\">Prev</a>" if Paginator.has_previous_page?
|
63
|
+
data << ' | ' if Paginator.has_previous_page? and Paginator.has_next_page?
|
64
|
+
data << "<a href =\"/#{resource}/?page=#{Paginator.next_page}\">Next</a>" if Paginator.has_next_page?
|
65
|
+
data << '</div>'
|
66
|
+
data.join(' ')
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
helpers PaginationHelper
|
71
|
+
end
|
72
|
+
|
73
|
+
and i use it like this in my haml file
|
74
|
+
= paginate 'posts'
|
4
75
|
|
5
76
|
== Contributing to paginate-simple
|
6
77
|
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.1.
|
1
|
+
0.1.1
|
data/lib/paginate-simple.rb
CHANGED
data/paginate-simple.gemspec
CHANGED
data/test/paginate_simple.spec
CHANGED
@@ -1,80 +1,84 @@
|
|
1
1
|
require './helper'
|
2
2
|
|
3
|
+
class Paginator
|
4
|
+
extend PaginateSimple
|
5
|
+
end
|
6
|
+
|
3
7
|
describe PaginateSimple do
|
4
8
|
before do
|
5
|
-
|
9
|
+
Paginator.config :total => 100 , :page => 1, :per_page => 10
|
6
10
|
end
|
7
11
|
|
8
12
|
it "should be configured properly" do
|
9
|
-
|
10
|
-
|
11
|
-
|
13
|
+
Paginator.total.should eql(100)
|
14
|
+
Paginator.current_page.should eql(1)
|
15
|
+
Paginator.per_page.should eql(10)
|
12
16
|
end
|
13
17
|
|
14
18
|
it "should provide the correct number of pages" do
|
15
|
-
|
19
|
+
Paginator.num_of_pages.should eql(10)
|
16
20
|
end
|
17
21
|
|
18
22
|
it "should answer to has_next_page? correctly" do
|
19
|
-
|
20
|
-
|
21
|
-
|
23
|
+
Paginator.should have_next_page
|
24
|
+
Paginator.current_page = 10
|
25
|
+
Paginator.should_not have_next_page
|
22
26
|
end
|
23
27
|
|
24
28
|
it "should answer to has_previous_page? correctly" do
|
25
|
-
|
26
|
-
|
27
|
-
|
29
|
+
Paginator.should_not have_previous_page
|
30
|
+
Paginator.current_page = 2
|
31
|
+
Paginator.should have_next_page
|
28
32
|
end
|
29
33
|
|
30
34
|
it "should return pages as iterator" do
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
+
Paginator.pages.should respond_to(:each)
|
36
|
+
Paginator.current_page = 1
|
37
|
+
Paginator.total = 0
|
38
|
+
Paginator.pages.should respond_to(:each)
|
35
39
|
end
|
36
40
|
|
37
41
|
it "should return correct num of pages" do
|
38
|
-
|
39
|
-
|
40
|
-
|
42
|
+
Paginator.per_page = 3
|
43
|
+
Paginator.total = 10
|
44
|
+
Paginator.should have(4).pages
|
41
45
|
|
42
|
-
|
43
|
-
|
44
|
-
|
46
|
+
Paginator.current_page = 1
|
47
|
+
Paginator.total = 0
|
48
|
+
Paginator.should have(0).pages
|
45
49
|
end
|
46
50
|
|
47
51
|
it "should provide offset" do
|
48
|
-
|
49
|
-
|
52
|
+
Paginator.current_page = 2
|
53
|
+
Paginator.offset.should eql(10)
|
50
54
|
end
|
51
55
|
|
52
56
|
it "should return next page or nil otherwise" do
|
53
|
-
|
54
|
-
|
55
|
-
|
57
|
+
Paginator.current_page = 4
|
58
|
+
Paginator.should have_next_page
|
59
|
+
Paginator.next_page.should eql(5)
|
56
60
|
|
57
|
-
|
58
|
-
|
59
|
-
|
61
|
+
Paginator.current_page = 10
|
62
|
+
Paginator.should_not have_next_page
|
63
|
+
Paginator.next_page.should be_nil
|
60
64
|
end
|
61
65
|
|
62
66
|
it "should return previous page or nil otherwise" do
|
63
|
-
|
64
|
-
|
65
|
-
|
67
|
+
Paginator.current_page = 2
|
68
|
+
Paginator.should have_previous_page
|
69
|
+
Paginator.previous_page.should eql(1)
|
66
70
|
|
67
|
-
|
68
|
-
|
69
|
-
|
71
|
+
Paginator.current_page = 1
|
72
|
+
Paginator.should_not have_previous_page
|
73
|
+
Paginator.previous_page.should be_nil
|
70
74
|
end
|
71
75
|
|
72
76
|
it "should raise ArgumentError on negative current_page" do
|
73
|
-
lambda {
|
77
|
+
lambda { Paginator.current_page = -1 }.should raise_error(ArgumentError)
|
74
78
|
end
|
75
79
|
|
76
80
|
it "should raise ArgumentError on negative total" do
|
77
|
-
lambda {
|
81
|
+
lambda { Paginator.total = -1 }.should raise_error(ArgumentError)
|
78
82
|
end
|
79
83
|
|
80
84
|
end
|
metadata
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
name: paginate-simple
|
3
3
|
version: !ruby/object:Gem::Version
|
4
4
|
prerelease:
|
5
|
-
version: 0.1.
|
5
|
+
version: 0.1.1
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- filip
|
@@ -103,7 +103,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
103
103
|
requirements:
|
104
104
|
- - ">="
|
105
105
|
- !ruby/object:Gem::Version
|
106
|
-
hash: -
|
106
|
+
hash: -3688072682752233478
|
107
107
|
segments:
|
108
108
|
- 0
|
109
109
|
version: "0"
|