api_me 0.5.3 → 0.6.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.editorconfig +41 -0
- data/lib/api_me.rb +5 -3
- data/lib/api_me/pagination.rb +2 -3
- data/lib/api_me/sorting.rb +57 -0
- data/lib/api_me/version.rb +1 -1
- data/spec/acceptance/api/v1/posts_spec.rb +22 -0
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4e8bb2cb3b9c132ede191b0d7276d7cbeabf7252
|
4
|
+
data.tar.gz: 130bf19a960566c5dc4c5f6ab05cb78c387596ed
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a52cc62640bb368b1e661fcdacb0c43f87fc93f99e5d87acaa421ad6fa90287f95326521255e4a5d442f7b5f111f4b0da4f920123d4d822e23b172fdafd627db
|
7
|
+
data.tar.gz: 77004d9ae0c8728fa094744ad96fb2c37dbb9b2318ba41ab7c37f35d08daddd9111aeb77438296d9050c14aa7d1b6509b10fe1751027def01690821e82661fcd
|
data/.editorconfig
ADDED
@@ -0,0 +1,41 @@
|
|
1
|
+
# EditorConfig helps developers define and maintain consistent
|
2
|
+
# coding styles between different editors and IDEs
|
3
|
+
# editorconfig.org
|
4
|
+
|
5
|
+
root = true
|
6
|
+
|
7
|
+
|
8
|
+
[*]
|
9
|
+
end_of_line = lf
|
10
|
+
charset = utf-8
|
11
|
+
trim_trailing_whitespace = true
|
12
|
+
insert_final_newline = true
|
13
|
+
indent_style = space
|
14
|
+
indent_size = 2
|
15
|
+
|
16
|
+
[*.rb]
|
17
|
+
indent_style = space
|
18
|
+
indent_size = 2
|
19
|
+
|
20
|
+
[*.yml]
|
21
|
+
indent_style = space
|
22
|
+
indent_size = 2
|
23
|
+
|
24
|
+
[*.js]
|
25
|
+
indent_style = space
|
26
|
+
indent_size = 2
|
27
|
+
|
28
|
+
[*.hbs]
|
29
|
+
indent_style = space
|
30
|
+
indent_size = 2
|
31
|
+
|
32
|
+
[*.css]
|
33
|
+
indent_style = space
|
34
|
+
indent_size = 2
|
35
|
+
|
36
|
+
[*.html]
|
37
|
+
indent_style = space
|
38
|
+
indent_size = 2
|
39
|
+
|
40
|
+
[*.md]
|
41
|
+
trim_trailing_whitespace = false
|
data/lib/api_me.rb
CHANGED
@@ -5,6 +5,7 @@ require 'kaminari'
|
|
5
5
|
|
6
6
|
require 'api_me/version'
|
7
7
|
require 'api_me/base_filter'
|
8
|
+
require 'api_me/sorting'
|
8
9
|
require 'api_me/pagination'
|
9
10
|
|
10
11
|
module ApiMe
|
@@ -67,7 +68,8 @@ module ApiMe
|
|
67
68
|
def index
|
68
69
|
@policy_scope = policy_scope(resource_scope)
|
69
70
|
@filter_scope = filter_scope(@policy_scope)
|
70
|
-
@
|
71
|
+
@sorted_scope = ApiMe::Sorting.new(scope: @filter_scope, sort_params: params[:sort]).results
|
72
|
+
@pagination_object = ApiMe::Pagination.new(scope: @sorted_scope, page_params: params[:page])
|
71
73
|
|
72
74
|
render json: @pagination_object.results, each_serializer: serializer_klass, meta: { page: @pagination_object.page_meta }
|
73
75
|
end
|
@@ -147,7 +149,7 @@ module ApiMe
|
|
147
149
|
def resource_scope
|
148
150
|
model_klass.all
|
149
151
|
end
|
150
|
-
|
152
|
+
|
151
153
|
def filter_scope(scope)
|
152
154
|
filter_klass.new(
|
153
155
|
scope: scope,
|
@@ -158,7 +160,7 @@ module ApiMe
|
|
158
160
|
def params_klass_symbol
|
159
161
|
model_klass.name.demodulize.underscore.to_sym
|
160
162
|
end
|
161
|
-
|
163
|
+
|
162
164
|
def filters_hash
|
163
165
|
ids_filter_hash = params[:ids] ? { ids: params[:ids] } : {}
|
164
166
|
(filter_params || {}).merge(ids_filter_hash)
|
data/lib/api_me/pagination.rb
CHANGED
@@ -17,7 +17,6 @@ module ApiMe
|
|
17
17
|
|
18
18
|
def page_meta
|
19
19
|
return Hash.new unless paging?
|
20
|
-
|
21
20
|
{
|
22
21
|
size: page_size.nil? ? default_page_size : page_size,
|
23
22
|
offset: page_offset,
|
@@ -30,7 +29,7 @@ module ApiMe
|
|
30
29
|
protected
|
31
30
|
|
32
31
|
def page
|
33
|
-
self.scope = scope.page(self.page_offset ? page_offset : 1)
|
32
|
+
self.scope = Kaminari.paginate_array(scope).page(self.page_offset ? page_offset : 1)
|
34
33
|
self
|
35
34
|
end
|
36
35
|
|
@@ -38,7 +37,6 @@ module ApiMe
|
|
38
37
|
if page_size
|
39
38
|
self.scope = scope.per(page_size)
|
40
39
|
end
|
41
|
-
|
42
40
|
self
|
43
41
|
end
|
44
42
|
|
@@ -51,5 +49,6 @@ module ApiMe
|
|
51
49
|
def paging?
|
52
50
|
page_size || page_offset
|
53
51
|
end
|
52
|
+
|
54
53
|
end
|
55
54
|
end
|
@@ -0,0 +1,57 @@
|
|
1
|
+
module ApiMe
|
2
|
+
class Sorting
|
3
|
+
attr_accessor :sort_criteria, :sort_reverse, :scope
|
4
|
+
|
5
|
+
def initialize(scope:, sort_params:)
|
6
|
+
self.scope = scope
|
7
|
+
|
8
|
+
if sort_params
|
9
|
+
self.sort_criteria = sort_params[:criteria]
|
10
|
+
self.sort_reverse = sort_params[:reverse]
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
def results
|
15
|
+
sorting? ? sort.scope : scope
|
16
|
+
end
|
17
|
+
|
18
|
+
def sort_meta
|
19
|
+
return Hash.new unless sorting?
|
20
|
+
{
|
21
|
+
criteria: sort_criteria.is_blank? || sort_criteria === "" ? default_sort_criteria : sort_criteria,
|
22
|
+
reverse: sort_reverse,
|
23
|
+
record_count: scope.size,
|
24
|
+
total_records: scope.total_count,
|
25
|
+
}
|
26
|
+
end
|
27
|
+
|
28
|
+
protected
|
29
|
+
|
30
|
+
def sort
|
31
|
+
unless sort_criteria === ""
|
32
|
+
sort_p = sort_criteria
|
33
|
+
if sort_reverse === "true"
|
34
|
+
self.scope = scope.sort_by {|scope| scope[sort_p]}.reverse!
|
35
|
+
else
|
36
|
+
self.scope = scope.sort_by {|scope| scope[sort_p]}
|
37
|
+
end
|
38
|
+
self.scope
|
39
|
+
else
|
40
|
+
default_sort_criteria
|
41
|
+
sort.scope
|
42
|
+
end
|
43
|
+
self
|
44
|
+
end
|
45
|
+
|
46
|
+
private
|
47
|
+
|
48
|
+
def default_sort_criteria
|
49
|
+
sort_criteria = "id"
|
50
|
+
end
|
51
|
+
|
52
|
+
def sorting?
|
53
|
+
sort_criteria || sort_reverse
|
54
|
+
end
|
55
|
+
|
56
|
+
end
|
57
|
+
end
|
data/lib/api_me/version.rb
CHANGED
@@ -137,4 +137,26 @@ describe 'Posts API' do
|
|
137
137
|
|
138
138
|
expect(json['posts'].length).to eq(filtered_posts.count)
|
139
139
|
end
|
140
|
+
|
141
|
+
it 'sends posts reverse sorted by id' do
|
142
|
+
20.times do |i|
|
143
|
+
Post.create(name: 'Post' + i.to_s)
|
144
|
+
end
|
145
|
+
|
146
|
+
get '/api/v1/posts?sort%5Bcriteria%5D=id&sort%5Breverse%5D=true'
|
147
|
+
json = JSON.parse(last_response.body)
|
148
|
+
expect(json['posts'].last['name']).to eq('Post0')
|
149
|
+
end
|
150
|
+
|
151
|
+
it 'sends posts reverse sorted by id and paginated with a size of 10 and an offset of 1' do
|
152
|
+
20.times do |i|
|
153
|
+
Post.create(name: 'Post' + i.to_s)
|
154
|
+
end
|
155
|
+
|
156
|
+
get '/api/v1/posts?page%5Boffset%5D=1&page%5Bsize%5D=10&sort%5Bcriteria%5D=id&sort%5Breverse%5D=true'
|
157
|
+
json = JSON.parse(last_response.body)
|
158
|
+
expect(json['posts'].first['name']).to eq('Post19')
|
159
|
+
expect(json['posts'].length).to eq(10)
|
160
|
+
end
|
161
|
+
|
140
162
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: api_me
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.6.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Sam Clopton
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2016-05-
|
12
|
+
date: 2016-05-27 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activerecord
|
@@ -173,6 +173,7 @@ executables: []
|
|
173
173
|
extensions: []
|
174
174
|
extra_rdoc_files: []
|
175
175
|
files:
|
176
|
+
- ".editorconfig"
|
176
177
|
- ".gitignore"
|
177
178
|
- ".rubocop.yml"
|
178
179
|
- ".ruby-version"
|
@@ -187,6 +188,7 @@ files:
|
|
187
188
|
- lib/api_me/base_filter.rb
|
188
189
|
- lib/api_me/engine.rb
|
189
190
|
- lib/api_me/pagination.rb
|
191
|
+
- lib/api_me/sorting.rb
|
190
192
|
- lib/api_me/version.rb
|
191
193
|
- lib/generators/api_me/controller/USAGE
|
192
194
|
- lib/generators/api_me/controller/controller_generator.rb
|