active_model_serializers 0.10.0.rc3 → 0.10.0.rc4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +1 -0
- data/.rubocop.yml +37 -33
- data/.rubocop_todo.yml +13 -88
- data/.simplecov +23 -11
- data/.travis.yml +17 -12
- data/CHANGELOG.md +417 -12
- data/CONTRIBUTING.md +206 -17
- data/Gemfile +12 -11
- data/README.md +78 -286
- data/Rakefile +44 -8
- data/active_model_serializers.gemspec +9 -1
- data/appveyor.yml +6 -4
- data/docs/ARCHITECTURE.md +120 -0
- data/docs/DESIGN.textile +8 -0
- data/docs/README.md +21 -15
- data/docs/general/adapters.md +79 -27
- data/docs/general/caching.md +52 -0
- data/docs/general/configuration_options.md +18 -2
- data/docs/general/getting_started.md +44 -19
- data/docs/general/instrumentation.md +40 -0
- data/docs/general/logging.md +14 -0
- data/docs/general/rendering.md +153 -0
- data/docs/general/serializers.md +207 -0
- data/docs/how-open-source-maintained.jpg +0 -0
- data/docs/howto/add_pagination_links.md +16 -7
- data/docs/howto/add_root_key.md +3 -3
- data/docs/howto/outside_controller_use.md +25 -9
- data/docs/howto/test.md +152 -0
- data/docs/integrations/ember-and-json-api.md +112 -0
- data/docs/integrations/grape.md +19 -0
- data/docs/jsonapi/schema.md +140 -0
- data/docs/jsonapi/schema/schema.json +366 -0
- data/lib/action_controller/serialization.rb +13 -9
- data/lib/active_model/serializable_resource.rb +9 -7
- data/lib/active_model/serializer.rb +93 -129
- data/lib/active_model/serializer/adapter.rb +37 -105
- data/lib/active_model/serializer/adapter/attributes.rb +66 -0
- data/lib/active_model/serializer/adapter/base.rb +58 -0
- data/lib/active_model/serializer/adapter/cached_serializer.rb +45 -0
- data/lib/active_model/serializer/adapter/fragment_cache.rb +43 -7
- data/lib/active_model/serializer/adapter/json.rb +11 -37
- data/lib/active_model/serializer/adapter/json/fragment_cache.rb +9 -1
- data/lib/active_model/serializer/adapter/json_api.rb +127 -62
- data/lib/active_model/serializer/adapter/json_api/deserialization.rb +207 -0
- data/lib/active_model/serializer/adapter/json_api/fragment_cache.rb +9 -1
- data/lib/active_model/serializer/adapter/json_api/link.rb +44 -0
- data/lib/active_model/serializer/adapter/json_api/pagination_links.rb +12 -4
- data/lib/active_model/serializer/adapter/null.rb +7 -1
- data/lib/active_model/serializer/array_serializer.rb +6 -37
- data/lib/active_model/serializer/associations.rb +21 -18
- data/lib/active_model/serializer/attribute.rb +25 -0
- data/lib/active_model/serializer/attributes.rb +82 -0
- data/lib/active_model/serializer/caching.rb +100 -0
- data/lib/active_model/serializer/collection_serializer.rb +47 -0
- data/lib/active_model/serializer/configuration.rb +17 -3
- data/lib/active_model/serializer/field.rb +56 -0
- data/lib/active_model/serializer/fieldset.rb +9 -18
- data/lib/active_model/serializer/include_tree.rb +111 -0
- data/lib/active_model/serializer/links.rb +33 -0
- data/lib/active_model/serializer/lint.rb +16 -3
- data/lib/active_model/serializer/reflection.rb +25 -8
- data/lib/active_model/serializer/type.rb +25 -0
- data/lib/active_model/serializer/version.rb +1 -1
- data/lib/active_model_serializers.rb +16 -26
- data/lib/active_model_serializers/callbacks.rb +55 -0
- data/lib/active_model_serializers/deserialization.rb +13 -0
- data/lib/active_model_serializers/logging.rb +119 -0
- data/lib/active_model_serializers/model.rb +39 -0
- data/lib/active_model_serializers/railtie.rb +38 -0
- data/lib/active_model_serializers/serialization_context.rb +10 -0
- data/lib/active_model_serializers/test.rb +7 -0
- data/lib/active_model_serializers/test/schema.rb +103 -0
- data/lib/active_model_serializers/test/serializer.rb +125 -0
- data/lib/generators/{serializer → rails}/USAGE +1 -1
- data/lib/generators/{serializer → rails}/resource_override.rb +1 -3
- data/lib/generators/{serializer → rails}/serializer_generator.rb +2 -3
- data/lib/generators/{serializer → rails}/templates/serializer.rb.erb +0 -0
- data/lib/grape/active_model_serializers.rb +14 -0
- data/lib/grape/formatters/active_model_serializers.rb +15 -0
- data/lib/grape/helpers/active_model_serializers.rb +16 -0
- data/test/action_controller/adapter_selector_test.rb +1 -1
- data/test/action_controller/json/include_test.rb +167 -0
- data/test/action_controller/json_api/deserialization_test.rb +59 -0
- data/test/action_controller/json_api/linked_test.rb +20 -3
- data/test/action_controller/json_api/pagination_test.rb +7 -7
- data/test/action_controller/serialization_scope_name_test.rb +8 -12
- data/test/action_controller/serialization_test.rb +44 -29
- data/test/active_model_serializers/logging_test.rb +77 -0
- data/test/active_model_serializers/model_test.rb +9 -0
- data/test/active_model_serializers/railtie_test_isolated.rb +57 -0
- data/test/active_model_serializers/serialization_context_test.rb +18 -0
- data/test/active_model_serializers/test/schema_test.rb +128 -0
- data/test/active_model_serializers/test/serializer_test.rb +63 -0
- data/test/active_record_test.rb +1 -1
- data/test/adapter/fragment_cache_test.rb +3 -2
- data/test/adapter/json/belongs_to_test.rb +2 -2
- data/test/adapter/json/collection_test.rb +15 -5
- data/test/adapter/json/has_many_test.rb +3 -3
- data/test/adapter/json_api/belongs_to_test.rb +3 -3
- data/test/adapter/json_api/collection_test.rb +8 -6
- data/test/adapter/json_api/fields_test.rb +89 -0
- data/test/adapter/json_api/has_many_embed_ids_test.rb +2 -2
- data/test/adapter/json_api/has_many_explicit_serializer_test.rb +2 -2
- data/test/adapter/json_api/has_many_test.rb +3 -3
- data/test/adapter/json_api/has_one_test.rb +2 -2
- data/test/adapter/json_api/json_api_test.rb +2 -2
- data/test/adapter/json_api/linked_test.rb +116 -5
- data/test/adapter/json_api/links_test.rb +68 -0
- data/test/adapter/json_api/pagination_links_test.rb +4 -4
- data/test/adapter/json_api/parse_test.rb +139 -0
- data/test/adapter/json_api/resource_type_config_test.rb +27 -15
- data/test/adapter/json_api/toplevel_jsonapi_test.rb +84 -0
- data/test/adapter/json_test.rb +2 -2
- data/test/adapter/null_test.rb +2 -2
- data/test/adapter_test.rb +3 -3
- data/test/array_serializer_test.rb +30 -93
- data/test/collection_serializer_test.rb +100 -0
- data/test/fixtures/poro.rb +19 -51
- data/test/generators/scaffold_controller_generator_test.rb +1 -0
- data/test/generators/serializer_generator_test.rb +2 -1
- data/test/grape_test.rb +82 -0
- data/test/include_tree/from_include_args_test.rb +26 -0
- data/test/include_tree/from_string_test.rb +94 -0
- data/test/include_tree/include_args_to_hash_test.rb +64 -0
- data/test/lint_test.rb +4 -1
- data/test/logger_test.rb +2 -2
- data/test/poro_test.rb +1 -1
- data/test/serializable_resource_test.rb +1 -1
- data/test/serializers/adapter_for_test.rb +24 -28
- data/test/serializers/association_macros_test.rb +1 -1
- data/test/serializers/associations_test.rb +143 -26
- data/test/serializers/attribute_test.rb +64 -3
- data/test/serializers/attributes_test.rb +1 -6
- data/test/serializers/cache_test.rb +46 -2
- data/test/serializers/configuration_test.rb +20 -3
- data/test/serializers/fieldset_test.rb +5 -16
- data/test/serializers/meta_test.rb +38 -29
- data/test/serializers/options_test.rb +1 -1
- data/test/serializers/root_test.rb +1 -1
- data/test/serializers/serializer_for_test.rb +78 -9
- data/test/support/custom_schemas/active_model_serializers/test/schema_test/my/index.json +6 -0
- data/test/support/isolated_unit.rb +77 -0
- data/test/support/rails5_shims.rb +29 -0
- data/test/support/rails_app.rb +7 -3
- data/test/support/schemas/active_model_serializers/test/schema_test/my/index.json +6 -0
- data/test/support/schemas/active_model_serializers/test/schema_test/my/show.json +6 -0
- data/test/support/schemas/custom/show.json +7 -0
- data/test/support/schemas/hyper_schema.json +93 -0
- data/test/support/schemas/render_using_json_api.json +43 -0
- data/test/support/schemas/simple_json_pointers.json +10 -0
- data/test/support/serialization_testing.rb +46 -6
- data/test/support/test_case.rb +14 -0
- data/test/test_helper.rb +21 -14
- metadata +160 -16
- data/lib/active_model/serializer/adapter/flatten_json.rb +0 -12
- data/lib/active_model/serializer/railtie.rb +0 -15
- data/lib/active_model/serializer/utils.rb +0 -35
- data/lib/tasks/rubocop.rake +0 -0
- data/test/capture_warnings.rb +0 -65
- data/test/utils/include_args_to_hash_test.rb +0 -79
data/CONTRIBUTING.md
CHANGED
@@ -1,31 +1,220 @@
|
|
1
|
+
First of all, **thank you**!
|
2
|
+
|
3
|
+
![Commit Strip
|
4
|
+
http://www.commitstrip.com/en/2014/05/07/the-truth-behind-open-source-apps/](docs/how-open-source-maintained.jpg)
|
5
|
+
|
6
|
+
## Common issues and resolutions
|
7
|
+
|
8
|
+
- Using `grape-active_model_serializers`, or any non-Rails server. See
|
9
|
+
[issue](https://github.com/rails-api/active_model_serializers/issues/1258).
|
10
|
+
|
1
11
|
## How can I help?
|
2
12
|
|
3
|
-
|
13
|
+
- [Filing an issue](CONTRIBUTING.md#filing-an-issue)
|
14
|
+
- [Writing code and comments](CONTRIBUTING.md#writing-code-and-comments)
|
15
|
+
|
16
|
+
### Filing an issue
|
17
|
+
|
18
|
+
Everyone is encouraged to open issues that are affecting them:
|
19
|
+
bugs, ideas, documentation (`/docs`), performance problems – everything helps!
|
20
|
+
|
21
|
+
#### Before
|
22
|
+
|
23
|
+
1. Start by looking at our [GitHub Issues](https://github.com/rails-api/active_model_serializers/issues).
|
24
|
+
|
25
|
+
- Check if your issue has already been reported.
|
26
|
+
- If you find an existing issue report, feel free to add further information to that report.
|
27
|
+
|
28
|
+
#### Writing
|
29
|
+
|
30
|
+
If possible, please include the following information when [reporting an
|
31
|
+
issue](https://github.com/rails-api/active_model_serializers/issues/new):
|
32
|
+
|
33
|
+
- ActiveModelSerializers version (0.8.x, 0.9.x, 0.10.x, commit ref).
|
34
|
+
- What are you using ActiveModelSerializers with? Rails? Grape? Other? Which versions?
|
35
|
+
- If you are not running the latest version (please check), and you cannot update it,
|
36
|
+
please specify in your report why you can't update to the latest version.
|
37
|
+
- Operating system type + version.
|
38
|
+
- Ruby version with patch level. And whether you're using rvm, rbenv, etc.
|
39
|
+
- Include `ruby -e "puts RUBY_DESCRIPTION"`.
|
40
|
+
- Clearly-written steps to reproduce the issue (i.e. "Show me how to show myself." ), including:
|
41
|
+
- What were you doing? Include code if possible.
|
42
|
+
- Command line parameters used, if any.
|
43
|
+
- RubyGems code in your Gemfile, if any. Gemfile.lock, if possible.
|
44
|
+
- Any configuration you've made.
|
45
|
+
- What did you expect to happen?
|
46
|
+
- What happened? Include as much information as possible.
|
47
|
+
- Nature of reported defect (e.g. user name missing, not "It doesn't work."). Is it intermittent?
|
48
|
+
- The best help here is a failing test. Even better if it's a PR.
|
49
|
+
- Then the steps to reproduce and/or a gist or repository that demonstrates the defect.
|
50
|
+
- Then examples of the code you were using.
|
51
|
+
- Any error messages (including stacktrace, i.e. "Show me the error.")
|
52
|
+
- Things you've tried.
|
53
|
+
- A pull request for your fix would be great. Code should have tests.
|
54
|
+
- Link to source code, if available.
|
55
|
+
|
56
|
+
Please make sure only to include one issue per report.
|
57
|
+
If you encounter multiple, unrelated issues, please report them as such.
|
58
|
+
|
59
|
+
Simon Tatham has written an excellent on article on
|
60
|
+
[How to Report Bugs Effectively](http://www.chiark.greenend.org.uk/~sgtatham/bugs.html)
|
61
|
+
which is [well worth reading](http://yourbugreportneedsmore.info/), although it is not specific to ActiveModelSerializers.
|
62
|
+
|
63
|
+
Include as much sample code as you can to help us reproduce the issue. (Inline, repo link, or gist, are fine. A failing test would help the most.)
|
64
|
+
|
65
|
+
This is extremely important for narrowing down the cause of your problem.
|
66
|
+
|
67
|
+
Thanks!
|
68
|
+
|
69
|
+
Sometimes an issue will be closed by a maintainer for various reasons. In some cases, this is
|
70
|
+
an invitation to make a better case for your issue or be able to reproduce a bug, and
|
71
|
+
its being close is just an opportunity to help out some more, and then re-open.
|
72
|
+
|
73
|
+
#### After
|
74
|
+
|
75
|
+
Thanks to everyone involved!
|
76
|
+
|
77
|
+
If you get help, sharing it back in the form of a pull-request or making an issue to document
|
78
|
+
what you've found is *extremely* helpful.
|
79
|
+
|
80
|
+
If you solve your issue, stop working on it, or realize the problem was something else,
|
81
|
+
please share that in a comment to an issue and close it. That way, everyone can learn and
|
82
|
+
we don't have closed issues without a clear resolution. Even if it's just a stackoverflow link :)
|
83
|
+
And please don't forget to stay involved in the issue until it is closed! Thanks to all!
|
84
|
+
|
85
|
+
### Writing code and comments
|
4
86
|
|
5
|
-
|
87
|
+
- We are actively working to identify tasks under the label [**Good for New
|
88
|
+
Contributors**](https://github.com/rails-api/active_model_serializers/labels/Good%20for%20New%20Contributors).
|
89
|
+
- [Changelog
|
90
|
+
Missing](https://github.com/rails-api/active_model_serializers/issues?q=label%3A%22Changelog+Missing%22+is%3Aclosed) is
|
91
|
+
an easy way to help out.
|
6
92
|
|
7
|
-
|
93
|
+
- [Fix a bug](https://github.com/rails-api/active_model_serializers/labels/Ready%20for%20PR).
|
94
|
+
- Ready for PR - A well defined bug, needs someone to PR a fix.
|
95
|
+
- Bug - Anything that is broken.
|
96
|
+
- Regression - A bug that did not exist in previous versions and isn't a new feature (applied in tandem with Bug).
|
97
|
+
- Performance - A performance related issue. We could track this as a bug, but usually these would have slightly lower priority than standard bugs.
|
8
98
|
|
9
|
-
|
99
|
+
- [Develop new features](https://github.com/rails-api/active_model_serializers/labels/Feature).
|
10
100
|
|
11
|
-
|
101
|
+
- [Improve code quality](https://codeclimate.com/github/rails-api/active_model_serializers/code?sort=smell_count&sort_direction=desc).
|
12
102
|
|
13
|
-
|
103
|
+
- [Improve amount of code exercised by tests](https://codeclimate.com/github/rails-api/active_model_serializers/coverage?sort=covered_percent&sort_direction=asc).
|
14
104
|
|
15
|
-
|
105
|
+
- [Fix RuboCop (Style) TODOS](https://github.com/rails-api/active_model_serializers/blob/master/.rubocop_todo.yml).
|
106
|
+
- Delete and offsense, run `rake rubocop` (or possibly `rake rubocop:auto_correct`),
|
107
|
+
and [submit a PR](CONTRIBUTING.md#submitting-a-pull-request-pr).
|
108
|
+
|
109
|
+
- We are also encouraging comments to substantial changes (larger than bugfixes and simple features) under an
|
110
|
+
"RFC" (Request for Comments) process before we start active development.
|
111
|
+
Look for the [**RFC**](https://github.com/rails-api/active_model_serializers/labels/RFC) label.
|
112
|
+
|
113
|
+
#### Submitting a pull request (PR)
|
114
|
+
|
115
|
+
1. The vast majority of development is happening under the `master` branch.
|
116
|
+
This is where we would suggest you start.
|
117
|
+
1. Fixing bugs is extraordinarily helpful and requires the least familiarity with ActiveModelSerializers.
|
118
|
+
Look for issues labeled [**Needs Bug Verification**](https://github.com/rails-api/active_model_serializers/labels/Needs%20Bug%20Verification) and [**Bug**](https://github.com/rails-api/active_model_serializers/labels/bug).
|
119
|
+
1. Adding or fixing documentation is also fantastic!
|
120
|
+
|
121
|
+
To fetch & test the library for development, do:
|
122
|
+
|
123
|
+
1. Fork the repository ( https://github.com/rails-api/active_model_serializers/fork )
|
124
|
+
1. `git clone https://github.com/{whoami}/active_model_serializers.git`
|
125
|
+
1. `cd active_model_serializers`
|
126
|
+
1. `bundle`
|
127
|
+
- To test against a particular rails version-- 4.0 is usually the most buggy-- set then
|
128
|
+
RAILS_VERSION environment variable as described in the [.travis.yml](.travis.yml).
|
129
|
+
e.g. `export RAILS_VERSION=4.0`.
|
130
|
+
1. Create your PR branch (`git checkout -b my-helpful-pr`)
|
131
|
+
1. Write tests for your feature, or regression tests highlighting a bug.
|
132
|
+
This is important so ActiveModelSerializers doesn't break it in a future version unintentionally.
|
133
|
+
1. Write the feature itself, or fix your bug
|
134
|
+
1. `bundle exec rake`
|
135
|
+
1. Commit your changes (`git commit -am 'Add some feature'`)
|
136
|
+
- Use well-described, small (atomic) commits.
|
137
|
+
1. Push to the branch (`git push origin my-helpful-pr`)
|
138
|
+
1. Create a new Pull Request
|
139
|
+
- Include links to any relevant github issues.
|
140
|
+
- *Don't* change the VERSION file.
|
141
|
+
- Update `/docs` to include, whenever possible, a new, suitable recommendation about how to use
|
142
|
+
the feature.
|
143
|
+
- Extra Credit: [Confirm it runs and tests pass on the rubies specified in the travis
|
144
|
+
config](.travis.yml). A maintainer will otherwise confirm it runs on these.
|
145
|
+
|
146
|
+
1. *Bonus Points* Update [CHANGELOG.md](https://github.com/rails-api/active_model_serializers/blob/master/CHANGELOG.md)
|
147
|
+
with a brief description of any breaking changes, fixes, features, or
|
148
|
+
miscellaneous changes under the proper version section.
|
149
|
+
1. Iterate on feedback given by the community (fix syntax, modify bits of code, add
|
150
|
+
tests), pushing the new commits to the PR each time
|
151
|
+
|
152
|
+
Remember to [squash your commits](CONTRIBUTING.md#about-pull-requests-prs) and rebase off `master`.
|
153
|
+
|
154
|
+
#### How maintainers handle pull requests:
|
155
|
+
|
156
|
+
- If the tests pass and the pull request looks good, a maintainer will merge it.
|
157
|
+
- If the pull request needs to be changed,
|
158
|
+
- you can change it by updating the branch you generated the pull request from
|
159
|
+
- either by adding more commits, or
|
160
|
+
- by force pushing to it
|
161
|
+
- A maintainer can make any changes themselves and manually merge the code in.
|
162
|
+
|
163
|
+
#### Commit Messages
|
164
|
+
|
165
|
+
- [A Note About Git Commit Messages](http://tbaggery.com/2008/04/19/a-note-about-git-commit-messages.html)
|
166
|
+
- [http://stopwritingramblingcommitmessages.com/](http://stopwritingramblingcommitmessages.com/)
|
167
|
+
- [ThoughtBot style guide](https://github.com/thoughtbot/guides/tree/master/style#git)
|
168
|
+
|
169
|
+
#### About Pull Requests (PR's)
|
170
|
+
|
171
|
+
- [Using Pull Requests](https://help.github.com/articles/using-pull-requests)
|
172
|
+
- [Github pull requests made easy](http://www.element84.com/github-pull-requests-made-easy.html)
|
173
|
+
- [Exercism Git Workflow](http://help.exercism.io/git-workflow.html).
|
174
|
+
- [Level up your Git](http://rakeroutes.com/blog/deliberate-git/)
|
175
|
+
- [All Your Open Source Code Are Belong To Us](http://www.benjaminfleischer.com/2013/07/30/all-your-open-source-code-are-belong-to-us/)
|
16
176
|
|
17
177
|
## Issue Labeling
|
18
178
|
|
19
|
-
|
179
|
+
ActiveModelSerializers uses a subset of [StandardIssueLabels](https://github.com/wagenet/StandardIssueLabels) for Github Issues. You can [see our labels here](https://github.com/rails-api/active_model_serializers/labels).
|
180
|
+
|
181
|
+
## Running tests
|
182
|
+
|
183
|
+
Run tests against different Rails versions by setting the RAILS_VERSION variable
|
184
|
+
and bundling gems. To test against all versions, you can do something like:
|
185
|
+
|
186
|
+
```bash
|
187
|
+
for version in 4.0 4.1 4.2 master; do
|
188
|
+
export RAILS_VERSION="$version"
|
189
|
+
rm -f Gemfile.lock
|
190
|
+
bundle check || bundle --local || bundle
|
191
|
+
bundle exec rake test
|
192
|
+
if [ "$?" -eq 0 ]; then
|
193
|
+
# green in ANSI
|
194
|
+
echo -e "\033[32m **** Tests passed against Rails ${RAILS_VERSION} **** \033[0m"
|
195
|
+
else
|
196
|
+
# red in ANSI
|
197
|
+
echo -e "\033[31m **** Tests failed against Rails ${RAILS_VERSION} **** \033[0m"
|
198
|
+
fi
|
199
|
+
unset RAILS_VERSION
|
200
|
+
done
|
201
|
+
```
|
202
|
+
|
203
|
+
|
204
|
+
### Running with Rake
|
205
|
+
|
206
|
+
The easiest way to run the unit tests is through Rake. The default task runs
|
207
|
+
the entire test suite for all classes. For more information, checkout the
|
208
|
+
full array of rake tasks with "rake -T"
|
209
|
+
|
210
|
+
Rake can be found at http://docs.seattlerb.org/rake/.
|
211
|
+
|
212
|
+
To run a single test suite
|
213
|
+
|
214
|
+
`$ rake test TEST=path/to/test.rb`
|
20
215
|
|
21
|
-
|
216
|
+
Which can be further narrowed down to one test:
|
22
217
|
|
23
|
-
|
24
|
-
2. Create your feature branch (`git checkout -b my-new-feature`)
|
25
|
-
3. Write tests for your feature, or regression tests highlighting a bug
|
26
|
-
4. Write the feature itself, or fix your bug
|
27
|
-
5. Commit your changes (`git commit -am 'Add some feature'`)
|
28
|
-
6. Push to the branch (`git push origin my-new-feature`)
|
29
|
-
7. Create a new Pull Request
|
218
|
+
`$ rake test TEST=path/to/test.rb TESTOPTS="--name=test_something"`
|
30
219
|
|
31
|
-
|
220
|
+
:heart: :sparkling_heart: :heart:
|
data/Gemfile
CHANGED
@@ -11,38 +11,39 @@ version = ENV['RAILS_VERSION'] || '4.2'
|
|
11
11
|
|
12
12
|
if version == 'master'
|
13
13
|
gem 'rack', github: 'rack/rack'
|
14
|
+
gem 'arel', github: 'rails/arel'
|
14
15
|
git 'https://github.com/rails/rails.git' do
|
15
16
|
gem 'railties'
|
16
17
|
gem 'activesupport'
|
17
18
|
gem 'activemodel'
|
18
19
|
gem 'actionpack'
|
20
|
+
gem 'activerecord', group: :test
|
19
21
|
# Rails 5
|
20
22
|
gem 'actionview'
|
21
23
|
end
|
22
|
-
# Rails 5
|
23
|
-
gem 'rails-controller-testing', github: 'rails/rails-controller-testing'
|
24
24
|
else
|
25
25
|
gem_version = "~> #{version}.0"
|
26
26
|
gem 'railties', gem_version
|
27
27
|
gem 'activesupport', gem_version
|
28
28
|
gem 'activemodel', gem_version
|
29
29
|
gem 'actionpack', gem_version
|
30
|
+
gem 'activerecord', gem_version, group: :test
|
30
31
|
end
|
31
32
|
|
33
|
+
# https://github.com/bundler/bundler/blob/89a8778c19269561926cea172acdcda241d26d23/lib/bundler/dependency.rb#L30-L54
|
34
|
+
@windows_platforms = [:mswin, :mingw, :x64_mingw]
|
35
|
+
|
36
|
+
# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
|
37
|
+
gem 'tzinfo-data', platforms: (@windows_platforms + [:jruby])
|
38
|
+
|
32
39
|
group :test do
|
33
|
-
gem '
|
34
|
-
gem 'sqlite3', platform: [:ruby, :mingw, :x64_mingw, :mswin]
|
40
|
+
gem 'sqlite3', platform: (@windows_platforms + [:ruby])
|
35
41
|
gem 'activerecord-jdbcsqlite3-adapter', platform: :jruby
|
36
|
-
gem 'codeclimate-test-reporter', require: false
|
37
|
-
end
|
38
42
|
|
39
|
-
|
40
|
-
gem 'simplecov', '~> 0.10', require: false
|
43
|
+
gem 'codeclimate-test-reporter', require: false
|
44
|
+
gem 'simplecov', '~> 0.10', require: false, group: :development
|
41
45
|
end
|
42
46
|
|
43
|
-
# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
|
44
|
-
gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]
|
45
|
-
|
46
47
|
group :development, :test do
|
47
48
|
gem 'rubocop', '~> 0.34.0', require: false
|
48
49
|
end
|
data/README.md
CHANGED
@@ -1,216 +1,53 @@
|
|
1
|
-
#
|
1
|
+
# ActiveModelSerializers
|
2
2
|
|
3
|
-
[![Build Status](https://travis-ci.org/rails-api/active_model_serializers.svg)](https://travis-ci.org/rails-api/active_model_serializers)
|
4
|
-
|
5
|
-
|
3
|
+
[![Build Status](https://travis-ci.org/rails-api/active_model_serializers.svg?branch=master)](https://travis-ci.org/rails-api/active_model_serializers)
|
4
|
+
(Windows: [![Build status](https://ci.appveyor.com/api/projects/status/x6xdjydutm54gvyt/branch/master?svg=true)](https://ci.appveyor.com/project/joaomdmoura/active-model-serializers/branch/master))
|
5
|
+
[![Code Quality](https://codeclimate.com/github/rails-api/active_model_serializers/badges/gpa.svg)](https://codeclimate.com/github/rails-api/active_model_serializers)
|
6
|
+
[![Test Coverage](https://codeclimate.com/github/rails-api/active_model_serializers/badges/coverage.svg)](https://codeclimate.com/github/rails-api/active_model_serializers/coverage)
|
6
7
|
|
7
|
-
|
8
|
+
## Documentation
|
8
9
|
|
9
|
-
|
10
|
+
- [0.10 (master) Documentation](https://github.com/rails-api/active_model_serializers/tree/master)
|
11
|
+
- [![API Docs](http://img.shields.io/badge/yard-docs-blue.svg)](http://www.rubydoc.info/github/rails-api/active_model_serializers)
|
12
|
+
- [Guides](docs)
|
13
|
+
- [0.9 (0-9-stable) Documentation](https://github.com/rails-api/active_model_serializers/tree/0-9-stable)
|
14
|
+
- [0.8 (0-8-stable) Documentation](https://github.com/rails-api/active_model_serializers/tree/0-8-stable)
|
10
15
|
|
11
|
-
|
12
|
-
Serializers describe _which_ attributes and relationships should be serialized.
|
13
|
-
Adapters describe _how_ attributes and relationships should be serialized.
|
14
|
-
|
15
|
-
By default AMS will use the **Flatten Json Adapter**. But we strongly advise you to use **JsonApi Adapter** that follows 1.0 of the format specified in [jsonapi.org/format](http://jsonapi.org/format).
|
16
|
-
Check how to change the adapter in the sections bellow.
|
17
|
-
|
18
|
-
# RELEASE CANDIDATE, PLEASE READ
|
19
|
-
|
20
|
-
This is the master branch of AMS. It will become the `0.10.0` release when it's
|
21
|
-
ready. Currently this is a release candidate. This is **not** backward
|
22
|
-
compatible with `0.9.0` or `0.8.0`.
|
23
|
-
|
24
|
-
`0.10.x` will be based on the `0.8.0` code, but with a more flexible
|
25
|
-
architecture. We'd love your help. [Learn how you can help here.](https://github.com/rails-api/active_model_serializers/blob/master/CONTRIBUTING.md)
|
26
|
-
|
27
|
-
## Example
|
28
|
-
|
29
|
-
Given two models, a `Post(title: string, body: text)` and a
|
30
|
-
`Comment(name: string, body: text, post_id: integer)`, you will have two
|
31
|
-
serializers:
|
32
|
-
|
33
|
-
```ruby
|
34
|
-
class PostSerializer < ActiveModel::Serializer
|
35
|
-
cache key: 'posts', expires_in: 3.hours
|
36
|
-
attributes :title, :body
|
37
|
-
|
38
|
-
has_many :comments
|
39
|
-
end
|
40
|
-
```
|
41
|
-
|
42
|
-
and
|
43
|
-
|
44
|
-
```ruby
|
45
|
-
class CommentSerializer < ActiveModel::Serializer
|
46
|
-
attributes :name, :body
|
47
|
-
|
48
|
-
belongs_to :post
|
49
|
-
end
|
50
|
-
```
|
51
|
-
|
52
|
-
Generally speaking, you as a user of AMS will write (or generate) these
|
53
|
-
serializer classes. If you want to use a different adapter, such as a JsonApi, you can
|
54
|
-
change this in an initializer:
|
55
|
-
|
56
|
-
```ruby
|
57
|
-
ActiveModel::Serializer.config.adapter = ActiveModel::Serializer::Adapter::JsonApi
|
58
|
-
```
|
59
|
-
|
60
|
-
or
|
61
|
-
|
62
|
-
```ruby
|
63
|
-
ActiveModel::Serializer.config.adapter = :json_api
|
64
|
-
```
|
65
|
-
|
66
|
-
You won't need to implement an adapter unless you wish to use a new format or
|
67
|
-
media type with AMS.
|
68
|
-
|
69
|
-
If you want to have a root key on your responses you should use the Json adapter, instead of the default FlattenJson:
|
70
|
-
|
71
|
-
```ruby
|
72
|
-
ActiveModel::Serializer.config.adapter = :json
|
73
|
-
```
|
74
|
-
|
75
|
-
If you would like the key in the outputted JSON to be different from its name in ActiveRecord, you can use the :key option to customize it:
|
76
|
-
|
77
|
-
```ruby
|
78
|
-
class PostSerializer < ActiveModel::Serializer
|
79
|
-
attributes :id, :body
|
80
|
-
|
81
|
-
# look up :subject on the model, but use +title+ in the JSON
|
82
|
-
attribute :subject, :key => :title
|
83
|
-
has_many :comments
|
84
|
-
end
|
85
|
-
```
|
86
|
-
|
87
|
-
In your controllers, when you use `render :json`, Rails will now first search
|
88
|
-
for a serializer for the object and use it if available.
|
89
|
-
|
90
|
-
```ruby
|
91
|
-
class PostsController < ApplicationController
|
92
|
-
def show
|
93
|
-
@post = Post.find(params[:id])
|
94
|
-
|
95
|
-
render json: @post
|
96
|
-
end
|
97
|
-
end
|
98
|
-
```
|
99
|
-
|
100
|
-
In this case, Rails will look for a serializer named `PostSerializer`, and if
|
101
|
-
it exists, use it to serialize the `Post`.
|
102
|
-
|
103
|
-
### Specify a serializer
|
104
|
-
|
105
|
-
If you wish to use a serializer other than the default, you can explicitly pass it to the renderer.
|
106
|
-
|
107
|
-
#### 1. For a resource:
|
108
|
-
|
109
|
-
```ruby
|
110
|
-
render json: @post, serializer: PostPreviewSerializer
|
111
|
-
```
|
112
|
-
|
113
|
-
#### 2. For an array resource:
|
114
|
-
|
115
|
-
```ruby
|
116
|
-
# Use the default `ArraySerializer`, which will use `each_serializer` to
|
117
|
-
# serialize each element
|
118
|
-
render json: @posts, each_serializer: PostPreviewSerializer
|
119
|
-
|
120
|
-
# Or, you can explicitly provide the collection serializer as well
|
121
|
-
render json: @posts, serializer: CollectionSerializer, each_serializer: PostPreviewSerializer
|
122
|
-
```
|
123
|
-
|
124
|
-
### Meta
|
125
|
-
|
126
|
-
If you want a `meta` attribute in your response, specify it in the `render`
|
127
|
-
call:
|
128
|
-
|
129
|
-
```ruby
|
130
|
-
render json: @post, meta: { total: 10 }
|
131
|
-
```
|
132
|
-
|
133
|
-
The key can be customized using `meta_key` option.
|
134
|
-
|
135
|
-
```ruby
|
136
|
-
render json: @post, meta: { total: 10 }, meta_key: "custom_meta"
|
137
|
-
```
|
138
|
-
|
139
|
-
`meta` will only be included in your response if you are using an Adapter that supports `root`, as JsonAPI and Json adapters, the default adapter (FlattenJson) doesn't have `root`.
|
140
|
-
|
141
|
-
### Using a serializer without `render`
|
16
|
+
## About
|
142
17
|
|
143
|
-
|
144
|
-
the resource you want to be serialized and call `.serializable_hash`.
|
145
|
-
|
146
|
-
```ruby
|
147
|
-
def create
|
148
|
-
@message = current_user.messages.create!(message_params)
|
149
|
-
MessageCreationWorker.perform(serialized_message)
|
150
|
-
head 204
|
151
|
-
end
|
152
|
-
|
153
|
-
def serialized_message
|
154
|
-
ActiveModel::SerializableResource.new(@message).serializable_hash
|
155
|
-
end
|
156
|
-
```
|
157
|
-
|
158
|
-
### Overriding association methods
|
159
|
-
|
160
|
-
If you want to override any association, you can use:
|
161
|
-
|
162
|
-
```ruby
|
163
|
-
class PostSerializer < ActiveModel::Serializer
|
164
|
-
attributes :id, :body
|
165
|
-
|
166
|
-
has_many :comments
|
167
|
-
|
168
|
-
def comments
|
169
|
-
object.comments.active
|
170
|
-
end
|
171
|
-
end
|
172
|
-
```
|
18
|
+
ActiveModelSerializers brings convention over configuration to your JSON generation.
|
173
19
|
|
174
|
-
|
20
|
+
ActiveModelSerializers works through two components: **serializers** and **adapters**.
|
175
21
|
|
176
|
-
|
177
|
-
|
178
|
-
```ruby
|
179
|
-
class PostSerializer < ActiveModel::Serializer
|
180
|
-
attributes :id, :body
|
22
|
+
Serializers describe _which_ attributes and relationships should be serialized.
|
181
23
|
|
182
|
-
|
24
|
+
Adapters describe _how_ attributes and relationships should be serialized.
|
183
25
|
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
```
|
26
|
+
SerializableResource co-ordinates the resource, Adapter and Serializer to produce the
|
27
|
+
resource serialization. The serialization has the `#as_json`, `#to_json` and `#serializable_hash`
|
28
|
+
methods used by the Rails JSON Renderer. (SerializableResource actually delegates
|
29
|
+
these methods to the adapter.)
|
189
30
|
|
190
|
-
|
31
|
+
By default ActiveModelSerializers will use the **Attributes Adapter**.
|
32
|
+
But we strongly advise you to use **JsonApi Adapter**, which
|
33
|
+
follows 1.0 of the format specified in [jsonapi.org/format](http://jsonapi.org/format).
|
34
|
+
Check how to change the adapter in the sections below.
|
191
35
|
|
192
|
-
|
36
|
+
## RELEASE CANDIDATE, PLEASE READ
|
193
37
|
|
194
|
-
|
195
|
-
Doesn't follow any specifc convention.
|
38
|
+
This is the **master** branch of ActiveModelSerializers.
|
196
39
|
|
197
|
-
|
40
|
+
It will become the `0.10.0` release when it's ready. Currently this is a release candidate.
|
198
41
|
|
199
|
-
|
200
|
-
Doesn't follow any specifc convention.
|
42
|
+
`0.10.x` is **not** backward compatible with `0.9.x` nor `0.8.x`.
|
201
43
|
|
202
|
-
|
44
|
+
`0.10.x` will be based on the `0.8.0` code, but with a more flexible
|
45
|
+
architecture. We'd love your help. [Learn how you can help here.](CONTRIBUTING.md)
|
203
46
|
|
204
|
-
|
205
|
-
[jsonapi.org/format](http://jsonapi.org/format). It will include the associated
|
206
|
-
resources in the `"included"` member when the resource names are included in the
|
207
|
-
`include` option. Including nested associated resources is also supported.
|
47
|
+
It is generally safe and recommended to use the master branch.
|
208
48
|
|
209
|
-
|
210
|
-
|
211
|
-
# or
|
212
|
-
render @posts, include: 'author,comments,comments.author'
|
213
|
-
```
|
49
|
+
For more information, see the post '[The future of
|
50
|
+
AMS](https://medium.com/@joaomdmoura/the-future-of-ams-e5f9047ca7e9)'.
|
214
51
|
|
215
52
|
## Installation
|
216
53
|
|
@@ -226,128 +63,83 @@ And then execute:
|
|
226
63
|
$ bundle
|
227
64
|
```
|
228
65
|
|
229
|
-
##
|
66
|
+
## Getting Started
|
230
67
|
|
231
|
-
|
232
|
-
will generate a serializer at the same time:
|
68
|
+
See [Getting Started](docs/general/getting_started.md) for the nuts and bolts.
|
233
69
|
|
234
|
-
|
235
|
-
|
236
|
-
```
|
70
|
+
More information is available in the [Guides](docs) and
|
71
|
+
[High-level behavior](README.md#high-level-behavior).
|
237
72
|
|
238
|
-
|
239
|
-
your new model. You can also generate a serializer for an existing model with
|
240
|
-
the serializer generator:
|
73
|
+
## Getting Help
|
241
74
|
|
242
|
-
|
243
|
-
|
244
|
-
```
|
75
|
+
If you find a bug, please report an [Issue](https://github.com/rails-api/active_model_serializers/issues/new)
|
76
|
+
and see our [contributing guide](CONTRIBUTING.md).
|
245
77
|
|
246
|
-
|
247
|
-
`has_many`/`has_one`/`belongs_to` declarations, based on the model. For example:
|
78
|
+
If you have a question, please [post to Stack Overflow](http://stackoverflow.com/questions/tagged/active-model-serializers).
|
248
79
|
|
249
|
-
|
250
|
-
class PostSerializer < ActiveModel::Serializer
|
251
|
-
attributes :title, :body
|
80
|
+
If you'd like to chat, we have a [community slack](http://amserializers.herokuapp.com).
|
252
81
|
|
253
|
-
|
254
|
-
has_one :author
|
255
|
-
end
|
256
|
-
```
|
82
|
+
Thanks!
|
257
83
|
|
258
|
-
|
84
|
+
## High-level behavior
|
259
85
|
|
260
|
-
|
261
|
-
class CommentSerializer < ActiveModel::Serializer
|
262
|
-
attributes :name, :body
|
86
|
+
Given a [serializable model](lib/active_model/serializer/lint.rb):
|
263
87
|
|
264
|
-
|
88
|
+
```ruby
|
89
|
+
# either
|
90
|
+
class SomeResource < ActiveRecord::Base
|
91
|
+
# columns: title, body
|
92
|
+
end
|
93
|
+
# or
|
94
|
+
class SomeResource < ActiveModelSerializers::Model
|
95
|
+
attr_accessor :title, :body
|
265
96
|
end
|
266
97
|
```
|
267
98
|
|
268
|
-
|
269
|
-
|
270
|
-
The `has_many`, `has_one`, and `belongs_to` declarations describe relationships between
|
271
|
-
resources. By default, when you serialize a `Post`, you will get its `Comments`
|
272
|
-
as well.
|
273
|
-
|
274
|
-
You may also use the `:serializer` option to specify a custom serializer class, for example:
|
99
|
+
And initialized as:
|
275
100
|
|
276
101
|
```ruby
|
277
|
-
|
102
|
+
resource = SomeResource.new(title: 'ActiveModelSerializers', body: 'Convention over configuration')
|
278
103
|
```
|
279
104
|
|
280
|
-
|
105
|
+
Given a serializer for the serializable model:
|
281
106
|
|
282
107
|
```ruby
|
283
|
-
|
108
|
+
class SomeSerializer < ActiveModel::Serializer
|
109
|
+
attribute :title, key: :name
|
110
|
+
attributes :body
|
111
|
+
end
|
284
112
|
```
|
285
113
|
|
286
|
-
|
287
|
-
|
288
|
-
Pagination links will be included in your response automatically as long as the resource is paginated using [Kaminari](https://github.com/amatsuda/kaminari) or [WillPaginate](https://github.com/mislav/will_paginate) and if you are using a ```JSON-API``` adapter.
|
289
|
-
|
290
|
-
Although the others adapters does not have this feature, it is possible to implement pagination links to `JSON` adapter. For more information about it, please see in our docs [How to add pagination links](https://github.com/rails-api/active_model_serializers/blob/master/docs/howto/add_pagination_links.md)
|
291
|
-
|
292
|
-
## Caching
|
293
|
-
|
294
|
-
To cache a serializer, call ```cache``` and pass its options.
|
295
|
-
The options are the same options of ```ActiveSupport::Cache::Store```, plus
|
296
|
-
a ```key``` option that will be the prefix of the object cache
|
297
|
-
on a pattern ```"#{key}/#{object.id}-#{object.updated_at}"```.
|
298
|
-
|
299
|
-
The cache support is optimized to use the cached object in multiple request. An object cached on a ```show``` request will be reused at the ```index```. If there is a relationship with another cached serializer it will also be created and reused automatically.
|
300
|
-
|
301
|
-
**[NOTE] Every object is individually cached.**
|
302
|
-
|
303
|
-
**[NOTE] The cache is automatically expired after an object is updated, but it's not deleted.**
|
114
|
+
The model can be serialized as:
|
304
115
|
|
305
116
|
```ruby
|
306
|
-
|
117
|
+
options = {}
|
118
|
+
serialization = SerializableResource.new(resource, options)
|
119
|
+
serialization.to_json
|
120
|
+
serialization.as_json
|
307
121
|
```
|
308
122
|
|
309
|
-
|
123
|
+
SerializableResource delegates to the adapter, which it builds as:
|
310
124
|
|
311
125
|
```ruby
|
312
|
-
|
313
|
-
|
314
|
-
|
315
|
-
|
316
|
-
|
317
|
-
end
|
126
|
+
adapter_options = {}
|
127
|
+
adapter = Adapter.create(serializer, adapter_options)
|
128
|
+
adapter.to_json
|
129
|
+
adapter.as_json
|
130
|
+
adapter.serializable_hash
|
318
131
|
```
|
319
132
|
|
320
|
-
|
321
|
-
the key ```"post/#{post.id}-#{post.updated_at}"```. You can use this key to expire it as you want,
|
322
|
-
but in this case it will be automatically expired after 3 hours.
|
323
|
-
|
324
|
-
### Fragmenting Caching
|
325
|
-
|
326
|
-
If there is some API endpoint that shouldn't be fully cached, you can still optimise it, using Fragment Cache on the attributes and relationships that you want to cache.
|
327
|
-
|
328
|
-
You can define the attribute by using ```only``` or ```except``` option on cache method.
|
329
|
-
|
330
|
-
**[NOTE] Cache serializers will be used at their relationships**
|
331
|
-
|
332
|
-
Example:
|
133
|
+
The adapter formats the serializer's attributes and associations (a.k.a. includes):
|
333
134
|
|
334
135
|
```ruby
|
335
|
-
|
336
|
-
|
337
|
-
|
338
|
-
|
339
|
-
has_many :comments
|
340
|
-
end
|
136
|
+
serializer_options = {}
|
137
|
+
serializer = SomeSerializer.new(resource, serializer_options)
|
138
|
+
serializer.attributes
|
139
|
+
serializer.associations
|
341
140
|
```
|
342
|
-
|
343
|
-
## Getting Help
|
344
|
-
|
345
|
-
If you find a bug, please report an [Issue](https://github.com/rails-api/active_model_serializers/issues/new).
|
346
|
-
|
347
|
-
If you have a question, please [post to Stack Overflow](http://stackoverflow.com/questions/tagged/active-model-serializers).
|
348
|
-
|
349
|
-
Thanks!
|
141
|
+
See [ARCHITECTURE.md](docs/ARCHITECTURE.md) for more information.
|
350
142
|
|
351
143
|
# Contributing
|
352
144
|
|
353
|
-
See [CONTRIBUTING.md](
|
145
|
+
See [CONTRIBUTING.md](CONTRIBUTING.md)
|