napa_pagination 0.0.3 → 0.0.4

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: 07d5307088369425a0b17541a94234c47451b619
4
- data.tar.gz: 7c2069fcf395bbc9c4261abca4bc633b8a32af6c
3
+ metadata.gz: deb862f59da8ed1c1df3e7d7d8c8823b00109da8
4
+ data.tar.gz: f9698f0a91dad8b045dd850e1f8c4e8e07adb5dd
5
5
  SHA512:
6
- metadata.gz: 6f03cb1064bf82e50b2460f23abeecd936e1c06fe344f38d13f9be5e6583d5e9941efc646e0ac1f9545b6a2180cf52fd400ea3d17364737266d8a3b35ad74199
7
- data.tar.gz: df5563f6d04fa1849087082d4966bbae1408a7adf87721005b202de0870408218a554e1e756be24f6c64066c9152fe136f4a2f6213ec7879f1ddbb4010ca1780
6
+ metadata.gz: ae48eadddde8de0784b530f1c3fe2bb445d912b87413065592a26335fc47e9808c39fc1b5ccf0332ff158361a2e2d10842166f83711b309598303d4aa4fa0a6d
7
+ data.tar.gz: 94735c539549b6402268d27784d41f0793a36fad2f29592167f1fc789f996eab63e4f4acb0432fae868650beb88f36378e3bd145fe79aa4b74a7bb061777de31
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # NapaPagination
2
2
 
3
- A simple pagination representer for Napa
3
+ A simple pagination representer & results orderer for Napa
4
4
 
5
5
  ## Installation
6
6
 
@@ -16,6 +16,9 @@ Or install it yourself as:
16
16
 
17
17
  $ gem install napa_pagination
18
18
 
19
+
20
+ # Pagination
21
+
19
22
  ## Usage
20
23
  Once you have the gem installed, simply replace `represent` with the `paginate` when returning the response from your API. Below is an example based on the [Napa Quickstart Guide](https://github.com/bellycard/napa/blob/master/docs/quickstart.md).
21
24
 
@@ -45,7 +48,7 @@ class PeopleApi < Grape::API
45
48
  optional :per_page, type: Integer, desc: 'Results per page', default: 100
46
49
  end
47
50
  get do
48
- people = Person.filter(declared(params, include_missing: false))
51
+ people = Person.filter(declared(params.except(:per_page), include_missing: false))
49
52
  # represent people, with: PersonRepresenter
50
53
  paginate people, with: PersonRepresenter
51
54
  end
@@ -54,6 +57,60 @@ class PeopleApi < Grape::API
54
57
  end
55
58
  ```
56
59
 
60
+ # Results Ordering
61
+
62
+ Once the gem is installed, and you've replaced `represent` with the `paginate you'll just need to add one api paremeters `:sort_by`
63
+
64
+ ```ruby
65
+ class PeopleApi < Grape::API
66
+ desc 'Get a list of people'
67
+ params do
68
+ optional :ids,
69
+ type: String,
70
+ desc: 'comma separated person ids'
71
+ optional :sort_by,
72
+ type: Symbol,
73
+ desc: 'the key to sort the results by',
74
+ values: [:id, :name, :age]
75
+ end
76
+ get do
77
+
78
+ people = Person.filter(declared(params.except(:sort_by), include_missing: false))
79
+ represent people, with: PersonRepresenter
80
+ end
81
+
82
+ ...
83
+ end
84
+ ```
85
+ ```ruby
86
+ class PeopleApi < Grape::API
87
+ desc 'Get a list of people'
88
+ params do
89
+ optional :ids,
90
+ type: String,
91
+ desc: 'comma separated person ids'
92
+ optional :sort_by,
93
+ type: Symbol,
94
+ desc: 'the key to sort the results by',
95
+ values: [:id, :name, :age]
96
+ optional :sort_order,
97
+ type: Symbol,
98
+ desc: 'the order to sort the results by',
99
+ values: [:asc, :desc]
100
+ end
101
+ get do
102
+ people = Person.filter(declared(params.except(:sort_by, :sort_order), include_missing: false))
103
+ order_by_params!(people)
104
+
105
+ represent people, with: PersonRepresenter
106
+ end
107
+
108
+ ...
109
+ end
110
+ ```
111
+
112
+ You can also change the order in which the sort happens by providing a `sort_order` parameter. By default this will be ascending.
113
+
57
114
  ## Contributing
58
115
 
59
116
  1. Fork it ( http://github.com/<my-github-username>/napa_pagination/fork )
@@ -1,6 +1,7 @@
1
1
  require 'grape'
2
2
  require 'napa'
3
3
  require 'kaminari/grape'
4
+ require 'active_record'
4
5
 
5
6
  require 'napa_pagination/version'
6
7
  require 'napa_pagination/pagination'
@@ -10,7 +10,7 @@ module NapaPagination
10
10
  r[:pagination] = data.to_h
11
11
  end
12
12
  else
13
- return { data: with.new(data).to_hash(args)}
13
+ return { data: with.new(data).to_hash(args) }
14
14
  end
15
15
  end
16
16
 
@@ -21,6 +21,8 @@ module NapaPagination
21
21
  page = params.try(:page) || 1
22
22
  per_page = params.try(:per_page) || 25
23
23
 
24
+ order_by_params!(data) if data.is_a?(ActiveRecord::Relation) && data.size > 0
25
+
24
26
  if data.is_a?(Array)
25
27
  Kaminari.paginate_array(data).page(page).per(per_page)
26
28
  else
@@ -28,6 +30,14 @@ module NapaPagination
28
30
  end
29
31
  end
30
32
 
33
+ def order_by_params!(data)
34
+ if data.column_names.map(&:to_sym).include?(params[:sort_by])
35
+ sort_order = params.try(:sort_order) || :asc
36
+ data.order!(params[:sort_by] => sort_order)
37
+ end
38
+ data
39
+ end
40
+
31
41
  # extend all endpoints to include this
32
42
  Grape::Endpoint.send :include, self
33
43
  end
@@ -1,3 +1,3 @@
1
1
  module NapaPagination
2
- VERSION = "0.0.3"
2
+ VERSION = "0.0.4"
3
3
  end
@@ -21,10 +21,11 @@ Gem::Specification.new do |spec|
21
21
  spec.add_dependency 'grape'
22
22
  spec.add_dependency 'napa'
23
23
  spec.add_dependency 'kaminari'
24
-
24
+ spec.add_dependency 'activerecord'
25
+
25
26
  spec.add_development_dependency "bundler", "~> 1.5"
26
27
  spec.add_development_dependency "rake"
27
- spec.add_development_dependency 'rspec'
28
+ spec.add_development_dependency 'rspec', "~> 3.0"
28
29
  spec.add_development_dependency 'pry'
29
30
  spec.add_development_dependency 'activerecord'
30
31
  spec.add_development_dependency 'sqlite3'
@@ -7,17 +7,20 @@ class FooRepresenter < Napa::Representer; end
7
7
  describe NapaPagination::GrapeHelpers do
8
8
  before do
9
9
  @endpoint = Grape::Endpoint.new(nil, {path: '/test', method: :get})
10
+ allow(@endpoint).to receive(:params).and_return({ })
10
11
  end
11
-
12
+
13
+ after do
14
+ Foo.destroy_all
15
+ end
16
+
12
17
  context '#represent_pagination' do
13
18
  before do
14
19
  Foo.create(word: 'bar')
15
20
  Foo.create(word: 'baz')
16
21
  end
17
22
 
18
- after do
19
- Foo.destroy_all
20
- end
23
+
21
24
 
22
25
  it 'returns the collection if it is already paginated' do
23
26
  objects = Kaminari.paginate_array([Foo.new, Foo.new, Foo.new]).page(1)
@@ -88,4 +91,36 @@ describe NapaPagination::GrapeHelpers do
88
91
  expect(output[:pagination][:total_count]).to eq(3)
89
92
  end
90
93
  end
94
+
95
+ context '#sort_by_params' do
96
+ before do
97
+ @added_first = Foo.create(word: 'b')
98
+ @asc_first = Foo.create(word: 'a')
99
+ @asc_last = Foo.create(word: 'c')
100
+ end
101
+
102
+
103
+ it 'orders the array in ascending order' do
104
+ allow(@endpoint).to receive(:params).and_return(Hashie::Mash.new({ sort_by: :word, sort_order: :asc }))
105
+
106
+ output = @endpoint.paginate(Foo.all, with: FooRepresenter)
107
+ expect(output[:data].first['word']).to eql(@asc_first.word)
108
+ expect(output[:data][1]['word']).to eql(@added_first.word)
109
+ expect(output[:data].last['word']).to eql(@asc_last.word)
110
+ end
111
+
112
+ it 'does nothing if there are not sort params' do
113
+
114
+ output = @endpoint.paginate(Foo.all, with: FooRepresenter)
115
+ expect(output[:data].first['word']).to eql(@added_first.word)
116
+ end
117
+
118
+ it 'orders the array in descending order' do
119
+ allow(@endpoint).to receive(:params).and_return(Hashie::Mash.new({ sort_by: :word, sort_order: :desc }))
120
+
121
+ output = @endpoint.paginate(Foo.all, with: FooRepresenter)
122
+ expect(output[:data].first['word']).to eql(@asc_last.word)
123
+ expect(output[:data].last['word']).to eql(@asc_first.word)
124
+ end
125
+ end
91
126
  end
data/spec/spec_helper.rb CHANGED
@@ -13,5 +13,9 @@ RSpec.configure do |config|
13
13
 
14
14
  class Foo < ActiveRecord::Base
15
15
  end
16
+
17
+ class FooRepresenter < Napa::Representer
18
+ property :word, type: String
19
+ end
16
20
  end
17
21
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: napa_pagination
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Darby Frey
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-06-12 00:00:00.000000000 Z
11
+ date: 2014-06-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: grape
@@ -52,6 +52,20 @@ dependencies:
52
52
  - - '>='
53
53
  - !ruby/object:Gem::Version
54
54
  version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: activerecord
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
55
69
  - !ruby/object:Gem::Dependency
56
70
  name: bundler
57
71
  requirement: !ruby/object:Gem::Requirement
@@ -84,16 +98,16 @@ dependencies:
84
98
  name: rspec
85
99
  requirement: !ruby/object:Gem::Requirement
86
100
  requirements:
87
- - - '>='
101
+ - - ~>
88
102
  - !ruby/object:Gem::Version
89
- version: '0'
103
+ version: '3.0'
90
104
  type: :development
91
105
  prerelease: false
92
106
  version_requirements: !ruby/object:Gem::Requirement
93
107
  requirements:
94
- - - '>='
108
+ - - ~>
95
109
  - !ruby/object:Gem::Version
96
- version: '0'
110
+ version: '3.0'
97
111
  - !ruby/object:Gem::Dependency
98
112
  name: pry
99
113
  requirement: !ruby/object:Gem::Requirement
@@ -205,7 +219,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
205
219
  version: '0'
206
220
  requirements: []
207
221
  rubyforge_project:
208
- rubygems_version: 2.1.11
222
+ rubygems_version: 2.0.7
209
223
  signing_key:
210
224
  specification_version: 4
211
225
  summary: A simple pagination representer for Napa