Skip to content

Commit b48aeee

Browse files
committed
Merge branch 'master' into 0-10-stable
2 parents 01d4f04 + dff621e commit b48aeee

37 files changed

+1316
-650
lines changed

.rubocop.yml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
AllCops:
22
TargetRubyVersion: 2.1
33
Exclude:
4-
- config/initializers/forbidden_yaml.rb
54
- !ruby/regexp /(vendor|bundle|bin|db|tmp)\/.*/
65
DisplayCopNames: true
76
DisplayStyleGuide: true
7+
# https://github.com/bbatsov/rubocop/blob/master/manual/caching.md
8+
# https://github.com/bbatsov/rubocop/blob/e8680418b351491e111a18cf5b453fc07a3c5239/config/default.yml#L60-L77
9+
UseCache: true
10+
CacheRootDirectory: tmp
811

912
Rails:
1013
Enabled: true

CHANGELOG.md

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
## 0.10.x
22

3-
### [master (unreleased)](https://github.com/rails-api/active_model_serializers/compare/v0.10.5...master)
3+
### [master (unreleased)](https://github.com/rails-api/active_model_serializers/compare/v0.10.6...master)
44

55
Breaking changes:
66

@@ -14,6 +14,20 @@ Fixes:
1414

1515
Misc:
1616

17+
### [v0.10.6 (2017-05-01)](https://github.com/rails-api/active_model_serializers/compare/v0.10.5...v0.10.6)
18+
19+
Fixes:
20+
21+
- [#1857](https://github.com/rails-api/active_model_serializers/pull/1857) JSON:API does not load belongs_to relation to get identifier id. (@bf4)
22+
- [#2119](https://github.com/rails-api/active_model_serializers/pull/2119) JSON:API returns null resource object identifier when 'id' is null. (@bf4)
23+
- [#2093](https://github.com/rails-api/active_model_serializers/pull/2093) undef problematic Serializer methods: display, select. (@bf4)
24+
25+
Misc:
26+
27+
- [#2104](https://github.com/rails-api/active_model_serializers/pull/2104) Documentation for serializers and rendering. (@cassidycodes)
28+
- [#2081](https://github.com/rails-api/active_model_serializers/pull/2081) Documentation for `include` option in adapters. (@charlie-wasp)
29+
- [#2120](https://github.com/rails-api/active_model_serializers/pull/2120) Documentation for association options: foreign_key, type, class_name, namespace. (@bf4)
30+
1731
### [v0.10.5 (2017-03-07)](https://github.com/rails-api/active_model_serializers/compare/v0.10.4...v0.10.5)
1832

1933
Breaking changes:
@@ -81,7 +95,7 @@ Misc:
8195

8296
- [#1878](https://github.com/rails-api/active_model_serializers/pull/1878) Cache key generation for serializers now uses `ActiveSupport::Cache.expand_cache_key` instead of `Array#join` by default and is also overridable. This change should be backward-compatible. (@markiz)
8397

84-
- [#1799](https://github.com/rails-api/active_model_serializers/pull/1799) Add documentation for setting the adapter. (@ScottKbka)
98+
- [#1799](https://github.com/rails-api/active_model_serializers/pull/1799) Add documentation for setting the adapter. (@cassidycodes)
8599
- [#1909](https://github.com/rails-api/active_model_serializers/pull/1909) Add documentation for relationship links. (@vasilakisfil, @NullVoxPopuli)
86100
- [#1959](https://github.com/rails-api/active_model_serializers/pull/1959) Add documentation for root. (@shunsuke227ono)
87101
- [#1967](https://github.com/rails-api/active_model_serializers/pull/1967) Improve type method documentation. (@yukideluxe)

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,8 @@ reading documentation for our `master`, which may include features that have not
9090
been released yet. Please see below for the documentation relevant to you.
9191

9292
- [0.10 (master) Documentation](https://github.com/rails-api/active_model_serializers/tree/master)
93-
- [0.10.4 (latest release) Documentation](https://github.com/rails-api/active_model_serializers/tree/v0.10.4)
94-
- [![API Docs](http://img.shields.io/badge/yard-docs-blue.svg)](http://www.rubydoc.info/gems/active_model_serializers/0.10.4)
93+
- [0.10.6 (latest release) Documentation](https://github.com/rails-api/active_model_serializers/tree/v0.10.6)
94+
- [![API Docs](http://img.shields.io/badge/yard-docs-blue.svg)](http://www.rubydoc.info/gems/active_model_serializers/0.10.6)
9595
- [Guides](docs)
9696
- [0.9 (0-9-stable) Documentation](https://github.com/rails-api/active_model_serializers/tree/0-9-stable)
9797
- [![API Docs](http://img.shields.io/badge/yard-docs-blue.svg)](http://www.rubydoc.info/github/rails-api/active_model_serializers/0-9-stable)

Rakefile

Lines changed: 1 addition & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ begin
77
require 'simplecov'
88
rescue LoadError # rubocop:disable Lint/HandleExceptions
99
end
10+
import('lib/tasks/rubocop.rake')
1011

1112
Bundler::GemHelper.install_tasks
1213

@@ -30,36 +31,6 @@ namespace :yard do
3031
end
3132
end
3233

33-
begin
34-
require 'rubocop'
35-
require 'rubocop/rake_task'
36-
rescue LoadError # rubocop:disable Lint/HandleExceptions
37-
else
38-
Rake::Task[:rubocop].clear if Rake::Task.task_defined?(:rubocop)
39-
require 'rbconfig'
40-
# https://github.com/bundler/bundler/blob/1b3eb2465a/lib/bundler/constants.rb#L2
41-
windows_platforms = /(msdos|mswin|djgpp|mingw)/
42-
if RbConfig::CONFIG['host_os'] =~ windows_platforms
43-
desc 'No-op rubocop on Windows-- unsupported platform'
44-
task :rubocop do
45-
puts 'Skipping rubocop on Windows'
46-
end
47-
elsif defined?(::Rubinius)
48-
desc 'No-op rubocop to avoid rbx segfault'
49-
task :rubocop do
50-
puts 'Skipping rubocop on rbx due to segfault'
51-
puts 'https://github.com/rubinius/rubinius/issues/3499'
52-
end
53-
else
54-
Rake::Task[:rubocop].clear if Rake::Task.task_defined?(:rubocop)
55-
desc 'Execute rubocop'
56-
RuboCop::RakeTask.new(:rubocop) do |task|
57-
task.options = ['--rails', '--display-cop-names', '--display-style-guide']
58-
task.fail_on_error = true
59-
end
60-
end
61-
end
62-
6334
require 'rake/testtask'
6435

6536
Rake::TestTask.new(:test) do |t|

active_model_serializers.gemspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ Gem::Specification.new do |spec|
5757
spec.add_development_dependency 'bundler', '~> 1.6'
5858
spec.add_development_dependency 'simplecov', '~> 0.11'
5959
spec.add_development_dependency 'timecop', '~> 0.7'
60-
spec.add_development_dependency 'grape', ['>= 0.13', '< 1.0']
60+
spec.add_development_dependency 'grape', ['>= 0.13', '< 0.19.1']
6161
spec.add_development_dependency 'json_schema'
6262
spec.add_development_dependency 'rake', ['>= 10.0', '< 12.0']
6363
end

bin/rubocop

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#!/usr/bin/env bash
2+
#
3+
# Usage:
4+
# bin/rubocop [-A|-t|-h]
5+
# bin/rubocop [file or path] [cli options]
6+
#
7+
# Options:
8+
# Autocorrect -A
9+
# AutoGenConfig -t
10+
# Usage -h,--help,help
11+
12+
set -e
13+
14+
case $1 in
15+
-A)
16+
echo "Rubocop autocorrect is ON" >&2
17+
bundle exec rake -f lib/tasks/rubocop.rake rubocop:auto_correct
18+
;;
19+
20+
-t)
21+
echo "Rubocop is generating a new TODO" >&2
22+
bundle exec rake -f lib/tasks/rubocop.rake rubocop:auto_gen_config
23+
;;
24+
25+
-h|--help|help)
26+
sed -ne '/^#/!q;s/.\{1,2\}//;1d;p' < "$0"
27+
;;
28+
29+
*)
30+
# with no args, run vanilla rubocop
31+
# else assume we're passing in arbitrary arguments
32+
if [ -z "$1" ]; then
33+
bundle exec rake -f lib/tasks/rubocop.rake rubocop
34+
else
35+
bundle exec rubocop "$@"
36+
fi
37+
;;
38+
esac

docs/general/adapters.md

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -141,18 +141,25 @@ This adapter follows **version 1.0** of the [format specified](../jsonapi/schema
141141
}
142142
```
143143

144-
#### Included
144+
### Include option
145145

146-
It will include the associated resources in the `"included"` member
147-
when the resource names are included in the `include` option.
148-
Including nested associated resources is also supported.
146+
Which [serializer associations](https://github.com/rails-api/active_model_serializers/blob/master/docs/general/serializers.md#associations) are rendered can be specified using the `include` option. The option usage is consistent with [the include option in the JSON API spec](http://jsonapi.org/format/#fetching-includes), and is available in all adapters.
149147

148+
Example of the usage:
150149
```ruby
151150
render json: @posts, include: ['author', 'comments', 'comments.author']
152151
# or
153152
render json: @posts, include: 'author,comments,comments.author'
154153
```
155154

155+
The format of the `include` option can be either:
156+
157+
- a String composed of a comma-separated list of [relationship paths](http://jsonapi.org/format/#fetching-includes).
158+
- an Array of Symbols and Hashes.
159+
- a mix of both.
160+
161+
An empty string or an empty array will prevent rendering of any associations.
162+
156163
In addition, two types of wildcards may be used:
157164

158165
- `*` includes one level of associations.
@@ -164,11 +171,6 @@ These can be combined with other paths.
164171
render json: @posts, include: '**' # or '*' for a single layer
165172
```
166173

167-
The format of the `include` option can be either:
168-
169-
- a String composed of a comma-separated list of [relationship paths](http://jsonapi.org/format/#fetching-includes).
170-
- an Array of Symbols and Hashes.
171-
- a mix of both.
172174

173175
The following would render posts and include:
174176

@@ -182,6 +184,20 @@ It could be combined, like above, with other paths in any combination desired.
182184
render json: @posts, include: 'author.comments.**'
183185
```
184186

187+
**Note:** Wildcards are ActiveModelSerializers-specific, they are not part of the JSON API spec.
188+
189+
The default include for the JSON API adapter is no associations. The default for the JSON and Attributes adapters is all associations.
190+
191+
For the JSON API adapter associated resources will be gathered in the `"included"` member. For the JSON and Attributes
192+
adapters associated resources will be rendered among the other attributes.
193+
194+
Only for the JSON API adapter you can specify, which attributes of associated resources will be rendered. This feature
195+
is called [sparse fieldset](http://jsonapi.org/format/#fetching-sparse-fieldsets):
196+
197+
```ruby
198+
render json: @posts, include: 'comments', fields: { comments: ['content', 'created_at'] }
199+
```
200+
185201
##### Security Considerations
186202

187203
Since the included options may come from the query params (i.e. user-controller):

docs/general/getting_started.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ and
3737
class CommentSerializer < ActiveModel::Serializer
3838
attributes :name, :body
3939

40-
belongs_to :post_id
40+
belongs_to :post
4141
end
4242
```
4343

docs/general/rendering.md

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ link(:link_name) { url_for(controller: 'controller_name', action: 'index', only_
203203

204204
#### include
205205

206-
PR please :)
206+
See [Adapters: Include Option](/docs/general/adapters.md#include-option).
207207

208208
#### Overriding the root key
209209

@@ -260,15 +260,29 @@ Note that by using a string and symbol, Ruby will assume the namespace is define
260260

261261
#### serializer
262262

263-
PR please :)
263+
Specify which serializer to use if you want to use a serializer other than the default.
264+
265+
For a single resource:
266+
267+
```ruby
268+
@post = Post.first
269+
render json: @post, serializer: SpecialPostSerializer
270+
```
271+
272+
To specify which serializer to use on individual items in a collection (i.e., an `index` action), use `each_serializer`:
273+
274+
```ruby
275+
@posts = Post.all
276+
render json: @posts, each_serializer: SpecialPostSerializer
277+
```
264278

265279
#### scope
266280

267-
PR please :)
281+
See [Serializers: Scope](/docs/general/serializers.md#scope).
268282

269283
#### scope_name
270284

271-
PR please :)
285+
See [Serializers: Scope](/docs/general/serializers.md#scope).
272286

273287
## Using a serializer without `render`
274288

docs/general/serializers.md

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,10 @@ Where:
6464
- `unless:`
6565
- `virtual_value:`
6666
- `polymorphic:` defines if polymorphic relation type should be nested in serialized association.
67+
- `type:` the resource type as used by JSON:API, especially on a `belongs_to` relationship.
68+
- `class_name:` used to determine `type` when `type` not given
69+
- `foreign_key:` used by JSON:API on a `belongs_to` relationship to avoid unnecessarily loading the association object.
70+
- `namespace:` used when looking up the serializer and `serializer` is not given. Falls back to the parent serializer's `:namespace` instance options, which, when present, comes from the render options. See [Rendering#namespace](rendering.md#namespace] for more details.
6771
- optional: `&block` is a context that returns the association's attributes.
6872
- prevents `association_name` method from being called.
6973
- return value of block is used as the association value.
@@ -382,11 +386,26 @@ The serialized value for a given key. e.g. `read_attribute_for_serialization(:ti
382386

383387
#### #links
384388

385-
PR please :)
389+
Allows you to modify the `links` node. By default, this node will be populated with the attributes set using the [::link](#link) method. Using `links: nil` will remove the `links` node.
390+
391+
```ruby
392+
ActiveModelSerializers::SerializableResource.new(
393+
@post,
394+
adapter: :json_api,
395+
links: {
396+
self: {
397+
href: 'http://example.com/posts',
398+
meta: {
399+
stuff: 'value'
400+
}
401+
}
402+
}
403+
)
404+
```
386405

387406
#### #json_key
388407

389-
PR please :)
408+
Returns the key used by the adapter as the resource root. See [root](#root) for more information.
390409

391410
## Examples
392411

0 commit comments

Comments
 (0)