simple_paginate 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +8 -0
- data/README.md +14 -0
- data/config/locales/{simple_paginate.yml → simple_paginate.en.yml} +0 -0
- data/lib/simple_paginate/models/active_record_extension.rb +18 -17
- data/lib/simple_paginate/models/active_record_model_extension.rb +53 -0
- data/lib/simple_paginate/version.rb +1 -1
- data/spec/simple_paginate/models/active_record_extension_spec.rb +35 -0
- metadata +5 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9ccfd558c3db4487692dce7e3e619020b59bb135
|
4
|
+
data.tar.gz: 4ceef5a49453b3fdf4662f1b2c5c3ae85a8b14ad
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4bb9548797d7d4dbedaf3a6a97247e7a3949b5023f05674d0a9440a972c5cc9a7ac4eaefccd30854558eab988c8308ab147d40beb35add414a363607cdbcd4cd
|
7
|
+
data.tar.gz: ed638195243abb718b5a0ba4000ba22fef39b1a77a7a4a7454f449f7cb832606ecb21b86a2f4dbe039fa358e2c7edf8594476520ce6d63fce0635b04c78afaf0
|
data/.travis.yml
ADDED
data/README.md
CHANGED
@@ -1,5 +1,7 @@
|
|
1
1
|
# simple_paginate
|
2
2
|
|
3
|
+
[![Build Status](https://travis-ci.org/xinminlabs/simple_paginate.png)](https://travis-ci.org/xinminlabs/simple_paginate)
|
4
|
+
|
3
5
|
Simple pagination solution for previous and next page navigation.
|
4
6
|
|
5
7
|
## Why simple_paginate
|
@@ -39,6 +41,9 @@ it fetches one more record (11 = 10 + 1) to calculate if there is a next page re
|
|
39
41
|
```
|
40
42
|
## perform a paginate query:
|
41
43
|
@users = User.paginate(:page => params[:page])
|
44
|
+
|
45
|
+
# or, use page and per scope:
|
46
|
+
@users = User.page(params[:page]).per(25)
|
42
47
|
```
|
43
48
|
|
44
49
|
### Helpers
|
@@ -75,3 +80,12 @@ SimplePaginate includes a handy template generator, To edit your paginator, run
|
|
75
80
|
```
|
76
81
|
% rails g simple_paginate:views
|
77
82
|
```
|
83
|
+
|
84
|
+
### Contribute
|
85
|
+
|
86
|
+
To run the test suite locally:
|
87
|
+
|
88
|
+
```
|
89
|
+
% bundle install
|
90
|
+
% rake spec:active_record_42
|
91
|
+
```
|
File without changes
|
@@ -1,26 +1,27 @@
|
|
1
|
+
require 'simple_paginate/models/active_record_model_extension'
|
2
|
+
|
1
3
|
module SimplePaginate
|
2
4
|
module ActiveRecordExtension
|
3
5
|
extend ActiveSupport::Concern
|
4
6
|
|
5
|
-
|
6
|
-
def
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
offset(per_page * (page - 1)).limit(per_page + 1).extending do
|
12
|
-
def current_page
|
13
|
-
(offset_value / (limit_value - 1)) + 1
|
14
|
-
end
|
15
|
-
|
16
|
-
def first_page?
|
17
|
-
current_page == 1
|
18
|
-
end
|
7
|
+
module ClassMethods
|
8
|
+
def inherited(kls)
|
9
|
+
super
|
10
|
+
kls.send(:include, SimplePaginate::ActiveRecordModelExtension)
|
11
|
+
end
|
12
|
+
end
|
19
13
|
|
20
|
-
|
21
|
-
|
22
|
-
|
14
|
+
included do
|
15
|
+
class << self
|
16
|
+
def inherited_with_simple_paginate(kls)
|
17
|
+
inherited_without_simple_paginate kls
|
18
|
+
kls.send(:include, SimplePaginate::ActiveRecordModelExtension)
|
23
19
|
end
|
20
|
+
alias_method_chain :inherited, :simple_paginate
|
21
|
+
end
|
22
|
+
|
23
|
+
self.descendants.each do |kls|
|
24
|
+
kls.send(:include, SimplePaginate::ActiveRecordModelExtension)
|
24
25
|
end
|
25
26
|
end
|
26
27
|
end
|
@@ -0,0 +1,53 @@
|
|
1
|
+
module SimplePaginate
|
2
|
+
module ActiveRecordModelExtension
|
3
|
+
extend ActiveSupport::Concern
|
4
|
+
|
5
|
+
included do
|
6
|
+
def self.paginate(options = {})
|
7
|
+
options = options.dup
|
8
|
+
page = options.delete(:page) || 1
|
9
|
+
per_page = options.delete(:per_page) || SimplePaginate.config.default_per_page
|
10
|
+
|
11
|
+
offset(per_page * (page - 1)).limit(per_page + 1).extending do
|
12
|
+
def current_page
|
13
|
+
(offset_value / (limit_value - 1)) + 1
|
14
|
+
end
|
15
|
+
|
16
|
+
def first_page?
|
17
|
+
current_page == 1
|
18
|
+
end
|
19
|
+
|
20
|
+
def last_page?
|
21
|
+
loaded? ? @actual_records.length < limit_value : count(:all) < limit_value
|
22
|
+
end
|
23
|
+
|
24
|
+
def per(num)
|
25
|
+
if (n = num.to_i) < 0 || !(/^\d/ =~ num.to_s)
|
26
|
+
self
|
27
|
+
elsif n.zero?
|
28
|
+
limit(n + 1)
|
29
|
+
else
|
30
|
+
limit(n + 1).offset(offset_value / (limit_value - 1) + 1)
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
private
|
35
|
+
|
36
|
+
def exec_queries
|
37
|
+
@actual_records = super
|
38
|
+
@records = if @actual_records.length > 1 && @actual_records.length == limit_value
|
39
|
+
@actual_records.first(limit_value - 1)
|
40
|
+
else
|
41
|
+
@actual_records
|
42
|
+
end
|
43
|
+
@records
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
def self.page(num = nil)
|
49
|
+
self.paginate(page: num)
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
@@ -35,6 +35,41 @@ describe SimplePaginate::ActiveRecordExtension do
|
|
35
35
|
|
36
36
|
it { expect(subject).to be_empty }
|
37
37
|
end
|
38
|
+
end
|
39
|
+
|
40
|
+
describe '#page' do
|
41
|
+
subject { User.page(page) }
|
42
|
+
|
43
|
+
context 'page 1' do
|
44
|
+
let(:page) { 1 }
|
45
|
+
|
46
|
+
it { expect(subject.first.name).to eq('user001') }
|
47
|
+
end
|
48
|
+
|
49
|
+
context 'page 2' do
|
50
|
+
let(:page) { 2 }
|
51
|
+
|
52
|
+
it { expect(subject.first.name).to eq('user026') }
|
53
|
+
end
|
54
|
+
|
55
|
+
context 'page < 1' do
|
56
|
+
let(:page) { 0 }
|
57
|
+
|
58
|
+
it { expect(subject.first.name).to eq('user001') }
|
59
|
+
end
|
60
|
+
|
61
|
+
context 'page > max page' do
|
62
|
+
let(:page) { 5 }
|
63
|
+
|
64
|
+
it { expect(subject).to be_empty }
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
describe '#per' do
|
69
|
+
context 'page 1 per 5' do
|
70
|
+
subject { User.page(1).per(5) }
|
71
|
+
it { expect(subject.last.name).to eq('user006') }
|
72
|
+
end
|
38
73
|
end
|
39
74
|
|
40
75
|
describe '#current_page' do
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: simple_paginate
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Yong Gu
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-03-
|
11
|
+
date: 2015-03-17 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -102,6 +102,7 @@ extensions: []
|
|
102
102
|
extra_rdoc_files: []
|
103
103
|
files:
|
104
104
|
- ".gitignore"
|
105
|
+
- ".travis.yml"
|
105
106
|
- Gemfile
|
106
107
|
- LICENSE
|
107
108
|
- LICENSE.txt
|
@@ -116,7 +117,7 @@ files:
|
|
116
117
|
- app/views/simple_paginate/_prev_page.html.erb
|
117
118
|
- app/views/simple_paginate/_prev_page.html.haml
|
118
119
|
- app/views/simple_paginate/_prev_page.html.slim
|
119
|
-
- config/locales/simple_paginate.yml
|
120
|
+
- config/locales/simple_paginate.en.yml
|
120
121
|
- gemfiles/.bundle/config
|
121
122
|
- gemfiles/active_record_42.gemfile
|
122
123
|
- gemfiles/active_record_42.gemfile.lock
|
@@ -130,6 +131,7 @@ files:
|
|
130
131
|
- lib/simple_paginate/helpers/paginator.rb
|
131
132
|
- lib/simple_paginate/helpers/tags.rb
|
132
133
|
- lib/simple_paginate/models/active_record_extension.rb
|
134
|
+
- lib/simple_paginate/models/active_record_model_extension.rb
|
133
135
|
- lib/simple_paginate/railtie.rb
|
134
136
|
- lib/simple_paginate/version.rb
|
135
137
|
- simple_paginate.gemspec
|