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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 3e498dd3437d05ab8c98a4b11532efd50a94ad47
4
- data.tar.gz: 51ecfe8f3116d63716843e4f49411f57e41d20b4
3
+ metadata.gz: d19f3500e6ae273b71844d13a04b6453e4ba5aab
4
+ data.tar.gz: 6f98a514b3ca501ce382052c3a99eb32f6eecce8
5
5
  SHA512:
6
- metadata.gz: 97c116075f449c8ccbe5e083fd26cc93eef6e385e39f50a47ffdfe1d63c1d8cc51b73b7d1db198b13cd67d4003c338da3f112e8fb5edaae45dd10695accc1569
7
- data.tar.gz: 9bffca6d2017d5b366c609336feba6903e0b817ed7dbd35bd8c58cbd78509b93cc47513a1e9beceb90e1f126cf3e12711063848f48e58091aadeed6809cc185a
6
+ metadata.gz: c9c86404edcce88d0dd8c34bc165d8ac3ea3fb41f8d4947f8294b602eab9ebf953dc9387792d49a6d19608247d1523e00846200b0095309645df3a503ec7f1b9
7
+ data.tar.gz: bdcb492370f82c52681e6fd658d4f725f0288f016d3990bcbea6c0c7304ace5e53c787694ce7a2028a1999685dc9fa1ada035c070eae44751dfc7a0104f80171
data/README.md CHANGED
@@ -1,3 +1,49 @@
1
1
  # gris_paginator
2
2
 
3
- Simple pagination for Gris apps with Kaminari.
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 = {}, with: nil)
3
+ def paginate(klass, conditions: {}, sort: nil, with: nil)
4
4
  fail ArgumentError, ':with argument required' if with.nil?
5
- present klass.where(conditions)
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
@@ -1,3 +1,3 @@
1
1
  module GrisPaginator
2
- VERSION = '0.0.1'
2
+ VERSION = '0.0.2'
3
3
  end
@@ -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
- it 'limits the result set count based on params[:size]' do
31
- per_page = 10
32
- allow(endpoint).to receive(:params).and_return(size: per_page)
33
- result = endpoint.paginate(Beach, with: BeachesPresenter)
34
- expect(result.size).to eq per_page
35
- expect(result.current_page).to eq 1
36
- expect(result.total_pages).to eq total_count / per_page
37
- expect(result.total_count).to eq total_count
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
- it 'jumps to the page of results corresponding to params[:page]' do
41
- per_page = 2
42
- page = 2
43
- allow(endpoint).to receive(:params).and_return(size: per_page, page: page)
44
- result = endpoint.paginate(Beach, with: BeachesPresenter)
45
- expect(result.size).to eq per_page
46
- expect(result.first.name).to eq '2'
47
- expect(result.current_page).to eq page
48
- expect(result.total_pages).to eq total_count / per_page
49
- expect(result.total_count).to eq total_count
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
- it 'filters collection based on conditions argument' do
53
- allow(endpoint).to receive(:params).and_return({})
54
- result = endpoint.paginate(Beach, { name: '2' }, with: BeachesPresenter)
55
- expect(result.size).to eq 1
56
- expect(result.first.name).to eq '2'
57
- expect(result.current_page).to eq 1
58
- expect(result.total_pages).to eq 1
59
- expect(result.total_count).to eq 1
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
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: gris_paginator
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dylan Fareed