mongoid-pagination 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +0 -1
- data/lib/mongoid/pagination.rb +10 -4
- data/lib/mongoid/pagination/version.rb +1 -1
- data/mongoid-pagination.gemspec +1 -0
- data/spec/pagination_spec.rb +64 -12
- metadata +15 -4
data/.gitignore
CHANGED
data/lib/mongoid/pagination.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
require 'active_support/core_ext'
|
2
|
+
|
1
3
|
module Mongoid
|
2
4
|
module Pagination
|
3
5
|
extend ActiveSupport::Concern
|
@@ -12,15 +14,19 @@ module Mongoid
|
|
12
14
|
#
|
13
15
|
# @return [Mongoid::Criteria]
|
14
16
|
def paginate(opts = {})
|
15
|
-
return criteria if opts[:limit].
|
17
|
+
return criteria if opts[:limit].blank? and opts[:page].blank? and opts[:offset].blank?
|
16
18
|
|
17
19
|
limit = (opts[:limit] || 25).to_i
|
18
20
|
page = (opts[:page] || 1).to_i
|
19
21
|
|
20
|
-
if page
|
21
|
-
offset = (
|
22
|
+
if opts[:page].blank?
|
23
|
+
offset = (opts[:offset] || 0)
|
22
24
|
else
|
23
|
-
|
25
|
+
if page > 1
|
26
|
+
offset = (page - 1) * limit
|
27
|
+
else
|
28
|
+
offset = 0
|
29
|
+
end
|
24
30
|
end
|
25
31
|
|
26
32
|
per_page(limit).offset(offset)
|
data/mongoid-pagination.gemspec
CHANGED
data/spec/pagination_spec.rb
CHANGED
@@ -20,6 +20,10 @@ describe Mongoid::Pagination do
|
|
20
20
|
subject.options[:skip].should be_nil
|
21
21
|
end
|
22
22
|
|
23
|
+
it "does not set the offset param by default" do
|
24
|
+
subject.options[:offset].should be_nil
|
25
|
+
end
|
26
|
+
|
23
27
|
it "defaults the limit param to 25" do
|
24
28
|
subject.options[:limit].should be_nil
|
25
29
|
end
|
@@ -44,7 +48,27 @@ describe Mongoid::Pagination do
|
|
44
48
|
end
|
45
49
|
end
|
46
50
|
|
47
|
-
describe "when passed
|
51
|
+
describe "when passed an offset param but no limit" do
|
52
|
+
subject { Person.paginate(:offset => 10) }
|
53
|
+
|
54
|
+
it "defaults the limit to 25" do
|
55
|
+
subject.options[:limit].should == 25
|
56
|
+
end
|
57
|
+
|
58
|
+
it "sets the offset to 10" do
|
59
|
+
subject.options[:skip].should == 10
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
describe "when passed an offset param and a page param" do
|
64
|
+
subject { Person.paginate(:offset => 0, :page => 2) }
|
65
|
+
|
66
|
+
it "uses the calculated offset from page instead of the offset param" do
|
67
|
+
subject.options[:skip].should == 25
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
describe "when passed a limit param but no page or offset" do
|
48
72
|
subject { Person.paginate(:limit => 100) }
|
49
73
|
|
50
74
|
it "defaults the page to 0" do
|
@@ -56,16 +80,32 @@ describe Mongoid::Pagination do
|
|
56
80
|
end
|
57
81
|
end
|
58
82
|
|
59
|
-
|
60
|
-
|
61
|
-
|
83
|
+
context "page param" do
|
84
|
+
it "sets the skip param to 0 if passed 0" do
|
85
|
+
Person.paginate(:page => 0).options[:skip].should == 0
|
86
|
+
end
|
87
|
+
|
88
|
+
it "sets the skip param to 0 if passed a string of 0" do
|
89
|
+
Person.paginate(:page => '0').options[:skip].should == 0
|
90
|
+
end
|
62
91
|
|
63
|
-
|
64
|
-
|
92
|
+
it "sets the skip param to 0 if the passed a string of 1" do
|
93
|
+
Person.paginate(:page => '1').options[:skip].should == 0
|
94
|
+
end
|
65
95
|
end
|
66
96
|
|
67
|
-
|
68
|
-
|
97
|
+
context "offset param" do
|
98
|
+
it "sets the skip param to 0 if passed 0" do
|
99
|
+
Person.paginate(:offset => 0).options[:skip].should == 0
|
100
|
+
end
|
101
|
+
|
102
|
+
it "sets the skip param to 0 if passed a string of 0" do
|
103
|
+
Person.paginate(:offset => '0').options[:skip].should == 0
|
104
|
+
end
|
105
|
+
|
106
|
+
it "sets the skip param to 1 if the passed a string of 1" do
|
107
|
+
Person.paginate(:offset => '1').options[:skip].should == 1
|
108
|
+
end
|
69
109
|
end
|
70
110
|
|
71
111
|
it "limits when passed a string param" do
|
@@ -78,12 +118,24 @@ describe Mongoid::Pagination do
|
|
78
118
|
end
|
79
119
|
|
80
120
|
context "results" do
|
81
|
-
|
82
|
-
|
121
|
+
context "page param" do
|
122
|
+
it "paginates correctly on the first page" do
|
123
|
+
Person.paginate(:page => 1, :limit => 2).to_a.should == [one, two]
|
124
|
+
end
|
125
|
+
|
126
|
+
it "paginates correctly on the second page" do
|
127
|
+
Person.paginate(:page => 2, :limit => 2).to_a.should == [three, four]
|
128
|
+
end
|
83
129
|
end
|
84
130
|
|
85
|
-
|
86
|
-
|
131
|
+
context "offset param" do
|
132
|
+
it "paginates correctly on the first page" do
|
133
|
+
Person.paginate(:offset => 0, :limit => 2).to_a.should == [one, two]
|
134
|
+
end
|
135
|
+
|
136
|
+
it "paginates correctly on the second page" do
|
137
|
+
Person.paginate(:page => 2, :limit => 2).to_a.should == [three, four]
|
138
|
+
end
|
87
139
|
end
|
88
140
|
end
|
89
141
|
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mongoid-pagination
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
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-
|
12
|
+
date: 2012-06-26 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: mongoid
|
16
|
-
requirement: &
|
16
|
+
requirement: &70234213653760 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ! '>='
|
@@ -21,7 +21,18 @@ dependencies:
|
|
21
21
|
version: '0'
|
22
22
|
type: :runtime
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *70234213653760
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: activesupport
|
27
|
+
requirement: &70234213653260 !ruby/object:Gem::Requirement
|
28
|
+
none: false
|
29
|
+
requirements:
|
30
|
+
- - ! '>='
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '0'
|
33
|
+
type: :runtime
|
34
|
+
prerelease: false
|
35
|
+
version_requirements: *70234213653260
|
25
36
|
description: A simple pagination module for Mongoid
|
26
37
|
email:
|
27
38
|
- ajsharp@gmail.com
|