api_me 0.7.1 → 0.8.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/Gemfile +1 -1
- data/README.md +22 -1
- data/api_me.gemspec +1 -1
- data/lib/api_me/model.rb +3 -1
- data/lib/api_me/pagination.rb +1 -1
- data/lib/api_me/sorting.rb +13 -15
- data/lib/api_me/version.rb +1 -1
- data/spec/acceptance/api/v1/posts_spec.rb +2 -2
- data/spec/internal/app/models/test_model.rb +2 -2
- data/spec/internal/app/serializers/user_serializer.rb +0 -1
- data/spec/internal/config/initializers/active_model_serializers.rb +1 -1
- metadata +4 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8aefa13bb6c4cb741b5b745f763b478ec279004d
|
4
|
+
data.tar.gz: 36d2113a88594b323d188d7f6f2812b2ae591109
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7e9a8cff6b5ca7301ef12b2e9b5cc6c41f7b39e3c0bb63249c26392d1f3a031c132462e1f035d2b4bcc18e364be52b90b60dbf61b318d3a7995d3b1f781e4d2a
|
7
|
+
data.tar.gz: 4db327ab00687bb5c1378dc8839de1d2917156919fb80a82a2008cab093f68bdcaec6697c7535cca523edf0e150a616e0e9f4f65963be00d3633389320d36824
|
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -12,6 +12,27 @@ Api controllers use the fantastic [Pundit](https://github.com/elabs/pundit) gem
|
|
12
12
|
|
13
13
|
The primary goal of this gem was to keep things simple so that customization is fairly straight forward by separating concerns and providing overrides. Reusing existing libraries was a primary goal during the design, hence the overall simplicity of this gem. We currently use this gem internally at [Inigo](inigo.io) and are committed to its ongoing maintenance.
|
14
14
|
|
15
|
+
### Upgrade from 0.7.X to 0.8.X
|
16
|
+
- Upgrade to the latest version of 0.7.X. Run the app/tests and check/fix all deprecations
|
17
|
+
- Upgrade to the latest version of 0.8.X, update active_model_serializers to 0.10.X.
|
18
|
+
- Add the following initializer:
|
19
|
+
`config/initializers/active_model_serializer.rb`
|
20
|
+
```rb
|
21
|
+
ActiveModelSerializers.config.adapter = :json
|
22
|
+
```
|
23
|
+
- Remove all `embed ...` methods from the serializers
|
24
|
+
- Update all belongs-to relationships to have FKs, serialize the FK id in all active model serializers instead of embeding the relationship (I.E. `has_one :foo` becomes `attributes :foo_id`)
|
25
|
+
- Update all has-many relationships to serialize the ids instead of embeding the relationship. (I.E. `has_many :foos` becomes `attributes :foo_ids`)
|
26
|
+
- Remove all has-one relationships and manually build a method to serialize the id. Ex:
|
27
|
+
```rb
|
28
|
+
attributes :foo_id
|
29
|
+
|
30
|
+
def foo_id
|
31
|
+
object.foo ? object.foo.id : nil
|
32
|
+
end
|
33
|
+
```
|
34
|
+
- Embeded has-one and belongs-to relationships look the same on the serializer, so look at the model to identify the differences.
|
35
|
+
|
15
36
|
### Installation
|
16
37
|
Add the gem to your Gemfile: `gem api_me`.
|
17
38
|
|
@@ -85,7 +106,7 @@ The ApiMe::BaseFilter is called if no filter exists for the resource, by default
|
|
85
106
|
|
86
107
|
### Sorting
|
87
108
|
|
88
|
-
To enable sorting
|
109
|
+
To enable sorting just pass `sort` in your request with `sortCiteria` and `sortReverse`.
|
89
110
|
|
90
111
|
Ember Example
|
91
112
|
````js
|
data/api_me.gemspec
CHANGED
@@ -21,7 +21,7 @@ Gem::Specification.new do |s|
|
|
21
21
|
s.add_runtime_dependency 'activerecord', '>= 3.2.0'
|
22
22
|
s.add_runtime_dependency 'activesupport', '>= 3.2.0'
|
23
23
|
s.add_runtime_dependency 'pundit', '~> 1.0'
|
24
|
-
s.add_runtime_dependency 'active_model_serializers', '~> 0.
|
24
|
+
s.add_runtime_dependency 'active_model_serializers', '~> 0.10.0'
|
25
25
|
s.add_runtime_dependency 'search_object', '~> 1.0'
|
26
26
|
s.add_runtime_dependency 'kaminari', '~> 0.16.3'
|
27
27
|
|
data/lib/api_me/model.rb
CHANGED
data/lib/api_me/pagination.rb
CHANGED
data/lib/api_me/sorting.rb
CHANGED
@@ -1,18 +1,17 @@
|
|
1
1
|
module ApiMe
|
2
2
|
class Sorting
|
3
|
-
attr_accessor :sort_criteria, :sort_reverse, :
|
3
|
+
attr_accessor :sort_criteria, :sort_reverse, :scope
|
4
4
|
|
5
5
|
def initialize(scope:, sort_params:)
|
6
6
|
self.scope = scope
|
7
7
|
if sort_params
|
8
|
-
self.
|
9
|
-
self.sort_criteria = sort_params[:criteria] || default_sort_criteria
|
8
|
+
self.sort_criteria = sort_params[:criteria]
|
10
9
|
self.sort_reverse = sort_params[:reverse]
|
11
10
|
end
|
12
11
|
end
|
13
12
|
|
14
13
|
def results
|
15
|
-
sorting? ? sort
|
14
|
+
sorting? ? sort.scope : scope
|
16
15
|
end
|
17
16
|
|
18
17
|
def sort_meta
|
@@ -27,22 +26,20 @@ module ApiMe
|
|
27
26
|
|
28
27
|
protected
|
29
28
|
|
30
|
-
def sort
|
31
|
-
unless
|
32
|
-
|
33
|
-
|
29
|
+
def sort
|
30
|
+
unless sort_criteria === ""
|
31
|
+
sort_p = sort_criteria
|
34
32
|
if sort_reverse === "true"
|
35
|
-
self.scope = scope.
|
33
|
+
self.scope = scope.sort_by {|scope| scope[sort_p]}.reverse!
|
36
34
|
else
|
37
|
-
self.scope = scope.
|
35
|
+
self.scope = scope.sort_by {|scope| scope[sort_p]}
|
38
36
|
end
|
37
|
+
self.scope
|
39
38
|
else
|
40
|
-
|
41
|
-
|
42
|
-
else
|
43
|
-
self.scope = scope.order(criteria => :asc)
|
44
|
-
end
|
39
|
+
default_sort_criteria
|
40
|
+
sort.scope
|
45
41
|
end
|
42
|
+
self
|
46
43
|
end
|
47
44
|
|
48
45
|
private
|
@@ -54,5 +51,6 @@ module ApiMe
|
|
54
51
|
def sorting?
|
55
52
|
sort_criteria || sort_reverse
|
56
53
|
end
|
54
|
+
|
57
55
|
end
|
58
56
|
end
|
data/lib/api_me/version.rb
CHANGED
@@ -142,7 +142,7 @@ describe 'Posts API' do
|
|
142
142
|
Post.create(name: 'Post' + i.to_s)
|
143
143
|
end
|
144
144
|
|
145
|
-
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'
|
146
146
|
json = JSON.parse(last_response.body)
|
147
147
|
expect(json['posts'].last['name']).to eq('Post0')
|
148
148
|
end
|
@@ -152,7 +152,7 @@ describe 'Posts API' do
|
|
152
152
|
Post.create(name: 'Post' + i.to_s)
|
153
153
|
end
|
154
154
|
|
155
|
-
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'
|
156
156
|
json = JSON.parse(last_response.body)
|
157
157
|
expect(json['posts'].first['name']).to eq('Post19')
|
158
158
|
expect(json['posts'].length).to eq(10)
|
@@ -1 +1 @@
|
|
1
|
-
|
1
|
+
ActiveModelSerializers.config.adapter = :json
|
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.8.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-
|
12
|
+
date: 2016-09-13 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activerecord
|
@@ -59,14 +59,14 @@ dependencies:
|
|
59
59
|
requirements:
|
60
60
|
- - "~>"
|
61
61
|
- !ruby/object:Gem::Version
|
62
|
-
version: 0.
|
62
|
+
version: 0.10.0
|
63
63
|
type: :runtime
|
64
64
|
prerelease: false
|
65
65
|
version_requirements: !ruby/object:Gem::Requirement
|
66
66
|
requirements:
|
67
67
|
- - "~>"
|
68
68
|
- !ruby/object:Gem::Version
|
69
|
-
version: 0.
|
69
|
+
version: 0.10.0
|
70
70
|
- !ruby/object:Gem::Dependency
|
71
71
|
name: search_object
|
72
72
|
requirement: !ruby/object:Gem::Requirement
|