grape-active_model_serializers 1.3.2 → 1.4.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/.rubocop_todo.yml +3 -0
- data/.travis.yml +32 -11
- data/CHANGELOG.md +40 -0
- data/CONTRIBUTING.md +126 -0
- data/README.md +93 -60
- data/UPGRADING.md +100 -0
- data/grape-active_model_serializers.gemspec +2 -1
- data/lib/grape-active_model_serializers/formatter.rb +6 -2
- data/lib/grape-active_model_serializers/version.rb +1 -1
- data/spec/grape-active_model_serializers/versioned_api_formatter_spec.rb +67 -0
- data/spec/support/serializers/v1/user_serializer.rb +5 -0
- metadata +26 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c7f60aa1ac797001ff0756f457621d9ff5e99792
|
4
|
+
data.tar.gz: 5ba457a7a9b482bae17e354bef91425659c0821c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f3207365ca7d238a108f87045f21aa493155b836c00c54fbe141f7519feb851828ca7c94dced658404a01db3ae9053b579f2521c7a300b4d8e06f1ecef34b66c
|
7
|
+
data.tar.gz: 6c9646fc114bb51c3f8acf6d30c890283b5a9d95a8c7f8328fcf6010e21970109aba81c9019ea2b5bbf6250d328d5f29ba8cb12e538a7a39f85f1677d64eb4de
|
data/.rubocop_todo.yml
CHANGED
data/.travis.yml
CHANGED
@@ -2,14 +2,35 @@ language: ruby
|
|
2
2
|
|
3
3
|
sudo: false
|
4
4
|
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
5
|
+
matrix:
|
6
|
+
include:
|
7
|
+
- rvm: 2.3.1
|
8
|
+
env: GRAPE_VERSION=0.8.0
|
9
|
+
- rvm: 2.3.1
|
10
|
+
env: GRAPE_VERSION=0.9.0
|
11
|
+
- rvm: 2.3.1
|
12
|
+
env: GRAPE_VERSION=0.10.1
|
13
|
+
- rvm: 2.3.1
|
14
|
+
env: GRAPE_VERSION=0.12.0
|
15
|
+
- rvm: 2.3.1
|
16
|
+
env: GRAPE_VERSION=0.13.0
|
17
|
+
- rvm: 2.3.1
|
18
|
+
env: GRAPE_VERSION=0.14.0
|
19
|
+
- rvm: 2.3.1
|
20
|
+
env: GRAPE_VERSION=0.15.0
|
21
|
+
- rvm: 2.3.1
|
22
|
+
env: GRAPE_VERSION=0.16.2
|
23
|
+
- rvm: 2.3.1
|
24
|
+
env: GRAPE_VERSION=HEAD
|
25
|
+
- rvm: 2.3.0
|
26
|
+
- rvm: 2.2.5
|
27
|
+
- rvm: 2.1
|
28
|
+
- rvm: rbx-2
|
29
|
+
- rvm: jruby-19mode
|
30
|
+
- rvm: ruby-head
|
31
|
+
- rvm: jruby-head
|
32
|
+
allow_failures:
|
33
|
+
- rvm: rbx-2
|
34
|
+
- rvm: jruby-19mode
|
35
|
+
- rvm: ruby-head
|
36
|
+
- rvm: jruby-head
|
data/CHANGELOG.md
ADDED
@@ -0,0 +1,40 @@
|
|
1
|
+
## Changelog
|
2
|
+
|
3
|
+
### 1.5.0 (Next)
|
4
|
+
|
5
|
+
* Your contribution here.
|
6
|
+
|
7
|
+
### 1.4.0 (July 14, 2016)
|
8
|
+
|
9
|
+
* [#49](https://github.com/jrhe/grape-active_model_serializers/pull/49): Adds support for active model serializer namespace - [@syntaxTerr0r](https://github.com/syntaxTerr0r).
|
10
|
+
* [#36](https://github.com/jrhe/grape-active_model_serializers/pull/36), [#50](https://github.com/jrhe/grape-active_model_serializers/pull/50): Added support through Grape 0.16.x - [@dblock](https://github.com/dblock).
|
11
|
+
|
12
|
+
### v1.3.2 (February 27, 2015)
|
13
|
+
|
14
|
+
* [#39](https://github.com/jrhe/grape-active_model_serializers/pull/39): Look for namespace and other options to configure serializers - [@jwkoelewijn](https://github.com/jwkoelewijn).
|
15
|
+
* [#40](https://github.com/jrhe/grape-active_model_serializers/pull/40): Use env to pass AMS meta around - [@dblock](https://github.com/dblock).
|
16
|
+
|
17
|
+
### v1.3.1 (November 20, 2014)
|
18
|
+
|
19
|
+
* [#30](https://github.com/jrhe/grape-active_model_serializers/pull/30): Read options from default_serializer_options - [@siong1987](https://github.com/siong1987).
|
20
|
+
* [#24](https://github.com/jrhe/grape-active_model_serializers/pull/24): Makes it possible to use `current_user` within serializers - [@sonxurxo](https://github.com/sonxurxo).
|
21
|
+
|
22
|
+
### v1.2.1 (July 23, 2014)
|
23
|
+
|
24
|
+
* [#21](https://github.com/jrhe/grape-active_model_serializers/pull/21): Correctly fetch serialization scope - [@radanskoric](https://github.com/radanskoric).
|
25
|
+
* [#18](https://github.com/jrhe/grape-active_model_serializers/pull/18): Added support for active model serializer 0.9.x - [@sbounmy](https://github.com/sbounmy).
|
26
|
+
|
27
|
+
* [#15](https://github.com/jrhe/grape-active_model_serializers/pull/15): Added `render` syntactic sugar - [@zph](https://github.com/zph).
|
28
|
+
* [#14](https://github.com/jrhe/grape-active_model_serializers/pull/14): Fix: `default_root` method to support symbol route in Grape - [@wjp2013](https://github.com/wjp2013).
|
29
|
+
* [#12](https://github.com/jrhe/grape-active_model_serializers/pull/12): Added support for `current_user` - [@kpassapk](https://github.com/kpassapk).
|
30
|
+
* [#11](https://github.com/jrhe/grape-active_model_serializers/pull/11): Fixed require path - [@schickling](https://github.com/schickling).
|
31
|
+
|
32
|
+
### v1.0.1 (September 9, 2013)
|
33
|
+
|
34
|
+
* [#6](https://github.com/jrhe/grape-active_model_serializers/pull/6): Conform to ActiveModel::Serializers way of determining array-ness - [@tfe](https://github.com/tfe).
|
35
|
+
* [#4](https://github.com/jrhe/grape-active_model_serializers/pull/4): Support for namespace options and rely more on active_model_serializers - [@johnallen3d](https://github.com/johnallen3d).
|
36
|
+
* [#1](https://github.com/jrhe/grape-active_model_serializers/pull/1): Fix: Grape::ActiveModelSerializers for models with compound names - [@george](https://github.com/george).
|
37
|
+
|
38
|
+
### v1.0.0
|
39
|
+
|
40
|
+
* Initial public release - [@jrhe](https://github.com/jrhe).
|
data/CONTRIBUTING.md
ADDED
@@ -0,0 +1,126 @@
|
|
1
|
+
# Contributing to Grape-Swagger
|
2
|
+
|
3
|
+
This project is work of [many contributors](https://github.com/ruby-grape/grape-active_model_serializers/graphs/contributors).
|
4
|
+
You're encouraged to submit [pull requests](https://github.com/ruby-grape/grape-active_model_serializers/pulls),
|
5
|
+
[propose features and discuss issues](https://github.com/ruby-grape/grape-active_model_serializers/issues).
|
6
|
+
When in doubt, ask a question in the [Grape Google Group](http://groups.google.com/group/ruby-grape).
|
7
|
+
|
8
|
+
In the examples below, substitute your Github username for `contributor` in URLs.
|
9
|
+
|
10
|
+
## Fork the Project
|
11
|
+
|
12
|
+
Fork the [project on Github](https://github.com/ruby-grape/grape-active_model_serializers) and check out your copy.
|
13
|
+
|
14
|
+
```
|
15
|
+
git clone https://github.com/contributor/grape-active_model_serializers.git
|
16
|
+
cd grape-active_model_serializers
|
17
|
+
git remote add upstream https://github.com/ruby-grape/grape-active_model_serializers.git
|
18
|
+
```
|
19
|
+
|
20
|
+
## Create a Topic Branch
|
21
|
+
|
22
|
+
Make sure your fork is up-to-date and create a topic branch for your feature or bug fix.
|
23
|
+
|
24
|
+
```
|
25
|
+
git checkout master
|
26
|
+
git pull upstream master
|
27
|
+
git checkout -b my-feature-branch
|
28
|
+
```
|
29
|
+
|
30
|
+
## Bundle Install and Test
|
31
|
+
|
32
|
+
Ensure that you can build the project and run tests.
|
33
|
+
|
34
|
+
```
|
35
|
+
bundle install
|
36
|
+
bundle exec rake
|
37
|
+
```
|
38
|
+
|
39
|
+
## Write Tests
|
40
|
+
|
41
|
+
Try to write a test that reproduces the problem you're trying to fix or describes a feature that you want to build.
|
42
|
+
Add to [spec](spec).
|
43
|
+
|
44
|
+
We definitely appreciate pull requests that highlight or reproduce a problem, even without a fix.
|
45
|
+
|
46
|
+
## Write Code
|
47
|
+
|
48
|
+
Implement your feature or bug fix.
|
49
|
+
|
50
|
+
Ruby style is enforced with [RuboCop](https://github.com/bbatsov/rubocop).
|
51
|
+
Run `bundle exec rubocop` and fix any style issues highlighted.
|
52
|
+
|
53
|
+
Make sure that `bundle exec rake` completes without errors.
|
54
|
+
|
55
|
+
## Write Documentation
|
56
|
+
|
57
|
+
Document any external behavior in the [README](README.md).
|
58
|
+
|
59
|
+
## Update Changelog
|
60
|
+
|
61
|
+
Add a line to [CHANGELOG](CHANGELOG.md) under *Next Release*.
|
62
|
+
Make it look like every other line, including your name and link to your Github account.
|
63
|
+
|
64
|
+
## Commit Changes
|
65
|
+
|
66
|
+
Make sure git knows your name and email address:
|
67
|
+
|
68
|
+
```
|
69
|
+
git config --global user.name "Your Name"
|
70
|
+
git config --global user.email "contributor@example.com"
|
71
|
+
```
|
72
|
+
|
73
|
+
Writing good commit logs is important. A commit log should describe what changed and why.
|
74
|
+
|
75
|
+
```
|
76
|
+
git add ...
|
77
|
+
git commit
|
78
|
+
```
|
79
|
+
|
80
|
+
## Push
|
81
|
+
|
82
|
+
```
|
83
|
+
git push origin my-feature-branch
|
84
|
+
```
|
85
|
+
|
86
|
+
## Make a Pull Request
|
87
|
+
|
88
|
+
Go to https://github.com/contributor/grape-active_model_serializers and select your feature branch.
|
89
|
+
Click the 'Pull Request' button and fill out the form. Pull requests are usually reviewed within a few days.
|
90
|
+
|
91
|
+
## Rebase
|
92
|
+
|
93
|
+
If you've been working on a change for a while, rebase with upstream/master.
|
94
|
+
|
95
|
+
```
|
96
|
+
git fetch upstream
|
97
|
+
git rebase upstream/master
|
98
|
+
git push origin my-feature-branch -f
|
99
|
+
```
|
100
|
+
|
101
|
+
## Update CHANGELOG Again
|
102
|
+
|
103
|
+
Update the [CHANGELOG](CHANGELOG.md) with the pull request number. A typical entry looks as follows.
|
104
|
+
|
105
|
+
```
|
106
|
+
* [#123](https://github.com/ruby-grape/grape-active_model_serializers/pull/123): Reticulated splines - [@contributor](https://github.com/contributor).
|
107
|
+
```
|
108
|
+
|
109
|
+
Amend your previous commit and force push the changes.
|
110
|
+
|
111
|
+
```
|
112
|
+
git commit --amend
|
113
|
+
git push origin my-feature-branch -f
|
114
|
+
```
|
115
|
+
|
116
|
+
## Check on Your Pull Request
|
117
|
+
|
118
|
+
Go back to your pull request after a few minutes and see whether it passed muster with Travis-CI. Everything should look green, otherwise fix issues and amend your commit as described above.
|
119
|
+
|
120
|
+
## Be Patient
|
121
|
+
|
122
|
+
It's likely that your change will not be merged and that the nitpicky maintainers will ask you to do more, or fix seemingly benign problems. Hang on there!
|
123
|
+
|
124
|
+
## Thank You
|
125
|
+
|
126
|
+
Please do know that we really appreciate and value your time and work. We love you, really.
|
data/README.md
CHANGED
@@ -2,24 +2,17 @@
|
|
2
2
|
|
3
3
|
Use [active_model_serializers](https://github.com/rails-api/active_model_serializers) with [Grape](https://github.com/intridea/grape)!
|
4
4
|
|
5
|
-
[
|
5
|
+
[](http://travis-ci.org/ruby-grape/grape-active_model_serializers) [](https://gemnasium.com/ruby-grape/grape-active_model_serializers) [](https://codeclimate.com/github/jrhe/grape-active_model_serializers)
|
10
6
|
|
11
7
|
## Installation
|
12
8
|
|
13
|
-
Add the `grape` and `grape-active_model_serializers` gems to Gemfile
|
9
|
+
Add the `grape` and `grape-active_model_serializers` gems to Gemfile and run `bundle install`.
|
14
10
|
|
15
11
|
```ruby
|
16
|
-
gem 'grape'
|
17
12
|
gem 'grape-active_model_serializers'
|
18
13
|
```
|
19
14
|
|
20
|
-
|
21
|
-
|
22
|
-
bundle
|
15
|
+
See [UPGRADING](UPGRADING.md) if you're upgrading from a previous version.
|
23
16
|
|
24
17
|
## Usage
|
25
18
|
|
@@ -30,7 +23,6 @@ And then execute:
|
|
30
23
|
require 'grape-active_model_serializers'
|
31
24
|
```
|
32
25
|
|
33
|
-
|
34
26
|
### Tell your API to use Grape::Formatter::ActiveModelSerializers
|
35
27
|
|
36
28
|
```ruby
|
@@ -40,8 +32,7 @@ class API < Grape::API
|
|
40
32
|
end
|
41
33
|
```
|
42
34
|
|
43
|
-
|
44
|
-
### Writing serializers
|
35
|
+
### Writing Serializers
|
45
36
|
|
46
37
|
See [active_model_serializers](https://github.com/rails-api/active_model_serializers)
|
47
38
|
|
@@ -59,8 +50,11 @@ end
|
|
59
50
|
```
|
60
51
|
In this case, as User objects are being returned, grape-active_model_serializers will look for a serializer named UserSerializer.
|
61
52
|
|
62
|
-
### Array
|
63
|
-
|
53
|
+
### Array Roots
|
54
|
+
|
55
|
+
When serializing an array, the array root is set to the innermost namespace name if there is one, otherwise it is set to the route name.
|
56
|
+
|
57
|
+
In the following API the array root is `users`.
|
64
58
|
|
65
59
|
```ruby
|
66
60
|
namespace :users do
|
@@ -68,16 +62,87 @@ namespace :users do
|
|
68
62
|
@user = User.find(params[:id])
|
69
63
|
end
|
70
64
|
end
|
71
|
-
# root = users
|
72
65
|
```
|
73
66
|
|
67
|
+
In the following example the array root is `people`.
|
68
|
+
|
74
69
|
```ruby
|
75
70
|
get "people" do
|
76
71
|
@user = User.all
|
77
72
|
end
|
78
|
-
# root = people
|
79
73
|
```
|
80
74
|
|
75
|
+
### API Versioning
|
76
|
+
|
77
|
+
If your Grape API is versioned you must namespace your serializers accordingly.
|
78
|
+
|
79
|
+
For example, given the following API.
|
80
|
+
|
81
|
+
```ruby
|
82
|
+
module CandyBar
|
83
|
+
class Core < Grape::API
|
84
|
+
version 'candy_bar', using: :header, vendor: 'acme'
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
module Chocolate
|
89
|
+
class Core < Grape::API
|
90
|
+
version 'chocolate', using: :header, vendor: 'acme'
|
91
|
+
end
|
92
|
+
end
|
93
|
+
|
94
|
+
class API < Grape::API
|
95
|
+
format :json
|
96
|
+
formatter :json, Grape::Formatter::ActiveModelSerializers
|
97
|
+
|
98
|
+
mount CandyBar::Core
|
99
|
+
mount Chocolate::Core
|
100
|
+
end
|
101
|
+
```
|
102
|
+
|
103
|
+
Namespace your serializers according to each version.
|
104
|
+
|
105
|
+
```ruby
|
106
|
+
module CandyBar
|
107
|
+
class UserSerializer < ActiveModel::Serializer
|
108
|
+
attributes :first_name, :last_name, :email
|
109
|
+
end
|
110
|
+
end
|
111
|
+
|
112
|
+
module Chocolate
|
113
|
+
class UserSerializer < ActiveModel::Serializer
|
114
|
+
attributes :first_name, :last_name
|
115
|
+
end
|
116
|
+
end
|
117
|
+
```
|
118
|
+
|
119
|
+
This keeps serializers organized.
|
120
|
+
|
121
|
+
```
|
122
|
+
app
|
123
|
+
└── api
|
124
|
+
├── chocolate
|
125
|
+
└── core.rb
|
126
|
+
└── candy_bar
|
127
|
+
└── core.rb
|
128
|
+
api.rb
|
129
|
+
└── serializers
|
130
|
+
├── chocolate
|
131
|
+
└── user_serializer.rb
|
132
|
+
└── candy_bar
|
133
|
+
└── user_serializer.rb
|
134
|
+
```
|
135
|
+
|
136
|
+
Or as follows.
|
137
|
+
|
138
|
+
```
|
139
|
+
└── serializers
|
140
|
+
├── chocolate_user_serializer.rb
|
141
|
+
└── candy_bar_user_serializer.rb
|
142
|
+
```
|
143
|
+
|
144
|
+
ActiveModelSerializer will fetch automatically the right serializer to render.
|
145
|
+
|
81
146
|
### Manually specifying serializer options
|
82
147
|
|
83
148
|
```ruby
|
@@ -99,7 +164,7 @@ namespace 'foo', serializer: BarSerializer do
|
|
99
164
|
end
|
100
165
|
```
|
101
166
|
|
102
|
-
### Custom
|
167
|
+
### Custom Metadata
|
103
168
|
|
104
169
|
```ruby
|
105
170
|
# Control any additional metadata using meta and meta_key
|
@@ -109,23 +174,21 @@ get "/homes"
|
|
109
174
|
end
|
110
175
|
```
|
111
176
|
|
112
|
-
###
|
177
|
+
### Default Serializer Options
|
113
178
|
|
114
179
|
```ruby
|
115
180
|
helpers do
|
116
181
|
def default_serializer_options
|
117
|
-
{only: params[:only], except: params[:except]}
|
182
|
+
{ only: params[:only], except: params[:except] }
|
118
183
|
end
|
119
184
|
end
|
120
185
|
```
|
121
186
|
|
122
|
-
###
|
187
|
+
### Current User
|
123
188
|
|
124
|
-
One of the nice features of ActiveModel::Serializers is that it
|
125
|
-
provides access to the authorization context via the `current_user`.
|
189
|
+
One of the nice features of ActiveModel::Serializers is that it provides access to the authorization context via the `current_user`.
|
126
190
|
|
127
|
-
In Grape, you can get the same behavior by defining a `current_user`
|
128
|
-
helper method:
|
191
|
+
In Grape, you can get the same behavior by defining a `current_user` helper method.
|
129
192
|
|
130
193
|
```ruby
|
131
194
|
helpers do
|
@@ -139,18 +202,18 @@ helpers do
|
|
139
202
|
end
|
140
203
|
```
|
141
204
|
|
142
|
-
Then, in your serializer, you could show or hide some elements
|
143
|
-
based on the current user's permissions:
|
205
|
+
Then, in your serializer, you could show or hide some elements based on the current user's permissions.
|
144
206
|
|
145
207
|
```ruby
|
146
208
|
class PostSerializer < ActiveModel::Serializer
|
147
|
-
...
|
148
209
|
def include_admin_comments?
|
149
210
|
current_user.roles.member? :admin
|
150
211
|
end
|
151
212
|
end
|
152
213
|
```
|
153
214
|
|
215
|
+
*Note*: in the [0.9.x stable version of active model serializers](https://github.com/rails-api/active_model_serializers/tree/0-9-stable#customizing-scope), you have to access current user on scope - so `scope.current_user`.
|
216
|
+
|
154
217
|
### Full Example
|
155
218
|
|
156
219
|
```ruby
|
@@ -171,40 +234,10 @@ end
|
|
171
234
|
API.new.get "/home" # => '{ user: { first_name: "JR", last_name: "HE" } }'
|
172
235
|
```
|
173
236
|
|
174
|
-
|
175
|
-
## RSpec
|
176
|
-
|
177
|
-
See "Writing Tests" in https://github.com/intridea/grape.
|
178
|
-
|
179
|
-
Enjoy :)
|
180
|
-
|
181
|
-
## Changelog
|
182
|
-
|
183
|
-
#### Next
|
184
|
-
* Adds support for Grape 0.10.x
|
185
|
-
|
186
|
-
|
187
|
-
#### v1.2.1
|
188
|
-
* Adds support for active model serializer 0.9.x
|
189
|
-
|
190
|
-
|
191
|
-
#### v1.0.0
|
192
|
-
* Released on rubygems.org
|
193
|
-
* *BREAKING* Changes behaviour of root keys when serialising arrays. See [Array roots](https://github.com/jrhe/grape-active_model_serializers#array-roots)
|
194
|
-
|
195
|
-
|
196
237
|
## Contributing
|
197
238
|
|
198
|
-
|
199
|
-
2. Create your feature branch (`git checkout -b my-new-feature`)
|
200
|
-
3. Commit your changes (`git commit -am 'Added some feature'`)
|
201
|
-
4. Push to the branch (`git push origin my-new-feature`)
|
202
|
-
5. Create new Pull Request
|
203
|
-
|
239
|
+
See [CONTRIBUTING](CONTRIBUTING.md).
|
204
240
|
|
205
|
-
##
|
206
|
-
The developers and maintainers of:
|
207
|
-
[active_model_serializers](https://github.com/rails-api/active_model_serializers)
|
208
|
-
[Grape](https://github.com/intridea/grape)!
|
241
|
+
## History
|
209
242
|
|
210
243
|
Structured and based upon [grape-rabl](https://github.com/LTe/grape-rabl).
|
data/UPGRADING.md
ADDED
@@ -0,0 +1,100 @@
|
|
1
|
+
### Upgrading to v.1.4.0
|
2
|
+
|
3
|
+
#### Changes in Serializer Namespacing
|
4
|
+
|
5
|
+
Version 1.4.0 introduced changes in serializer namespacing when using Grape API versioning.
|
6
|
+
|
7
|
+
If you haven't declared an API version in Grape, nothing changed.
|
8
|
+
|
9
|
+
If your Grape API is versioned, which means you have declared at least one version in Grape, you must namespace your serializers accordingly.
|
10
|
+
|
11
|
+
For example, given the following API.
|
12
|
+
|
13
|
+
```ruby
|
14
|
+
module CandyBar
|
15
|
+
class Core < Grape::API
|
16
|
+
version 'candy_bar', using: :header, vendor: 'acme'
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
module Chocolate
|
21
|
+
class Core < Grape::API
|
22
|
+
version 'chocolate', using: :header, vendor: 'acme'
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
class API < Grape::API
|
27
|
+
format :json
|
28
|
+
formatter :json, Grape::Formatter::ActiveModelSerializers
|
29
|
+
|
30
|
+
mount CandyBar::Core
|
31
|
+
mount Chocolate::Core
|
32
|
+
end
|
33
|
+
```
|
34
|
+
|
35
|
+
Namespace serializers according to each version.
|
36
|
+
|
37
|
+
```ruby
|
38
|
+
module CandyBar
|
39
|
+
class UserSerializer < ActiveModel::Serializer
|
40
|
+
attributes :first_name, :last_name, :email
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
module Chocolate
|
45
|
+
class UserSerializer < ActiveModel::Serializer
|
46
|
+
attributes :first_name, :last_name
|
47
|
+
end
|
48
|
+
end
|
49
|
+
```
|
50
|
+
|
51
|
+
This will allow you to keep your serializers easier to maintain and more organized.
|
52
|
+
|
53
|
+
```
|
54
|
+
app
|
55
|
+
└── api
|
56
|
+
├── chocolate
|
57
|
+
└── core.rb
|
58
|
+
└── candy_bar
|
59
|
+
└── core.rb
|
60
|
+
api.rb
|
61
|
+
└── serializers
|
62
|
+
├── chocolate
|
63
|
+
└── user_serializer.rb
|
64
|
+
└── candy_bar
|
65
|
+
└── user_serializer.rb
|
66
|
+
```
|
67
|
+
|
68
|
+
or alternatively:
|
69
|
+
|
70
|
+
```
|
71
|
+
└── serializers
|
72
|
+
├── chocolate_user_serializer.rb
|
73
|
+
└── candy_bar_user_serializer.rb
|
74
|
+
```
|
75
|
+
|
76
|
+
Thus, ActiveModelSerializer will fetch automatically the right serializer to render.
|
77
|
+
|
78
|
+
### Upgrading to v1.0.0
|
79
|
+
|
80
|
+
#### Changes to Array Roots
|
81
|
+
|
82
|
+
When serializing an array, the array root is set to the innermost namespace name if there is one, otherwise it is set to the route name.
|
83
|
+
|
84
|
+
In the following example the array root is `users`.
|
85
|
+
|
86
|
+
```ruby
|
87
|
+
namespace :users do
|
88
|
+
get ":id" do
|
89
|
+
@user = User.find(params[:id])
|
90
|
+
end
|
91
|
+
end
|
92
|
+
```
|
93
|
+
|
94
|
+
In the following example the array root is `people`.
|
95
|
+
|
96
|
+
```ruby
|
97
|
+
get "people" do
|
98
|
+
@user = User.all
|
99
|
+
end
|
100
|
+
```
|
@@ -6,7 +6,7 @@ Gem::Specification.new do |gem|
|
|
6
6
|
gem.email = ['contact@jrhe.co.uk']
|
7
7
|
gem.summary = 'Use active_model_serializer in grape'
|
8
8
|
gem.description = 'Provides a Formatter for the Grape API DSL to emit objects serialized with active_model_serializers.'
|
9
|
-
gem.homepage = 'https://github.com/
|
9
|
+
gem.homepage = 'https://github.com/ruby-grape/grape-active_model_serializers'
|
10
10
|
|
11
11
|
gem.executables = `git ls-files -- bin/*`.split("\n").map { |f| File.basename(f) }
|
12
12
|
gem.files = `git ls-files`.split("\n")
|
@@ -19,6 +19,7 @@ Gem::Specification.new do |gem|
|
|
19
19
|
gem.add_dependency 'grape'
|
20
20
|
gem.add_dependency 'active_model_serializers', '>= 0.9.0'
|
21
21
|
|
22
|
+
gem.add_development_dependency 'listen', '~> 3.0.7'
|
22
23
|
gem.add_development_dependency 'rspec'
|
23
24
|
gem.add_development_dependency 'rack-test'
|
24
25
|
gem.add_development_dependency 'rake'
|
@@ -15,8 +15,12 @@ module Grape
|
|
15
15
|
def fetch_serializer(resource, env)
|
16
16
|
endpoint = env['api.endpoint']
|
17
17
|
options = build_options_from_endpoint(endpoint)
|
18
|
+
ams_options = {}.tap do |ns|
|
19
|
+
# Extracting declared version from Grape
|
20
|
+
ns[:namespace] = options[:version].try(:classify) if options.try(:[], :version)
|
21
|
+
end
|
18
22
|
|
19
|
-
serializer = options.fetch(:serializer, ActiveModel::Serializer.serializer_for(resource))
|
23
|
+
serializer = options.fetch(:serializer, ActiveModel::Serializer.serializer_for(resource, ams_options))
|
20
24
|
return nil unless serializer
|
21
25
|
|
22
26
|
options[:scope] = endpoint unless options.key?(:scope)
|
@@ -28,7 +32,7 @@ module Grape
|
|
28
32
|
def other_options(env)
|
29
33
|
options = {}
|
30
34
|
ams_meta = env['ams_meta'] || {}
|
31
|
-
meta =
|
35
|
+
meta = ams_meta.delete(:meta)
|
32
36
|
meta_key = ams_meta.delete(:meta_key)
|
33
37
|
options[:meta_key] = meta_key if meta && meta_key
|
34
38
|
options[meta_key || :meta] = meta if meta
|
@@ -0,0 +1,67 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'grape-active_model_serializers/formatter'
|
3
|
+
|
4
|
+
describe Grape::Formatter::ActiveModelSerializers do
|
5
|
+
describe 'with a versioned API' do
|
6
|
+
subject { Grape::Formatter::ActiveModelSerializers }
|
7
|
+
|
8
|
+
describe 'serializer options from namespace' do
|
9
|
+
let(:app) { Class.new(Grape::API) }
|
10
|
+
|
11
|
+
before do
|
12
|
+
app.format :json
|
13
|
+
app.formatter :json, Grape::Formatter::ActiveModelSerializers
|
14
|
+
app.version 'v1', using: :param
|
15
|
+
|
16
|
+
app.namespace('space') do |ns|
|
17
|
+
ns.get('/', root: false, apiver: 'v1') do
|
18
|
+
{ user: { first_name: 'JR', last_name: 'HE', email: 'jrhe@github.com' } }
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
it 'should read serializer options like "root"' do
|
24
|
+
expect(described_class.build_options_from_endpoint(app.endpoints.first)).to include :root
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
describe '.fetch_serializer' do
|
29
|
+
let(:user) { User.new(first_name: 'John', email: 'j.doe@internet.com') }
|
30
|
+
|
31
|
+
if Grape::Util.const_defined?('InheritableSetting')
|
32
|
+
let(:endpoint) { Grape::Endpoint.new(Grape::Util::InheritableSetting.new, path: '/', method: 'foo', version: 'v1', root: false) }
|
33
|
+
else
|
34
|
+
let(:endpoint) { Grape::Endpoint.new({}, path: '/', method: 'foo', version: 'v1', root: false) }
|
35
|
+
end
|
36
|
+
|
37
|
+
let(:env) { { 'api.endpoint' => endpoint } }
|
38
|
+
|
39
|
+
before do
|
40
|
+
def endpoint.current_user
|
41
|
+
@current_user ||= User.new(first_name: 'Current user')
|
42
|
+
end
|
43
|
+
|
44
|
+
def endpoint.default_serializer_options
|
45
|
+
{ only: :only, except: :except }
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
subject { described_class.fetch_serializer(user, env) }
|
50
|
+
|
51
|
+
it { should be_a V1::UserSerializer }
|
52
|
+
|
53
|
+
it 'should have correct scope set' do
|
54
|
+
expect(subject.scope.current_user).to eq(endpoint.current_user)
|
55
|
+
end
|
56
|
+
|
57
|
+
it 'should read default serializer options' do
|
58
|
+
expect(subject.instance_variable_get('@only')).to eq([:only])
|
59
|
+
expect(subject.instance_variable_get('@except')).to eq([:except])
|
60
|
+
end
|
61
|
+
|
62
|
+
it 'should read serializer options like "root"' do
|
63
|
+
expect(described_class.build_options_from_endpoint(endpoint).keys).to include :root
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: grape-active_model_serializers
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.4.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jonathan Richard Henry Evans
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2016-07-15 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: grape
|
@@ -38,6 +38,20 @@ dependencies:
|
|
38
38
|
- - ">="
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: 0.9.0
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: listen
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: 3.0.7
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: 3.0.7
|
41
55
|
- !ruby/object:Gem::Dependency
|
42
56
|
name: rspec
|
43
57
|
requirement: !ruby/object:Gem::Requirement
|
@@ -120,11 +134,14 @@ files:
|
|
120
134
|
- ".rubocop.yml"
|
121
135
|
- ".rubocop_todo.yml"
|
122
136
|
- ".travis.yml"
|
137
|
+
- CHANGELOG.md
|
138
|
+
- CONTRIBUTING.md
|
123
139
|
- Gemfile
|
124
140
|
- Guardfile
|
125
141
|
- LICENSE.txt
|
126
142
|
- README.md
|
127
143
|
- Rakefile
|
144
|
+
- UPGRADING.md
|
128
145
|
- grape-active_model_serializers.gemspec
|
129
146
|
- lib/.rspec
|
130
147
|
- lib/.travis.yml
|
@@ -135,6 +152,7 @@ files:
|
|
135
152
|
- spec/features/grape-active_model_serializers/render_spec.rb
|
136
153
|
- spec/grape-active_model_serializers/endpoint_extension_spec.rb
|
137
154
|
- spec/grape-active_model_serializers/formatter_spec.rb
|
155
|
+
- spec/grape-active_model_serializers/versioned_api_formatter_spec.rb
|
138
156
|
- spec/grape-active_model_serializers_spec.rb
|
139
157
|
- spec/old_grape_ams_spec.rb
|
140
158
|
- spec/spec_helper.rb
|
@@ -142,7 +160,8 @@ files:
|
|
142
160
|
- spec/support/models/user.rb
|
143
161
|
- spec/support/serializers/blog_post_serializer.rb
|
144
162
|
- spec/support/serializers/user_serializer.rb
|
145
|
-
|
163
|
+
- spec/support/serializers/v1/user_serializer.rb
|
164
|
+
homepage: https://github.com/ruby-grape/grape-active_model_serializers
|
146
165
|
licenses:
|
147
166
|
- MIT
|
148
167
|
metadata: {}
|
@@ -162,7 +181,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
162
181
|
version: '0'
|
163
182
|
requirements: []
|
164
183
|
rubyforge_project:
|
165
|
-
rubygems_version: 2.
|
184
|
+
rubygems_version: 2.6.6
|
166
185
|
signing_key:
|
167
186
|
specification_version: 4
|
168
187
|
summary: Use active_model_serializer in grape
|
@@ -170,6 +189,7 @@ test_files:
|
|
170
189
|
- spec/features/grape-active_model_serializers/render_spec.rb
|
171
190
|
- spec/grape-active_model_serializers/endpoint_extension_spec.rb
|
172
191
|
- spec/grape-active_model_serializers/formatter_spec.rb
|
192
|
+
- spec/grape-active_model_serializers/versioned_api_formatter_spec.rb
|
173
193
|
- spec/grape-active_model_serializers_spec.rb
|
174
194
|
- spec/old_grape_ams_spec.rb
|
175
195
|
- spec/spec_helper.rb
|
@@ -177,3 +197,5 @@ test_files:
|
|
177
197
|
- spec/support/models/user.rb
|
178
198
|
- spec/support/serializers/blog_post_serializer.rb
|
179
199
|
- spec/support/serializers/user_serializer.rb
|
200
|
+
- spec/support/serializers/v1/user_serializer.rb
|
201
|
+
has_rdoc:
|