api_me 0.7.0 → 0.7.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +1 -1
- data/README.md +1 -1
- data/lib/api_me.rb +11 -3
- data/lib/api_me/model.rb +4 -0
- data/lib/api_me/pagination.rb +1 -1
- data/lib/api_me/sorting.rb +15 -13
- data/lib/api_me/version.rb +1 -1
- data/spec/acceptance/api/v1/posts_spec.rb +2 -4
- data/spec/internal/app/models/post.rb +1 -0
- data/spec/internal/app/models/test_model.rb +3 -1
- data/spec/internal/app/models/user.rb +1 -0
- data/spec/internal/app/serializers/post_serializer.rb +2 -0
- data/spec/internal/config/initializers/active_model_serializers.rb +1 -0
- data/spec/internal/db/schema.rb +1 -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: a97089cfc6b7bf6d795b611f125e707765cd0556
|
4
|
+
data.tar.gz: 15fb80cbe68170c4f56453bddd63726f5fbe3e93
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d560b2e6b10738a5f85d3634d2516860b796046e80cf0ff60001f0a056a7cd70e1a3770eac520fbf662ed31e3c183252a113308cddf70337ab42c36cd9c41e9f
|
7
|
+
data.tar.gz: a405ad926c4611f2b325a750ab3aecd04851b9b70565158cbed3b70ad7f5c87bdfd19a511b0b9f221df3d836a4bef5f2ada8bceded6e63d242e7a52b5554e990
|
data/.travis.yml
CHANGED
data/README.md
CHANGED
@@ -85,7 +85,7 @@ The ApiMe::BaseFilter is called if no filter exists for the resource, by default
|
|
85
85
|
|
86
86
|
### Sorting
|
87
87
|
|
88
|
-
To enable sorting
|
88
|
+
To enable sorting simply pass `sort` in your API request with `sortCiteria` and `sortReverse` as hash parameters. Associated models will need a third parameter passed in `associationCriteria`, meaning the attribute of the associated model to sort by.
|
89
89
|
|
90
90
|
Ember Example
|
91
91
|
````js
|
data/lib/api_me.rb
CHANGED
@@ -71,14 +71,14 @@ module ApiMe
|
|
71
71
|
@sorted_scope = sort_scope(@filter_scope, params[:sort])
|
72
72
|
@pagination_object = ApiMe::Pagination.new(scope: @sorted_scope, page_params: params[:page])
|
73
73
|
|
74
|
-
render json: @pagination_object.results, each_serializer: serializer_klass, meta: { page: @pagination_object.page_meta }
|
74
|
+
render json: @pagination_object.results, root: collection_root_key, each_serializer: serializer_klass, meta: { page: @pagination_object.page_meta }
|
75
75
|
end
|
76
76
|
|
77
77
|
def show
|
78
78
|
@object = find_resource
|
79
79
|
authorize @object
|
80
80
|
|
81
|
-
render json: @object, serializer: serializer_klass
|
81
|
+
render json: @object, root: singular_root_key, serializer: serializer_klass
|
82
82
|
end
|
83
83
|
|
84
84
|
def create
|
@@ -86,7 +86,7 @@ module ApiMe
|
|
86
86
|
authorize @object
|
87
87
|
@object.save!(object_params)
|
88
88
|
|
89
|
-
render status: 201, json: @object, serializer: serializer_klass
|
89
|
+
render status: 201, json: @object, root: singular_root_key, serializer: serializer_klass
|
90
90
|
rescue ActiveRecord::RecordInvalid => e
|
91
91
|
handle_errors(e)
|
92
92
|
end
|
@@ -112,6 +112,14 @@ module ApiMe
|
|
112
112
|
|
113
113
|
protected
|
114
114
|
|
115
|
+
def singular_root_key
|
116
|
+
model_klass.name.singularize.underscore
|
117
|
+
end
|
118
|
+
|
119
|
+
def collection_root_key
|
120
|
+
model_klass.name.pluralize.underscore
|
121
|
+
end
|
122
|
+
|
115
123
|
def object_params
|
116
124
|
params.require(params_klass_symbol).permit(*policy(@object || model_klass).permitted_attributes)
|
117
125
|
end
|
data/lib/api_me/model.rb
ADDED
data/lib/api_me/pagination.rb
CHANGED
data/lib/api_me/sorting.rb
CHANGED
@@ -1,17 +1,18 @@
|
|
1
1
|
module ApiMe
|
2
2
|
class Sorting
|
3
|
-
attr_accessor :sort_criteria, :sort_reverse, :scope
|
3
|
+
attr_accessor :sort_criteria, :sort_reverse, :sort_association, :scope
|
4
4
|
|
5
5
|
def initialize(scope:, sort_params:)
|
6
6
|
self.scope = scope
|
7
7
|
if sort_params
|
8
|
-
self.
|
8
|
+
self.sort_association = sort_params[:assoCriteria]
|
9
|
+
self.sort_criteria = sort_params[:criteria] || default_sort_criteria
|
9
10
|
self.sort_reverse = sort_params[:reverse]
|
10
11
|
end
|
11
12
|
end
|
12
13
|
|
13
14
|
def results
|
14
|
-
sorting? ? sort
|
15
|
+
sorting? ? sort(sort_criteria) : scope
|
15
16
|
end
|
16
17
|
|
17
18
|
def sort_meta
|
@@ -26,20 +27,22 @@ module ApiMe
|
|
26
27
|
|
27
28
|
protected
|
28
29
|
|
29
|
-
def sort
|
30
|
-
unless
|
31
|
-
|
30
|
+
def sort(criteria = default_sort_criteria)
|
31
|
+
unless sort_association == ""
|
32
|
+
criteria_class = criteria.camelize.constantize
|
33
|
+
|
32
34
|
if sort_reverse === "true"
|
33
|
-
self.scope = scope.
|
35
|
+
self.scope = scope.joins(criteria.to_sym).merge(criteria_class.order(sort_association => :desc))
|
34
36
|
else
|
35
|
-
self.scope = scope.
|
37
|
+
self.scope = scope.joins(criteria.to_sym).merge(criteria_class.order(sort_association => :asc))
|
36
38
|
end
|
37
|
-
self.scope
|
38
39
|
else
|
39
|
-
|
40
|
-
|
40
|
+
if sort_reverse === "true"
|
41
|
+
self.scope = scope.order(criteria => :desc)
|
42
|
+
else
|
43
|
+
self.scope = scope.order(criteria => :asc)
|
44
|
+
end
|
41
45
|
end
|
42
|
-
self
|
43
46
|
end
|
44
47
|
|
45
48
|
private
|
@@ -51,6 +54,5 @@ module ApiMe
|
|
51
54
|
def sorting?
|
52
55
|
sort_criteria || sort_reverse
|
53
56
|
end
|
54
|
-
|
55
57
|
end
|
56
58
|
end
|
data/lib/api_me/version.rb
CHANGED
@@ -58,7 +58,6 @@ describe 'Posts API' do
|
|
58
58
|
get '/api/v1/posts?page%5Boffset%5D=3'
|
59
59
|
json = JSON.parse(last_response.body)
|
60
60
|
expect(json['posts'].length).to eq(0)
|
61
|
-
|
62
61
|
end
|
63
62
|
|
64
63
|
it 'is page size of 10 working for default offset of 1' do
|
@@ -143,7 +142,7 @@ describe 'Posts API' do
|
|
143
142
|
Post.create(name: 'Post' + i.to_s)
|
144
143
|
end
|
145
144
|
|
146
|
-
get '/api/v1/posts?sort%5Bcriteria%5D=id&sort%5Breverse%5D=true'
|
145
|
+
get '/api/v1/posts?sort%5Bcriteria%5D=id&sort%5Breverse%5D=true&sort%5BassoCriteria%5D='
|
147
146
|
json = JSON.parse(last_response.body)
|
148
147
|
expect(json['posts'].last['name']).to eq('Post0')
|
149
148
|
end
|
@@ -153,10 +152,9 @@ describe 'Posts API' do
|
|
153
152
|
Post.create(name: 'Post' + i.to_s)
|
154
153
|
end
|
155
154
|
|
156
|
-
get '/api/v1/posts?page%5Boffset%5D=1&page%5Bsize%5D=10&sort%5Bcriteria%5D=id&sort%5Breverse%5D=true'
|
155
|
+
get '/api/v1/posts?page%5Boffset%5D=1&page%5Bsize%5D=10&sort%5Bcriteria%5D=id&sort%5Breverse%5D=true&sort%5BassoCriteria%5D='
|
157
156
|
json = JSON.parse(last_response.body)
|
158
157
|
expect(json['posts'].first['name']).to eq('Post19')
|
159
158
|
expect(json['posts'].length).to eq(10)
|
160
159
|
end
|
161
|
-
|
162
160
|
end
|
@@ -0,0 +1 @@
|
|
1
|
+
#ActiveModelSerializers.config.adapter = :json
|
data/spec/internal/db/schema.rb
CHANGED
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.7.
|
4
|
+
version: 0.7.1
|
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-
|
12
|
+
date: 2016-12-21 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activerecord
|
@@ -187,6 +187,7 @@ files:
|
|
187
187
|
- lib/api_me.rb
|
188
188
|
- lib/api_me/base_filter.rb
|
189
189
|
- lib/api_me/engine.rb
|
190
|
+
- lib/api_me/model.rb
|
190
191
|
- lib/api_me/pagination.rb
|
191
192
|
- lib/api_me/sorting.rb
|
192
193
|
- lib/api_me/version.rb
|
@@ -223,6 +224,7 @@ files:
|
|
223
224
|
- spec/internal/app/serializers/test_model_serializer.rb
|
224
225
|
- spec/internal/app/serializers/user_serializer.rb
|
225
226
|
- spec/internal/config/database.yml
|
227
|
+
- spec/internal/config/initializers/active_model_serializers.rb
|
226
228
|
- spec/internal/config/routes.rb
|
227
229
|
- spec/internal/db/schema.rb
|
228
230
|
- spec/internal/log/.gitignore
|