active_model_serializers_custom 0.10.90
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.github/ISSUE_TEMPLATE.md +29 -0
- data/.github/PULL_REQUEST_TEMPLATE.md +15 -0
- data/.gitignore +35 -0
- data/.rubocop.yml +109 -0
- data/.simplecov +110 -0
- data/.travis.yml +63 -0
- data/CHANGELOG.md +727 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/CONTRIBUTING.md +105 -0
- data/Gemfile +74 -0
- data/MIT-LICENSE +22 -0
- data/README.md +305 -0
- data/Rakefile +76 -0
- data/active_model_serializers.gemspec +64 -0
- data/appveyor.yml +28 -0
- data/bin/bench +171 -0
- data/bin/bench_regression +316 -0
- data/bin/rubocop +38 -0
- data/bin/serve_benchmark +39 -0
- data/docs/README.md +41 -0
- data/docs/STYLE.md +58 -0
- data/docs/general/adapters.md +269 -0
- data/docs/general/caching.md +58 -0
- data/docs/general/configuration_options.md +185 -0
- data/docs/general/deserialization.md +100 -0
- data/docs/general/fields.md +31 -0
- data/docs/general/getting_started.md +133 -0
- data/docs/general/instrumentation.md +40 -0
- data/docs/general/key_transforms.md +40 -0
- data/docs/general/logging.md +21 -0
- data/docs/general/rendering.md +293 -0
- data/docs/general/serializers.md +495 -0
- data/docs/how-open-source-maintained.jpg +0 -0
- data/docs/howto/add_pagination_links.md +138 -0
- data/docs/howto/add_relationship_links.md +140 -0
- data/docs/howto/add_root_key.md +62 -0
- data/docs/howto/grape_integration.md +42 -0
- data/docs/howto/outside_controller_use.md +66 -0
- data/docs/howto/passing_arbitrary_options.md +27 -0
- data/docs/howto/serialize_poro.md +73 -0
- data/docs/howto/test.md +154 -0
- data/docs/howto/upgrade_from_0_8_to_0_10.md +265 -0
- data/docs/integrations/ember-and-json-api.md +147 -0
- data/docs/integrations/grape.md +19 -0
- data/docs/jsonapi/errors.md +56 -0
- data/docs/jsonapi/schema.md +151 -0
- data/docs/jsonapi/schema/schema.json +366 -0
- data/docs/rfcs/0000-namespace.md +106 -0
- data/docs/rfcs/template.md +15 -0
- data/lib/action_controller/serialization.rb +76 -0
- data/lib/active_model/serializable_resource.rb +13 -0
- data/lib/active_model/serializer.rb +418 -0
- data/lib/active_model/serializer/adapter.rb +26 -0
- data/lib/active_model/serializer/adapter/attributes.rb +17 -0
- data/lib/active_model/serializer/adapter/base.rb +20 -0
- data/lib/active_model/serializer/adapter/json.rb +17 -0
- data/lib/active_model/serializer/adapter/json_api.rb +17 -0
- data/lib/active_model/serializer/adapter/null.rb +17 -0
- data/lib/active_model/serializer/array_serializer.rb +14 -0
- data/lib/active_model/serializer/association.rb +91 -0
- data/lib/active_model/serializer/attribute.rb +27 -0
- data/lib/active_model/serializer/belongs_to_reflection.rb +13 -0
- data/lib/active_model/serializer/collection_serializer.rb +90 -0
- data/lib/active_model/serializer/concerns/caching.rb +304 -0
- data/lib/active_model/serializer/error_serializer.rb +16 -0
- data/lib/active_model/serializer/errors_serializer.rb +34 -0
- data/lib/active_model/serializer/field.rb +92 -0
- data/lib/active_model/serializer/fieldset.rb +33 -0
- data/lib/active_model/serializer/has_many_reflection.rb +12 -0
- data/lib/active_model/serializer/has_one_reflection.rb +9 -0
- data/lib/active_model/serializer/lazy_association.rb +99 -0
- data/lib/active_model/serializer/link.rb +23 -0
- data/lib/active_model/serializer/lint.rb +152 -0
- data/lib/active_model/serializer/null.rb +19 -0
- data/lib/active_model/serializer/reflection.rb +212 -0
- data/lib/active_model/serializer/version.rb +7 -0
- data/lib/active_model_serializers.rb +63 -0
- data/lib/active_model_serializers/adapter.rb +100 -0
- data/lib/active_model_serializers/adapter/attributes.rb +15 -0
- data/lib/active_model_serializers/adapter/base.rb +85 -0
- data/lib/active_model_serializers/adapter/json.rb +23 -0
- data/lib/active_model_serializers/adapter/json_api.rb +535 -0
- data/lib/active_model_serializers/adapter/json_api/deserialization.rb +215 -0
- data/lib/active_model_serializers/adapter/json_api/error.rb +98 -0
- data/lib/active_model_serializers/adapter/json_api/jsonapi.rb +51 -0
- data/lib/active_model_serializers/adapter/json_api/link.rb +85 -0
- data/lib/active_model_serializers/adapter/json_api/meta.rb +39 -0
- data/lib/active_model_serializers/adapter/json_api/pagination_links.rb +90 -0
- data/lib/active_model_serializers/adapter/json_api/relationship.rb +106 -0
- data/lib/active_model_serializers/adapter/json_api/resource_identifier.rb +68 -0
- data/lib/active_model_serializers/adapter/null.rb +11 -0
- data/lib/active_model_serializers/callbacks.rb +57 -0
- data/lib/active_model_serializers/deprecate.rb +56 -0
- data/lib/active_model_serializers/deserialization.rb +17 -0
- data/lib/active_model_serializers/json_pointer.rb +16 -0
- data/lib/active_model_serializers/logging.rb +124 -0
- data/lib/active_model_serializers/lookup_chain.rb +82 -0
- data/lib/active_model_serializers/model.rb +132 -0
- data/lib/active_model_serializers/railtie.rb +52 -0
- data/lib/active_model_serializers/register_jsonapi_renderer.rb +80 -0
- data/lib/active_model_serializers/serializable_resource.rb +84 -0
- data/lib/active_model_serializers/serialization_context.rb +41 -0
- data/lib/active_model_serializers/test.rb +9 -0
- data/lib/active_model_serializers/test/schema.rb +140 -0
- data/lib/active_model_serializers/test/serializer.rb +127 -0
- data/lib/generators/rails/USAGE +6 -0
- data/lib/generators/rails/resource_override.rb +12 -0
- data/lib/generators/rails/serializer_generator.rb +38 -0
- data/lib/generators/rails/templates/serializer.rb.erb +8 -0
- data/lib/grape/active_model_serializers.rb +18 -0
- data/lib/grape/formatters/active_model_serializers.rb +34 -0
- data/lib/grape/helpers/active_model_serializers.rb +19 -0
- data/lib/tasks/rubocop.rake +55 -0
- data/test/action_controller/adapter_selector_test.rb +64 -0
- data/test/action_controller/explicit_serializer_test.rb +137 -0
- data/test/action_controller/json/include_test.rb +248 -0
- data/test/action_controller/json_api/deserialization_test.rb +114 -0
- data/test/action_controller/json_api/errors_test.rb +42 -0
- data/test/action_controller/json_api/fields_test.rb +68 -0
- data/test/action_controller/json_api/linked_test.rb +204 -0
- data/test/action_controller/json_api/pagination_test.rb +126 -0
- data/test/action_controller/json_api/transform_test.rb +191 -0
- data/test/action_controller/lookup_proc_test.rb +51 -0
- data/test/action_controller/namespace_lookup_test.rb +239 -0
- data/test/action_controller/serialization_scope_name_test.rb +237 -0
- data/test/action_controller/serialization_test.rb +480 -0
- data/test/active_model_serializers/adapter_for_test.rb +210 -0
- data/test/active_model_serializers/json_pointer_test.rb +24 -0
- data/test/active_model_serializers/logging_test.rb +79 -0
- data/test/active_model_serializers/model_test.rb +144 -0
- data/test/active_model_serializers/railtie_test_isolated.rb +70 -0
- data/test/active_model_serializers/register_jsonapi_renderer_test_isolated.rb +163 -0
- data/test/active_model_serializers/serialization_context_test_isolated.rb +73 -0
- data/test/active_model_serializers/test/schema_test.rb +133 -0
- data/test/active_model_serializers/test/serializer_test.rb +64 -0
- data/test/active_record_test.rb +11 -0
- data/test/adapter/attributes_test.rb +42 -0
- data/test/adapter/deprecation_test.rb +102 -0
- data/test/adapter/json/belongs_to_test.rb +47 -0
- data/test/adapter/json/collection_test.rb +106 -0
- data/test/adapter/json/has_many_test.rb +55 -0
- data/test/adapter/json/transform_test.rb +95 -0
- data/test/adapter/json_api/belongs_to_test.rb +157 -0
- data/test/adapter/json_api/collection_test.rb +98 -0
- data/test/adapter/json_api/errors_test.rb +78 -0
- data/test/adapter/json_api/fields_test.rb +98 -0
- data/test/adapter/json_api/has_many_explicit_serializer_test.rb +98 -0
- data/test/adapter/json_api/has_many_test.rb +175 -0
- data/test/adapter/json_api/has_one_test.rb +82 -0
- data/test/adapter/json_api/include_data_if_sideloaded_test.rb +215 -0
- data/test/adapter/json_api/json_api_test.rb +35 -0
- data/test/adapter/json_api/linked_test.rb +415 -0
- data/test/adapter/json_api/links_test.rb +112 -0
- data/test/adapter/json_api/pagination_links_test.rb +208 -0
- data/test/adapter/json_api/parse_test.rb +139 -0
- data/test/adapter/json_api/relationship_test.rb +399 -0
- data/test/adapter/json_api/resource_meta_test.rb +102 -0
- data/test/adapter/json_api/toplevel_jsonapi_test.rb +84 -0
- data/test/adapter/json_api/transform_test.rb +514 -0
- data/test/adapter/json_api/type_test.rb +195 -0
- data/test/adapter/json_test.rb +48 -0
- data/test/adapter/null_test.rb +24 -0
- data/test/adapter/polymorphic_test.rb +220 -0
- data/test/adapter_test.rb +69 -0
- data/test/array_serializer_test.rb +24 -0
- data/test/benchmark/app.rb +67 -0
- data/test/benchmark/benchmarking_support.rb +69 -0
- data/test/benchmark/bm_active_record.rb +83 -0
- data/test/benchmark/bm_adapter.rb +40 -0
- data/test/benchmark/bm_caching.rb +121 -0
- data/test/benchmark/bm_lookup_chain.rb +85 -0
- data/test/benchmark/bm_transform.rb +47 -0
- data/test/benchmark/config.ru +3 -0
- data/test/benchmark/controllers.rb +85 -0
- data/test/benchmark/fixtures.rb +221 -0
- data/test/cache_test.rb +717 -0
- data/test/collection_serializer_test.rb +129 -0
- data/test/fixtures/active_record.rb +115 -0
- data/test/fixtures/poro.rb +227 -0
- data/test/generators/scaffold_controller_generator_test.rb +26 -0
- data/test/generators/serializer_generator_test.rb +77 -0
- data/test/grape_test.rb +198 -0
- data/test/lint_test.rb +51 -0
- data/test/logger_test.rb +22 -0
- data/test/poro_test.rb +11 -0
- data/test/serializable_resource_test.rb +81 -0
- data/test/serializers/association_macros_test.rb +39 -0
- data/test/serializers/associations_test.rb +520 -0
- data/test/serializers/attribute_test.rb +155 -0
- data/test/serializers/attributes_test.rb +54 -0
- data/test/serializers/caching_configuration_test_isolated.rb +172 -0
- data/test/serializers/configuration_test.rb +34 -0
- data/test/serializers/fieldset_test.rb +16 -0
- data/test/serializers/meta_test.rb +204 -0
- data/test/serializers/options_test.rb +34 -0
- data/test/serializers/read_attribute_for_serialization_test.rb +81 -0
- data/test/serializers/reflection_test.rb +481 -0
- data/test/serializers/root_test.rb +23 -0
- data/test/serializers/serialization_test.rb +57 -0
- data/test/serializers/serializer_for_test.rb +138 -0
- data/test/serializers/serializer_for_with_namespace_test.rb +90 -0
- data/test/support/custom_schemas/active_model_serializers/test/schema_test/my/index.json +6 -0
- data/test/support/isolated_unit.rb +86 -0
- data/test/support/rails5_shims.rb +55 -0
- data/test/support/rails_app.rb +40 -0
- 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 +81 -0
- data/test/test_helper.rb +72 -0
- metadata +622 -0
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
In the interest of fostering an open and welcoming environment, we as
|
6
|
+
contributors and maintainers pledge to making participation in our project and
|
7
|
+
our community a harassment-free experience for everyone, regardless of age, body
|
8
|
+
size, disability, ethnicity, gender identity and expression, level of experience,
|
9
|
+
nationality, personal appearance, race, religion, or sexual identity and
|
10
|
+
orientation.
|
11
|
+
|
12
|
+
## Our Standards
|
13
|
+
|
14
|
+
Examples of behavior that contributes to creating a positive environment
|
15
|
+
include:
|
16
|
+
|
17
|
+
* Using welcoming and inclusive language
|
18
|
+
* Being respectful of differing viewpoints and experiences
|
19
|
+
* Gracefully accepting constructive criticism
|
20
|
+
* Focusing on what is best for the community
|
21
|
+
* Showing empathy towards other community members
|
22
|
+
|
23
|
+
Examples of unacceptable behavior by participants include:
|
24
|
+
|
25
|
+
* The use of sexualized language or imagery and unwelcome sexual attention or
|
26
|
+
advances
|
27
|
+
* Trolling, insulting/derogatory comments, and personal or political attacks
|
28
|
+
* Public or private harassment
|
29
|
+
* Publishing others' private information, such as a physical or electronic
|
30
|
+
address, without explicit permission
|
31
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
32
|
+
professional setting
|
33
|
+
|
34
|
+
## Our Responsibilities
|
35
|
+
|
36
|
+
Project maintainers are responsible for clarifying the standards of acceptable
|
37
|
+
behavior and are expected to take appropriate and fair corrective action in
|
38
|
+
response to any instances of unacceptable behavior.
|
39
|
+
|
40
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
41
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
42
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
43
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
44
|
+
threatening, offensive, or harmful.
|
45
|
+
|
46
|
+
## Scope
|
47
|
+
|
48
|
+
This Code of Conduct applies both within project spaces and in public spaces
|
49
|
+
when an individual is representing the project or its community. Examples of
|
50
|
+
representing a project or community include using an official project e-mail
|
51
|
+
address, posting via an official social media account, or acting as an appointed
|
52
|
+
representative at an online or offline event. Representation of a project may be
|
53
|
+
further defined and clarified by project maintainers.
|
54
|
+
|
55
|
+
## Enforcement
|
56
|
+
|
57
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
58
|
+
reported by contacting one of the owners listed at https://rubygems.org/gems/active_model_serializers. All
|
59
|
+
complaints will be reviewed and investigated and will result in a response that
|
60
|
+
is deemed necessary and appropriate to the circumstances. The project team is
|
61
|
+
obligated to maintain confidentiality with regard to the reporter of an incident.
|
62
|
+
Further details of specific enforcement policies may be posted separately.
|
63
|
+
|
64
|
+
Project maintainers who do not follow or enforce the Code of Conduct in good
|
65
|
+
faith may face temporary or permanent repercussions as determined by other
|
66
|
+
members of the project's leadership.
|
67
|
+
|
68
|
+
## Attribution
|
69
|
+
|
70
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
|
71
|
+
available at [http://contributor-covenant.org/version/1/4][version]
|
72
|
+
|
73
|
+
[homepage]: http://contributor-covenant.org
|
74
|
+
[version]: http://contributor-covenant.org/version/1/4/
|
data/CONTRIBUTING.md
ADDED
@@ -0,0 +1,105 @@
|
|
1
|
+
## Have an issue?
|
2
|
+
|
3
|
+
Before opening an issue, try the following:
|
4
|
+
|
5
|
+
##### Consult the documentation
|
6
|
+
|
7
|
+
See if your issue can be resolved by information in the documentation.
|
8
|
+
|
9
|
+
- [0.10 (master) Documentation](https://github.com/rails-api/active_model_serializers/tree/master/docs)
|
10
|
+
- [![API Docs](http://img.shields.io/badge/yard-docs-blue.svg)](http://www.rubydoc.info/github/rails-api/active_model_serializers/v0.10.0)
|
11
|
+
- [Guides](docs)
|
12
|
+
- [0.9 (0-9-stable) Documentation](https://github.com/rails-api/active_model_serializers/tree/0-9-stable)
|
13
|
+
- [0.8 (0-8-stable) Documentation](https://github.com/rails-api/active_model_serializers/tree/0-8-stable)
|
14
|
+
|
15
|
+
##### Check for an existing issue
|
16
|
+
|
17
|
+
Take a look at the issues to see if a similar one has already been created. If
|
18
|
+
one exists, please add any additional information that might expedite
|
19
|
+
resolution.
|
20
|
+
|
21
|
+
#### Open an issue
|
22
|
+
|
23
|
+
If the documentation wasn't able to help resolve the issue and no issue already
|
24
|
+
exists, please open a new issue with the following in mind:
|
25
|
+
|
26
|
+
- Please make sure only to include one issue per report. If you encounter
|
27
|
+
multiple, unrelated issues, please report them as such.
|
28
|
+
- Be detailed. Provide backtraces and example code when possible. Provide
|
29
|
+
information about your environment. e.g., Ruby version, rails version, etc.
|
30
|
+
- Own your issue. Actively participate in the discussion and help drive the
|
31
|
+
issue to closure.
|
32
|
+
- If you resolve your own issue, please share the details on the issue and close
|
33
|
+
it out. Others might have the same issue and sharing solutions is helpful.
|
34
|
+
|
35
|
+
## Contributing
|
36
|
+
|
37
|
+
Contributing can be done in many ways and is not exclusive to code. If you have
|
38
|
+
thoughts on a particular issue or feature, we encourage you to open new issues
|
39
|
+
for discussion or add your comments to existing ones.
|
40
|
+
|
41
|
+
#### Pull requests
|
42
|
+
|
43
|
+
We also gladly welcome pull requests. When preparing to work on pull request,
|
44
|
+
please adhere to these standards:
|
45
|
+
|
46
|
+
- Base work on the master branch unless fixing an issue with
|
47
|
+
[0.9-stable](https://github.com/rails-api/active_model_serializers/tree/0-9-stable)
|
48
|
+
or
|
49
|
+
[0.8-stable](https://github.com/rails-api/active_model_serializers/tree/0-8-stable)
|
50
|
+
- Squash your commits and regularly rebase off master.
|
51
|
+
- Provide a description of the changes contained in the pull request.
|
52
|
+
- Note any specific areas that should be reviewed.
|
53
|
+
- Include tests.
|
54
|
+
- The test suite must pass on [supported Ruby versions](.travis.yml)
|
55
|
+
- Include updates to the [documentation](https://github.com/rails-api/active_model_serializers/tree/master/docs)
|
56
|
+
where applicable.
|
57
|
+
- Update the
|
58
|
+
[CHANGELOG](https://github.com/rails-api/active_model_serializers/blob/master/CHANGELOG.md)
|
59
|
+
to the appropriate sections with a brief description of the changes.
|
60
|
+
- Do not change the VERSION file.
|
61
|
+
|
62
|
+
#### Running tests
|
63
|
+
|
64
|
+
Run all tests
|
65
|
+
|
66
|
+
`$ rake test`
|
67
|
+
|
68
|
+
Run a single test suite
|
69
|
+
|
70
|
+
`$ rake test TEST=path/to/test.rb`
|
71
|
+
|
72
|
+
Run a single test
|
73
|
+
|
74
|
+
`$ rake test TEST=path/to/test.rb TESTOPTS="--name=test_something"`
|
75
|
+
|
76
|
+
Run tests against different Rails versions by setting the RAILS_VERSION variable
|
77
|
+
and bundling gems. (save this script somewhere executable and run from top of AMS repository)
|
78
|
+
|
79
|
+
```bash
|
80
|
+
#!/usr/bin/env bash
|
81
|
+
|
82
|
+
rcommand='puts YAML.load_file("./.travis.yml")["env"]["matrix"].join(" ").gsub("RAILS_VERSION=", "")'
|
83
|
+
versions=$(ruby -ryaml -e "$rcommand")
|
84
|
+
|
85
|
+
for version in ${versions[@]}; do
|
86
|
+
export RAILS_VERSION="$version"
|
87
|
+
rm -f Gemfile.lock
|
88
|
+
bundle check || bundle --local || bundle
|
89
|
+
bundle exec rake test
|
90
|
+
if [ "$?" -eq 0 ]; then
|
91
|
+
# green in ANSI
|
92
|
+
echo -e "\033[32m **** Tests passed against Rails ${RAILS_VERSION} **** \033[0m"
|
93
|
+
else
|
94
|
+
# red in ANSI
|
95
|
+
echo -e "\033[31m **** Tests failed against Rails ${RAILS_VERSION} **** \033[0m"
|
96
|
+
read -p '[Enter] any key to continue, [q] to quit...' prompt
|
97
|
+
if [ "$prompt" = 'q' ]; then
|
98
|
+
unset RAILS_VERSION
|
99
|
+
exit 1
|
100
|
+
fi
|
101
|
+
fi
|
102
|
+
unset RAILS_VERSION
|
103
|
+
done
|
104
|
+
```
|
105
|
+
|
data/Gemfile
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
source 'https://rubygems.org'
|
4
|
+
#
|
5
|
+
# Add a Gemfile.local to locally bundle gems outside of version control
|
6
|
+
local_gemfile = File.join(File.expand_path('..', __FILE__), 'Gemfile.local')
|
7
|
+
eval_gemfile local_gemfile if File.readable?(local_gemfile)
|
8
|
+
|
9
|
+
# Specify your gem's dependencies in active_model_serializers.gemspec
|
10
|
+
gemspec
|
11
|
+
|
12
|
+
version = ENV['RAILS_VERSION'] || '4.2'
|
13
|
+
|
14
|
+
if version == 'master'
|
15
|
+
gem 'rack', github: 'rack/rack'
|
16
|
+
gem 'arel', github: 'rails/arel'
|
17
|
+
gem 'rails', github: 'rails/rails'
|
18
|
+
git 'https://github.com/rails/rails.git' do
|
19
|
+
gem 'railties'
|
20
|
+
gem 'activesupport'
|
21
|
+
gem 'activemodel'
|
22
|
+
gem 'actionpack'
|
23
|
+
gem 'activerecord', group: :test
|
24
|
+
# Rails 5
|
25
|
+
gem 'actionview'
|
26
|
+
end
|
27
|
+
else
|
28
|
+
gem_version = "~> #{version}.0"
|
29
|
+
gem 'rails', gem_version
|
30
|
+
gem 'railties', gem_version
|
31
|
+
gem 'activesupport', gem_version
|
32
|
+
gem 'activemodel', gem_version
|
33
|
+
gem 'actionpack', gem_version
|
34
|
+
gem 'activerecord', gem_version, group: :test
|
35
|
+
end
|
36
|
+
|
37
|
+
# https://github.com/bundler/bundler/blob/89a8778c19269561926cea172acdcda241d26d23/lib/bundler/dependency.rb#L30-L54
|
38
|
+
@windows_platforms = [:mswin, :mingw, :x64_mingw]
|
39
|
+
|
40
|
+
# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
|
41
|
+
gem 'tzinfo-data', platforms: (@windows_platforms + [:jruby])
|
42
|
+
|
43
|
+
if ENV['CI']
|
44
|
+
if RUBY_VERSION < '2.4'
|
45
|
+
# Windows: An error occurred while installing nokogiri (1.8.0)
|
46
|
+
gem 'nokogiri', '< 1.7', platforms: @windows_platforms
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
group :bench do
|
51
|
+
# https://github.com/rails-api/active_model_serializers/commit/cb4459580a6f4f37f629bf3185a5224c8624ca76
|
52
|
+
gem 'benchmark-ips', '>= 2.7.2', require: false, group: :development
|
53
|
+
end
|
54
|
+
|
55
|
+
group :test do
|
56
|
+
gem 'sqlite3', '~> 1.3.13', platform: (@windows_platforms + [:ruby])
|
57
|
+
platforms :jruby do
|
58
|
+
if version == 'master' || version >= '5'
|
59
|
+
gem 'activerecord-jdbcsqlite3-adapter', '~> 50'
|
60
|
+
else
|
61
|
+
gem 'activerecord-jdbcsqlite3-adapter', '~> 1.3.0'
|
62
|
+
end
|
63
|
+
end
|
64
|
+
gem 'codeclimate-test-reporter', require: false
|
65
|
+
gem 'm', '~> 1.5'
|
66
|
+
gem 'pry', '>= 0.10'
|
67
|
+
gem 'byebug', '~> 8.2' if RUBY_VERSION < '2.2'
|
68
|
+
gem 'pry-byebug', platform: :ruby
|
69
|
+
end
|
70
|
+
|
71
|
+
group :development, :test do
|
72
|
+
gem 'rubocop', '~> 0.40.0', require: false
|
73
|
+
gem 'yard', require: false
|
74
|
+
end
|
data/MIT-LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 Steve Klabnik
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,305 @@
|
|
1
|
+
# ActiveModelSerializers
|
2
|
+
|
3
|
+
<table>
|
4
|
+
<tr>
|
5
|
+
<td>Build Status</td>
|
6
|
+
<td>
|
7
|
+
<a href="https://travis-ci.org/rails-api/active_model_serializers"><img src="https://travis-ci.org/rails-api/active_model_serializers.svg?branch=master" alt="Build Status" ></a>
|
8
|
+
<a href="https://ci.appveyor.com/project/joaomdmoura/active-model-serializers/branch/master"><img src="https://ci.appveyor.com/api/projects/status/x6xdjydutm54gvyt/branch/master?svg=true" alt="Build status"></a>
|
9
|
+
</td>
|
10
|
+
</tr>
|
11
|
+
<tr>
|
12
|
+
<td>Code Quality</td>
|
13
|
+
<td>
|
14
|
+
<a href="https://codeclimate.com/github/rails-api/active_model_serializers"><img src="https://codeclimate.com/github/rails-api/active_model_serializers/badges/gpa.svg" alt="Code Quality"></a>
|
15
|
+
<a href="https://codebeat.co/projects/github-com-rails-api-active_model_serializers"><img src="https://codebeat.co/badges/a9ab35fa-8b5a-4680-9d4e-a81f9a55ebcd" alt="codebeat" ></a>
|
16
|
+
<a href="https://codeclimate.com/github/rails-api/active_model_serializers/coverage"><img src="https://codeclimate.com/github/rails-api/active_model_serializers/badges/coverage.svg" alt="Test Coverage"></a>
|
17
|
+
</td>
|
18
|
+
</tr>
|
19
|
+
<tr>
|
20
|
+
<td>Issue Stats</td>
|
21
|
+
<td>
|
22
|
+
<a href="https://github.com/rails-api/active_model_serializers/pulse/monthly">Pulse</a>
|
23
|
+
</td>
|
24
|
+
</tr>
|
25
|
+
</table>
|
26
|
+
|
27
|
+
## About
|
28
|
+
|
29
|
+
ActiveModelSerializers brings convention over configuration to your JSON generation.
|
30
|
+
|
31
|
+
ActiveModelSerializers works through two components: **serializers** and **adapters**.
|
32
|
+
|
33
|
+
Serializers describe _which_ attributes and relationships should be serialized.
|
34
|
+
|
35
|
+
Adapters describe _how_ attributes and relationships should be serialized.
|
36
|
+
|
37
|
+
SerializableResource co-ordinates the resource, Adapter and Serializer to produce the
|
38
|
+
resource serialization. The serialization has the `#as_json`, `#to_json` and `#serializable_hash`
|
39
|
+
methods used by the Rails JSON Renderer. (SerializableResource actually delegates
|
40
|
+
these methods to the adapter.)
|
41
|
+
|
42
|
+
By default ActiveModelSerializers will use the **Attributes Adapter** (no JSON root).
|
43
|
+
But we strongly advise you to use **JsonApi Adapter**, which
|
44
|
+
follows 1.0 of the format specified in [jsonapi.org/format](http://jsonapi.org/format).
|
45
|
+
Check how to change the adapter in the sections below.
|
46
|
+
|
47
|
+
`0.10.x` is **not** backward compatible with `0.9.x` nor `0.8.x`.
|
48
|
+
|
49
|
+
`0.10.x` is based on the `0.8.0` code, but with a more flexible
|
50
|
+
architecture. We'd love your help. [Learn how you can help here.](CONTRIBUTING.md)
|
51
|
+
|
52
|
+
## Installation
|
53
|
+
|
54
|
+
Add this line to your application's Gemfile:
|
55
|
+
|
56
|
+
```
|
57
|
+
gem 'active_model_serializers', '~> 0.10.0'
|
58
|
+
```
|
59
|
+
|
60
|
+
And then execute:
|
61
|
+
|
62
|
+
```
|
63
|
+
$ bundle
|
64
|
+
```
|
65
|
+
|
66
|
+
## Getting Started
|
67
|
+
|
68
|
+
See [Getting Started](docs/general/getting_started.md) for the nuts and bolts.
|
69
|
+
|
70
|
+
More information is available in the [Guides](docs) and
|
71
|
+
[High-level behavior](README.md#high-level-behavior).
|
72
|
+
|
73
|
+
## Getting Help
|
74
|
+
|
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).
|
77
|
+
|
78
|
+
If you have a question, please [post to Stack Overflow](http://stackoverflow.com/questions/tagged/active-model-serializers).
|
79
|
+
|
80
|
+
If you'd like to chat, we have a [community slack](http://amserializers.herokuapp.com).
|
81
|
+
|
82
|
+
Thanks!
|
83
|
+
|
84
|
+
## Documentation
|
85
|
+
|
86
|
+
If you're reading this at https://github.com/rails-api/active_model_serializers you are
|
87
|
+
reading documentation for our `master`, which may include features that have not
|
88
|
+
been released yet. Please see below for the documentation relevant to you.
|
89
|
+
|
90
|
+
- [0.10 (master) Documentation](https://github.com/rails-api/active_model_serializers/tree/master)
|
91
|
+
- [0.10.6 (latest release) Documentation](https://github.com/rails-api/active_model_serializers/tree/v0.10.6)
|
92
|
+
- [![API Docs](http://img.shields.io/badge/yard-docs-blue.svg)](http://www.rubydoc.info/gems/active_model_serializers/0.10.6)
|
93
|
+
- [Guides](docs)
|
94
|
+
- [0.9 (0-9-stable) Documentation](https://github.com/rails-api/active_model_serializers/tree/0-9-stable)
|
95
|
+
- [![API Docs](http://img.shields.io/badge/yard-docs-blue.svg)](http://www.rubydoc.info/github/rails-api/active_model_serializers/0-9-stable)
|
96
|
+
- [0.8 (0-8-stable) Documentation](https://github.com/rails-api/active_model_serializers/tree/0-8-stable)
|
97
|
+
- [![API Docs](http://img.shields.io/badge/yard-docs-blue.svg)](http://www.rubydoc.info/github/rails-api/active_model_serializers/0-8-stable)
|
98
|
+
|
99
|
+
|
100
|
+
## High-level behavior
|
101
|
+
|
102
|
+
Choose an adapter from [adapters](lib/active_model_serializers/adapter):
|
103
|
+
|
104
|
+
``` ruby
|
105
|
+
ActiveModelSerializers.config.adapter = :json_api # Default: `:attributes`
|
106
|
+
```
|
107
|
+
|
108
|
+
Given a [serializable model](lib/active_model/serializer/lint.rb):
|
109
|
+
|
110
|
+
```ruby
|
111
|
+
# either
|
112
|
+
class SomeResource < ActiveRecord::Base
|
113
|
+
# columns: title, body
|
114
|
+
end
|
115
|
+
# or
|
116
|
+
class SomeResource < ActiveModelSerializers::Model
|
117
|
+
attributes :title, :body
|
118
|
+
end
|
119
|
+
```
|
120
|
+
|
121
|
+
And initialized as:
|
122
|
+
|
123
|
+
```ruby
|
124
|
+
resource = SomeResource.new(title: 'ActiveModelSerializers', body: 'Convention over configuration')
|
125
|
+
```
|
126
|
+
|
127
|
+
Given a serializer for the serializable model:
|
128
|
+
|
129
|
+
```ruby
|
130
|
+
class SomeSerializer < ActiveModel::Serializer
|
131
|
+
attribute :title, key: :name
|
132
|
+
attributes :body
|
133
|
+
end
|
134
|
+
```
|
135
|
+
|
136
|
+
The model can be serialized as:
|
137
|
+
|
138
|
+
```ruby
|
139
|
+
options = {}
|
140
|
+
serialization = ActiveModelSerializers::SerializableResource.new(resource, options)
|
141
|
+
serialization.to_json
|
142
|
+
serialization.as_json
|
143
|
+
```
|
144
|
+
|
145
|
+
SerializableResource delegates to the adapter, which it builds as:
|
146
|
+
|
147
|
+
```ruby
|
148
|
+
adapter_options = {}
|
149
|
+
adapter = ActiveModelSerializers::Adapter.create(serializer, adapter_options)
|
150
|
+
adapter.to_json
|
151
|
+
adapter.as_json
|
152
|
+
adapter.serializable_hash
|
153
|
+
```
|
154
|
+
|
155
|
+
The adapter formats the serializer's attributes and associations (a.k.a. includes):
|
156
|
+
|
157
|
+
```ruby
|
158
|
+
serializer_options = {}
|
159
|
+
serializer = SomeSerializer.new(resource, serializer_options)
|
160
|
+
serializer.attributes
|
161
|
+
serializer.associations
|
162
|
+
```
|
163
|
+
|
164
|
+
## Architecture
|
165
|
+
|
166
|
+
This section focuses on architecture the 0.10.x version of ActiveModelSerializers. If you are interested in the architecture of the 0.8 or 0.9 versions,
|
167
|
+
please refer to the [0.8 README](https://github.com/rails-api/active_model_serializers/blob/0-8-stable/README.md) or
|
168
|
+
[0.9 README](https://github.com/rails-api/active_model_serializers/blob/0-9-stable/README.md).
|
169
|
+
|
170
|
+
The original design is also available [here](https://github.com/rails-api/active_model_serializers/blob/d72b66d4c5355b0ff0a75a04895fcc4ea5b0c65e/README.textile).
|
171
|
+
|
172
|
+
### ActiveModel::Serializer
|
173
|
+
|
174
|
+
An **`ActiveModel::Serializer`** wraps a [serializable resource](https://github.com/rails/rails/blob/4-2-stable/activemodel/lib/active_model/serialization.rb)
|
175
|
+
and exposes an `attributes` method, among a few others.
|
176
|
+
It allows you to specify which attributes and associations should be represented in the serializatation of the resource.
|
177
|
+
It requires an adapter to transform its attributes into a JSON document; it cannot be serialized itself.
|
178
|
+
It may be useful to think of it as a
|
179
|
+
[presenter](http://blog.steveklabnik.com/posts/2011-09-09-better-ruby-presenters).
|
180
|
+
|
181
|
+
#### ActiveModel::CollectionSerializer
|
182
|
+
|
183
|
+
The **`ActiveModel::CollectionSerializer`** represents a collection of resources as serializers
|
184
|
+
and, if there is no serializer, primitives.
|
185
|
+
|
186
|
+
### ActiveModelSerializers::Adapter::Base
|
187
|
+
|
188
|
+
The **`ActiveModelSerializers::Adapter::Base`** describes the structure of the JSON document generated from a
|
189
|
+
serializer. For example, the `Attributes` example represents each serializer as its
|
190
|
+
unmodified attributes. The `JsonApi` adapter represents the serializer as a [JSON
|
191
|
+
API](http://jsonapi.org/) document.
|
192
|
+
|
193
|
+
### ActiveModelSerializers::SerializableResource
|
194
|
+
|
195
|
+
The **`ActiveModelSerializers::SerializableResource`** acts to coordinate the serializer(s) and adapter
|
196
|
+
to an object that responds to `to_json`, and `as_json`. It is used in the controller to
|
197
|
+
encapsulate the serialization resource when rendered. However, it can also be used on its own
|
198
|
+
to serialize a resource outside of a controller, as well.
|
199
|
+
|
200
|
+
### Primitive handling
|
201
|
+
|
202
|
+
Definitions: A primitive is usually a String or Array. There is no serializer
|
203
|
+
defined for them; they will be serialized when the resource is converted to JSON (`as_json` or
|
204
|
+
`to_json`). (The below also applies for any object with no serializer.)
|
205
|
+
|
206
|
+
- ActiveModelSerializers doesn't handle primitives passed to `render json:` at all.
|
207
|
+
|
208
|
+
Internally, if no serializer can be found in the controller, the resource is not decorated by
|
209
|
+
ActiveModelSerializers.
|
210
|
+
|
211
|
+
- However, when a primitive value is an attribute or in a collection, it is not modified.
|
212
|
+
|
213
|
+
When serializing a collection and the collection serializer (CollectionSerializer) cannot
|
214
|
+
identify a serializer for a resource in its collection, it throws [`:no_serializer`](https://github.com/rails-api/active_model_serializers/issues/1191#issuecomment-142327128).
|
215
|
+
For example, when caught by `Reflection#build_association`, and the association value is set directly:
|
216
|
+
|
217
|
+
```ruby
|
218
|
+
reflection_options[:virtual_value] = association_value.try(:as_json) || association_value
|
219
|
+
```
|
220
|
+
|
221
|
+
(which is called by the adapter as `serializer.associations(*)`.)
|
222
|
+
|
223
|
+
### How options are parsed
|
224
|
+
|
225
|
+
High-level overview:
|
226
|
+
|
227
|
+
- For a **collection**
|
228
|
+
- `:serializer` specifies the collection serializer and
|
229
|
+
- `:each_serializer` specifies the serializer for each resource in the collection.
|
230
|
+
- For a **single resource**, the `:serializer` option is the resource serializer.
|
231
|
+
- Options are partitioned in serializer options and adapter options. Keys for adapter options are specified by
|
232
|
+
[`ADAPTER_OPTION_KEYS`](https://github.com/rails-api/active_model_serializers/blob/master/lib/active_model_serializers/serializable_resource.rb#L5).
|
233
|
+
The remaining options are serializer options.
|
234
|
+
|
235
|
+
Details:
|
236
|
+
|
237
|
+
1. **ActionController::Serialization**
|
238
|
+
1. `serializable_resource = ActiveModelSerializers::SerializableResource.new(resource, options)`
|
239
|
+
1. `options` are partitioned into `adapter_opts` and everything else (`serializer_opts`).
|
240
|
+
The `adapter_opts` keys are defined in [`ActiveModelSerializers::SerializableResource::ADAPTER_OPTION_KEYS`](lib/active_model_serializers/serializable_resource.rb#L5).
|
241
|
+
1. **ActiveModelSerializers::SerializableResource**
|
242
|
+
1. `if serializable_resource.serializer?` (there is a serializer for the resource, and an adapter is used.)
|
243
|
+
- Where `serializer?` is `use_adapter? && !!(serializer)`
|
244
|
+
- Where `use_adapter?`: 'True when no explicit adapter given, or explicit value is truthy (non-nil);
|
245
|
+
False when explicit adapter is falsy (nil or false)'
|
246
|
+
- Where `serializer`:
|
247
|
+
1. from explicit `:serializer` option, else
|
248
|
+
2. implicitly from resource `ActiveModel::Serializer.serializer_for(resource)`
|
249
|
+
1. A side-effect of checking `serializer` is:
|
250
|
+
- The `:serializer` option is removed from the serializer_opts hash
|
251
|
+
- If the `:each_serializer` option is present, it is removed from the serializer_opts hash and set as the `:serializer` option
|
252
|
+
1. The serializer and adapter are created as
|
253
|
+
1. `serializer_instance = serializer.new(resource, serializer_opts)`
|
254
|
+
2. `adapter_instance = ActiveModel::Serializer::Adapter.create(serializer_instance, adapter_opts)`
|
255
|
+
1. **ActiveModel::Serializer::CollectionSerializer#new**
|
256
|
+
1. If the `serializer_instance` was a `CollectionSerializer` and the `:serializer` serializer_opts
|
257
|
+
is present, then [that serializer is passed into each resource](https://github.com/rails-api/active_model_serializers/blob/a54d237e2828fe6bab1ea5dfe6360d4ecc8214cd/lib/active_model/serializer/array_serializer.rb#L14-L16).
|
258
|
+
1. **ActiveModel::Serializer#attributes** is used by the adapter to get the attributes for
|
259
|
+
resource as defined by the serializer.
|
260
|
+
|
261
|
+
(In Rails, the `options` are also passed to the `as_json(options)` or `to_json(options)`
|
262
|
+
methods on the resource serialization by the Rails JSON renderer. They are, therefore, important
|
263
|
+
to know about, but not part of ActiveModelSerializers.)
|
264
|
+
|
265
|
+
### What does a 'serializable resource' look like?
|
266
|
+
|
267
|
+
- An `ActiveRecord::Base` object.
|
268
|
+
- Any Ruby object that passes the
|
269
|
+
[Lint](https://www.rubydoc.info/gems/active_model_serializers/ActiveModel/Serializer/Lint/Tests)
|
270
|
+
[(code)](lib/active_model/serializer/lint.rb).
|
271
|
+
|
272
|
+
ActiveModelSerializers provides a
|
273
|
+
[`ActiveModelSerializers::Model`](https://github.com/rails-api/active_model_serializers/blob/master/lib/active_model_serializers/model.rb),
|
274
|
+
which is a simple serializable PORO (Plain-Old Ruby Object).
|
275
|
+
|
276
|
+
`ActiveModelSerializers::Model` may be used either as a reference implementation, or in production code.
|
277
|
+
|
278
|
+
```ruby
|
279
|
+
class MyModel < ActiveModelSerializers::Model
|
280
|
+
attributes :id, :name, :level
|
281
|
+
end
|
282
|
+
```
|
283
|
+
|
284
|
+
The default serializer for `MyModel` would be `MyModelSerializer` whether MyModel is an
|
285
|
+
ActiveRecord::Base object or not.
|
286
|
+
|
287
|
+
Outside of the controller the rules are **exactly** the same as for records. For example:
|
288
|
+
|
289
|
+
```ruby
|
290
|
+
render json: MyModel.new(level: 'awesome'), adapter: :json
|
291
|
+
```
|
292
|
+
|
293
|
+
would be serialized the same as
|
294
|
+
|
295
|
+
```ruby
|
296
|
+
ActiveModelSerializers::SerializableResource.new(MyModel.new(level: 'awesome'), adapter: :json).as_json
|
297
|
+
```
|
298
|
+
|
299
|
+
## Semantic Versioning
|
300
|
+
|
301
|
+
This project adheres to [semver](http://semver.org/)
|
302
|
+
|
303
|
+
## Contributing
|
304
|
+
|
305
|
+
See [CONTRIBUTING.md](CONTRIBUTING.md)
|