gris_paginator 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/README.md +47 -1
- data/lib/gris_paginator/pagination_helpers.rb +5 -2
- data/lib/gris_paginator/version.rb +1 -1
- data/spec/pagination_helpers_spec.rb +58 -27
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d19f3500e6ae273b71844d13a04b6453e4ba5aab
|
4
|
+
data.tar.gz: 6f98a514b3ca501ce382052c3a99eb32f6eecce8
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c9c86404edcce88d0dd8c34bc165d8ac3ea3fb41f8d4947f8294b602eab9ebf953dc9387792d49a6d19608247d1523e00846200b0095309645df3a503ec7f1b9
|
7
|
+
data.tar.gz: bdcb492370f82c52681e6fd658d4f725f0288f016d3990bcbea6c0c7304ace5e53c787694ce7a2028a1999685dc9fa1ada035c070eae44751dfc7a0104f80171
|
data/README.md
CHANGED
@@ -1,3 +1,49 @@
|
|
1
1
|
# gris_paginator
|
2
2
|
|
3
|
-
|
3
|
+
[gris_paginator](https://github.com/dylanfareed/gris-paginator) provides a simple pagination helper for [Gris](https://github.com/artsy/gris) apps with [Kaminari](https://github.com/amatsuda/kaminari).
|
4
|
+
|
5
|
+
gris_paginator is alpha software.
|
6
|
+
|
7
|
+
---
|
8
|
+
|
9
|
+
### Installation
|
10
|
+
|
11
|
+
gris_paginator is [available as a gem on rubygems](https://rubygems.org/gems/gris_paginator), to install it run:
|
12
|
+
|
13
|
+
```
|
14
|
+
gem install gris_paginator
|
15
|
+
```
|
16
|
+
|
17
|
+
Otherwise, if your project uses [Bundler](http://bundler.io/), add gris_paginator to your Gemfile:
|
18
|
+
|
19
|
+
```
|
20
|
+
gem 'gris_paginator'
|
21
|
+
```
|
22
|
+
|
23
|
+
And run:
|
24
|
+
|
25
|
+
```
|
26
|
+
$ bundle install
|
27
|
+
```
|
28
|
+
|
29
|
+
---
|
30
|
+
|
31
|
+
### Usage
|
32
|
+
|
33
|
+
```ruby
|
34
|
+
class RegistrationsEndpoint < Grape::API
|
35
|
+
namespace :registrations do
|
36
|
+
desc 'List existing registrations'
|
37
|
+
params do
|
38
|
+
requires :account_id, type: Integer, desc: 'Account ID to query for registrations.'
|
39
|
+
optional :sort, type: String, default: 'created_at DESC', desc: 'Sort order for registrations.'
|
40
|
+
optional :page, type: Integer, default: 1, desc: 'Current page.'
|
41
|
+
optional :size, type: Integer, default: 10, desc: 'Number of registrations to return per page.'
|
42
|
+
end
|
43
|
+
get do
|
44
|
+
conditions = { account_id: params[:account_id] }
|
45
|
+
paginate Registration, conditions: conditions, with: RegistrationsPresenter
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
```
|
@@ -1,8 +1,11 @@
|
|
1
1
|
module GrisPaginator
|
2
2
|
module PaginationHelpers
|
3
|
-
def paginate(klass, conditions
|
3
|
+
def paginate(klass, conditions: {}, sort: nil, with: nil)
|
4
4
|
fail ArgumentError, ':with argument required' if with.nil?
|
5
|
-
|
5
|
+
order = sort.blank? ? params[:sort] : sort
|
6
|
+
present klass
|
7
|
+
.order(order)
|
8
|
+
.where(conditions)
|
6
9
|
.page(params[:page])
|
7
10
|
.per(params[:size]),
|
8
11
|
with: with
|
@@ -11,7 +11,7 @@ describe GrisPaginator::PaginationHelpers do
|
|
11
11
|
context 'paginate' do
|
12
12
|
before do
|
13
13
|
total_count.times do |index|
|
14
|
-
Beach.create! name: index
|
14
|
+
Beach.create! name: "Playa Grande #{index}"
|
15
15
|
end
|
16
16
|
allow(endpoint).to receive(:env).and_return({})
|
17
17
|
end
|
@@ -27,36 +27,67 @@ describe GrisPaginator::PaginationHelpers do
|
|
27
27
|
expect(result.total_count).to eq total_count
|
28
28
|
end
|
29
29
|
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
30
|
+
context 'size' do
|
31
|
+
it 'limits the result set count based on params[:size]' do
|
32
|
+
per_page = 10
|
33
|
+
allow(endpoint).to receive(:params).and_return(size: per_page)
|
34
|
+
result = endpoint.paginate Beach, with: BeachesPresenter
|
35
|
+
expect(result.size).to eq per_page
|
36
|
+
expect(result.current_page).to eq 1
|
37
|
+
expect(result.total_pages).to eq total_count / per_page
|
38
|
+
expect(result.total_count).to eq total_count
|
39
|
+
end
|
38
40
|
end
|
39
41
|
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
42
|
+
context 'page' do
|
43
|
+
it 'jumps to the page of results corresponding to params[:page]' do
|
44
|
+
per_page = 2
|
45
|
+
page = 2
|
46
|
+
allow(endpoint).to receive(:params)
|
47
|
+
.and_return(size: per_page, page: page)
|
48
|
+
result = endpoint.paginate Beach, with: BeachesPresenter
|
49
|
+
expect(result.size).to eq per_page
|
50
|
+
expect(result.first.name).to eq 'Playa Grande 2'
|
51
|
+
expect(result.current_page).to eq page
|
52
|
+
expect(result.total_pages).to eq total_count / per_page
|
53
|
+
expect(result.total_count).to eq total_count
|
54
|
+
end
|
50
55
|
end
|
51
56
|
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
57
|
+
context 'conditions' do
|
58
|
+
it 'filters collection based on conditions argument' do
|
59
|
+
allow(endpoint).to receive(:params).and_return({})
|
60
|
+
result = endpoint.paginate Beach,
|
61
|
+
conditions: { name: 'Playa Grande 1' },
|
62
|
+
with: BeachesPresenter
|
63
|
+
expect(result.size).to eq 1
|
64
|
+
expect(result.first.id).to eq 2
|
65
|
+
expect(result.first.name).to eq 'Playa Grande 1'
|
66
|
+
expect(result.current_page).to eq 1
|
67
|
+
expect(result.total_pages).to eq 1
|
68
|
+
expect(result.total_count).to eq 1
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
context 'sort' do
|
73
|
+
it 'orders collection by default without sort argument or sort params' do
|
74
|
+
allow(endpoint).to receive(:params).and_return({})
|
75
|
+
result = endpoint.paginate Beach, with: BeachesPresenter
|
76
|
+
expect(result.first.id).to eq Beach.first.id
|
77
|
+
end
|
78
|
+
|
79
|
+
it 'orders collection based on sort argument' do
|
80
|
+
allow(endpoint).to receive(:params).and_return({})
|
81
|
+
result = endpoint.paginate Beach, sort: 'id DESC',
|
82
|
+
with: BeachesPresenter
|
83
|
+
expect(result.first.id).to eq Beach.last.id
|
84
|
+
end
|
85
|
+
|
86
|
+
it 'orders collection based on sort params' do
|
87
|
+
allow(endpoint).to receive(:params).and_return(sort: 'id DESC')
|
88
|
+
result = endpoint.paginate Beach, with: BeachesPresenter
|
89
|
+
expect(result.first.id).to eq Beach.last.id
|
90
|
+
end
|
60
91
|
end
|
61
92
|
end
|
62
93
|
end
|