active_model_serializers 0.10.3 → 0.10.12
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/CHANGELOG.md +154 -2
- data/README.md +153 -15
- data/lib/action_controller/serialization.rb +11 -1
- data/lib/active_model/serializable_resource.rb +2 -0
- data/lib/active_model/serializer.rb +275 -81
- data/lib/active_model/serializer/adapter.rb +2 -0
- data/lib/active_model/serializer/adapter/attributes.rb +2 -0
- data/lib/active_model/serializer/adapter/base.rb +2 -0
- data/lib/active_model/serializer/adapter/json.rb +2 -0
- data/lib/active_model/serializer/adapter/json_api.rb +2 -0
- data/lib/active_model/serializer/adapter/null.rb +2 -0
- data/lib/active_model/serializer/array_serializer.rb +2 -0
- data/lib/active_model/serializer/association.rb +53 -14
- data/lib/active_model/serializer/attribute.rb +2 -0
- data/lib/active_model/serializer/belongs_to_reflection.rb +7 -1
- data/lib/active_model/serializer/collection_serializer.rb +8 -5
- data/lib/active_model/serializer/concerns/caching.rb +36 -23
- data/lib/active_model/serializer/error_serializer.rb +2 -0
- data/lib/active_model/serializer/errors_serializer.rb +2 -0
- data/lib/active_model/serializer/field.rb +2 -0
- data/lib/active_model/serializer/fieldset.rb +3 -1
- data/lib/active_model/serializer/has_many_reflection.rb +6 -1
- data/lib/active_model/serializer/has_one_reflection.rb +3 -1
- 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 +2 -0
- data/lib/active_model/serializer/null.rb +2 -0
- data/lib/active_model/serializer/reflection.rb +122 -73
- data/lib/active_model/serializer/version.rb +3 -1
- data/lib/active_model_serializers.rb +29 -11
- data/lib/active_model_serializers/adapter.rb +3 -1
- data/lib/active_model_serializers/adapter/attributes.rb +23 -0
- data/lib/active_model_serializers/adapter/base.rb +4 -2
- data/lib/active_model_serializers/adapter/json.rb +2 -0
- data/lib/active_model_serializers/adapter/json_api.rb +44 -26
- data/lib/active_model_serializers/adapter/json_api/deserialization.rb +4 -2
- data/lib/active_model_serializers/adapter/json_api/error.rb +2 -0
- data/lib/active_model_serializers/adapter/json_api/jsonapi.rb +2 -0
- data/lib/active_model_serializers/adapter/json_api/link.rb +2 -0
- data/lib/active_model_serializers/adapter/json_api/meta.rb +2 -0
- data/lib/active_model_serializers/adapter/json_api/pagination_links.rb +42 -21
- data/lib/active_model_serializers/adapter/json_api/relationship.rb +52 -9
- data/lib/active_model_serializers/adapter/json_api/resource_identifier.rb +35 -18
- data/lib/active_model_serializers/adapter/null.rb +2 -0
- data/lib/active_model_serializers/callbacks.rb +2 -0
- data/lib/active_model_serializers/deprecate.rb +2 -0
- data/lib/active_model_serializers/deserialization.rb +2 -0
- data/lib/active_model_serializers/json_pointer.rb +2 -0
- data/lib/active_model_serializers/logging.rb +2 -0
- data/lib/active_model_serializers/lookup_chain.rb +2 -0
- data/lib/active_model_serializers/model.rb +111 -30
- data/lib/active_model_serializers/model/caching.rb +25 -0
- data/lib/active_model_serializers/railtie.rb +4 -0
- data/lib/active_model_serializers/register_jsonapi_renderer.rb +2 -0
- data/lib/active_model_serializers/serializable_resource.rb +4 -2
- data/lib/active_model_serializers/serialization_context.rb +2 -0
- data/lib/active_model_serializers/test.rb +2 -0
- data/lib/active_model_serializers/test/schema.rb +4 -2
- data/lib/active_model_serializers/test/serializer.rb +2 -0
- data/lib/generators/rails/resource_override.rb +3 -1
- data/lib/generators/rails/serializer_generator.rb +2 -0
- data/lib/grape/active_model_serializers.rb +2 -0
- data/lib/grape/formatters/active_model_serializers.rb +2 -0
- data/lib/grape/helpers/active_model_serializers.rb +2 -0
- data/lib/tasks/rubocop.rake +55 -0
- metadata +74 -291
- data/.github/ISSUE_TEMPLATE.md +0 -29
- data/.github/PULL_REQUEST_TEMPLATE.md +0 -15
- data/.gitignore +0 -35
- data/.rubocop.yml +0 -102
- data/.simplecov +0 -110
- data/.travis.yml +0 -51
- data/CODE_OF_CONDUCT.md +0 -74
- data/CONTRIBUTING.md +0 -105
- data/Gemfile +0 -56
- data/Rakefile +0 -103
- data/active_model_serializers.gemspec +0 -62
- data/appveyor.yml +0 -24
- data/bin/bench +0 -171
- data/bin/bench_regression +0 -316
- data/bin/serve_benchmark +0 -39
- data/docs/ARCHITECTURE.md +0 -125
- data/docs/README.md +0 -42
- data/docs/STYLE.md +0 -58
- data/docs/general/adapters.md +0 -247
- data/docs/general/caching.md +0 -58
- data/docs/general/configuration_options.md +0 -169
- data/docs/general/deserialization.md +0 -100
- data/docs/general/fields.md +0 -31
- data/docs/general/getting_started.md +0 -133
- data/docs/general/instrumentation.md +0 -40
- data/docs/general/key_transforms.md +0 -40
- data/docs/general/logging.md +0 -14
- data/docs/general/rendering.md +0 -294
- data/docs/general/serializers.md +0 -461
- data/docs/how-open-source-maintained.jpg +0 -0
- data/docs/howto/add_pagination_links.md +0 -138
- data/docs/howto/add_relationship_links.md +0 -137
- data/docs/howto/add_root_key.md +0 -55
- data/docs/howto/grape_integration.md +0 -42
- data/docs/howto/outside_controller_use.md +0 -65
- data/docs/howto/passing_arbitrary_options.md +0 -27
- data/docs/howto/serialize_poro.md +0 -32
- data/docs/howto/test.md +0 -154
- data/docs/howto/upgrade_from_0_8_to_0_10.md +0 -265
- data/docs/integrations/ember-and-json-api.md +0 -144
- data/docs/integrations/grape.md +0 -19
- data/docs/jsonapi/errors.md +0 -56
- data/docs/jsonapi/schema.md +0 -151
- data/docs/jsonapi/schema/schema.json +0 -366
- data/docs/rfcs/0000-namespace.md +0 -106
- data/docs/rfcs/template.md +0 -15
- data/lib/active_model/serializer/collection_reflection.rb +0 -7
- data/lib/active_model/serializer/concerns/associations.rb +0 -102
- data/lib/active_model/serializer/concerns/attributes.rb +0 -82
- data/lib/active_model/serializer/concerns/configuration.rb +0 -59
- data/lib/active_model/serializer/concerns/links.rb +0 -35
- data/lib/active_model/serializer/concerns/meta.rb +0 -29
- data/lib/active_model/serializer/concerns/type.rb +0 -25
- data/lib/active_model/serializer/singular_reflection.rb +0 -7
- data/lib/active_model_serializers/key_transform.rb +0 -74
- data/test/action_controller/adapter_selector_test.rb +0 -53
- data/test/action_controller/explicit_serializer_test.rb +0 -135
- data/test/action_controller/json/include_test.rb +0 -246
- data/test/action_controller/json_api/deserialization_test.rb +0 -112
- data/test/action_controller/json_api/errors_test.rb +0 -40
- data/test/action_controller/json_api/fields_test.rb +0 -57
- data/test/action_controller/json_api/linked_test.rb +0 -202
- data/test/action_controller/json_api/pagination_test.rb +0 -116
- data/test/action_controller/json_api/transform_test.rb +0 -181
- data/test/action_controller/lookup_proc_test.rb +0 -49
- data/test/action_controller/namespace_lookup_test.rb +0 -226
- data/test/action_controller/serialization_scope_name_test.rb +0 -229
- data/test/action_controller/serialization_test.rb +0 -472
- data/test/active_model_serializers/adapter_for_test.rb +0 -208
- data/test/active_model_serializers/json_pointer_test.rb +0 -22
- data/test/active_model_serializers/key_transform_test.rb +0 -297
- data/test/active_model_serializers/logging_test.rb +0 -77
- data/test/active_model_serializers/model_test.rb +0 -22
- data/test/active_model_serializers/railtie_test_isolated.rb +0 -63
- data/test/active_model_serializers/register_jsonapi_renderer_test_isolated.rb +0 -143
- data/test/active_model_serializers/serialization_context_test_isolated.rb +0 -71
- data/test/active_model_serializers/test/schema_test.rb +0 -130
- data/test/active_model_serializers/test/serializer_test.rb +0 -62
- data/test/active_record_test.rb +0 -9
- data/test/adapter/attributes_test.rb +0 -43
- data/test/adapter/deprecation_test.rb +0 -100
- data/test/adapter/json/belongs_to_test.rb +0 -45
- data/test/adapter/json/collection_test.rb +0 -104
- data/test/adapter/json/has_many_test.rb +0 -45
- data/test/adapter/json/transform_test.rb +0 -93
- data/test/adapter/json_api/belongs_to_test.rb +0 -155
- data/test/adapter/json_api/collection_test.rb +0 -96
- data/test/adapter/json_api/errors_test.rb +0 -76
- data/test/adapter/json_api/fields_test.rb +0 -88
- data/test/adapter/json_api/has_many_embed_ids_test.rb +0 -43
- data/test/adapter/json_api/has_many_explicit_serializer_test.rb +0 -96
- data/test/adapter/json_api/has_many_test.rb +0 -165
- data/test/adapter/json_api/has_one_test.rb +0 -80
- data/test/adapter/json_api/include_data_if_sideloaded_test.rb +0 -166
- data/test/adapter/json_api/json_api_test.rb +0 -33
- data/test/adapter/json_api/linked_test.rb +0 -413
- data/test/adapter/json_api/links_test.rb +0 -95
- data/test/adapter/json_api/pagination_links_test.rb +0 -193
- data/test/adapter/json_api/parse_test.rb +0 -137
- data/test/adapter/json_api/relationship_test.rb +0 -397
- data/test/adapter/json_api/resource_identifier_test.rb +0 -110
- data/test/adapter/json_api/resource_meta_test.rb +0 -100
- data/test/adapter/json_api/toplevel_jsonapi_test.rb +0 -82
- data/test/adapter/json_api/transform_test.rb +0 -504
- data/test/adapter/json_api/type_test.rb +0 -61
- data/test/adapter/json_test.rb +0 -46
- data/test/adapter/null_test.rb +0 -22
- data/test/adapter/polymorphic_test.rb +0 -171
- data/test/adapter_test.rb +0 -67
- data/test/array_serializer_test.rb +0 -22
- data/test/benchmark/app.rb +0 -65
- data/test/benchmark/benchmarking_support.rb +0 -67
- data/test/benchmark/bm_active_record.rb +0 -81
- data/test/benchmark/bm_adapter.rb +0 -38
- data/test/benchmark/bm_caching.rb +0 -119
- data/test/benchmark/bm_lookup_chain.rb +0 -83
- data/test/benchmark/bm_transform.rb +0 -45
- data/test/benchmark/config.ru +0 -3
- data/test/benchmark/controllers.rb +0 -83
- data/test/benchmark/fixtures.rb +0 -219
- data/test/cache_test.rb +0 -579
- data/test/collection_serializer_test.rb +0 -110
- data/test/fixtures/active_record.rb +0 -78
- data/test/fixtures/poro.rb +0 -286
- data/test/generators/scaffold_controller_generator_test.rb +0 -24
- data/test/generators/serializer_generator_test.rb +0 -74
- data/test/grape_test.rb +0 -178
- data/test/lint_test.rb +0 -49
- data/test/logger_test.rb +0 -20
- data/test/poro_test.rb +0 -9
- data/test/serializable_resource_test.rb +0 -79
- data/test/serializers/association_macros_test.rb +0 -37
- data/test/serializers/associations_test.rb +0 -370
- data/test/serializers/attribute_test.rb +0 -151
- data/test/serializers/attributes_test.rb +0 -52
- data/test/serializers/caching_configuration_test_isolated.rb +0 -170
- data/test/serializers/configuration_test.rb +0 -32
- data/test/serializers/fieldset_test.rb +0 -14
- data/test/serializers/meta_test.rb +0 -202
- data/test/serializers/options_test.rb +0 -21
- data/test/serializers/read_attribute_for_serialization_test.rb +0 -79
- data/test/serializers/root_test.rb +0 -21
- data/test/serializers/serialization_test.rb +0 -55
- data/test/serializers/serializer_for_test.rb +0 -136
- data/test/serializers/serializer_for_with_namespace_test.rb +0 -87
- data/test/support/custom_schemas/active_model_serializers/test/schema_test/my/index.json +0 -6
- data/test/support/isolated_unit.rb +0 -82
- data/test/support/rails5_shims.rb +0 -53
- data/test/support/rails_app.rb +0 -36
- data/test/support/schemas/active_model_serializers/test/schema_test/my/index.json +0 -6
- data/test/support/schemas/active_model_serializers/test/schema_test/my/show.json +0 -6
- data/test/support/schemas/custom/show.json +0 -7
- data/test/support/schemas/hyper_schema.json +0 -93
- data/test/support/schemas/render_using_json_api.json +0 -43
- data/test/support/schemas/simple_json_pointers.json +0 -10
- data/test/support/serialization_testing.rb +0 -71
- data/test/test_helper.rb +0 -58
data/docs/rfcs/0000-namespace.md
DELETED
@@ -1,106 +0,0 @@
|
|
1
|
-
- Start Date: (2015-10-29)
|
2
|
-
- RFC PR: https://github.com/rails-api/active_model_serializers/pull/1310
|
3
|
-
- ActiveModelSerializers Issue: https://github.com/rails-api/active_model_serializers/issues/1298
|
4
|
-
|
5
|
-
# Summary
|
6
|
-
|
7
|
-
Provide a consistent API for the user of the AMS.
|
8
|
-
|
9
|
-
# Motivation
|
10
|
-
|
11
|
-
The actual public API is defined under `ActiveModelSerializers`,
|
12
|
-
`ActiveModel::Serializer` and `ActiveModel`.
|
13
|
-
|
14
|
-
At the `ActiveModel::Serializer` we have:
|
15
|
-
|
16
|
-
- `ActiveModel::Serializer.config`
|
17
|
-
- `ActiveModel::Serializer`
|
18
|
-
|
19
|
-
At the `ActiveModelSerializers` we have:
|
20
|
-
|
21
|
-
- `ActiveModelSerializers::Model`
|
22
|
-
- `ActiveModelSerializers.logger`
|
23
|
-
|
24
|
-
At `ActiveModel` we have:
|
25
|
-
|
26
|
-
- `ActiveModel::SerializableResource`
|
27
|
-
|
28
|
-
The idea here is to provide a single namespace `ActiveModelSerializers` to the user.
|
29
|
-
Following the same idea we have on other gems like
|
30
|
-
[Devise](https://github.com/plataformatec/devise/blob/e9c82472ffe7c43a448945f77e034a0e47dde0bb/lib/devise.rb),
|
31
|
-
[Refile](https://github.com/refile/refile/blob/6b24c293d044862dafbf1bfa4606672a64903aa2/lib/refile.rb) and
|
32
|
-
[Active Job](https://github.com/rails/rails/blob/30bacc26f8f258b39e12f63fe52389a968d9c1ea/activejob/lib/active_job.rb)
|
33
|
-
for example.
|
34
|
-
|
35
|
-
This way we are clarifing the boundaries of
|
36
|
-
[ActiveModelSerializers and Rails](https://github.com/rails-api/active_model_serializers/blob/master/CHANGELOG.md#prehistory)
|
37
|
-
and make clear that the `ActiveModel::Serializer` class is no longer the primary
|
38
|
-
behavior of the ActiveModelSerializers.
|
39
|
-
|
40
|
-
# Detailed design
|
41
|
-
|
42
|
-
## New classes and modules organization
|
43
|
-
|
44
|
-
Since this will be a big change we can do this on baby steps, read small pull requests. A
|
45
|
-
possible approach is:
|
46
|
-
|
47
|
-
- All new code will be in `lib/active_model_serializers/` using
|
48
|
-
the module namespace `ActiveModelSerializers`.
|
49
|
-
- Move all content under `ActiveModel::Serializer` to be under
|
50
|
-
`ActiveModelSerializers`, the adapter is on this steps;
|
51
|
-
- Move all content under `ActiveModel` to be under `ActiveModelSerializers`,
|
52
|
-
the `SerializableResource` is on this step;
|
53
|
-
- Change all public API that doesn't make sense, keeping in mind only to keep
|
54
|
-
this in the same namespace
|
55
|
-
- Update the README;
|
56
|
-
- Update the docs;
|
57
|
-
|
58
|
-
The following table represents the current and the desired classes and modules
|
59
|
-
at the first moment.
|
60
|
-
|
61
|
-
| Current | Desired | Notes |
|
62
|
-
|--------------------------------------------------------|--------------------------------------------------|--------------------|
|
63
|
-
| `ActiveModelSerializers` and `ActiveModel::Serializer` | `ActiveModelSerializers` | The main namespace |
|
64
|
-
| `ActiveModelSerializers.logger` | `ActiveModelSerializers.logger` ||
|
65
|
-
| `ActiveModelSerializers::Model` | `ActiveModelSerializers::Model` ||
|
66
|
-
| `ActiveModel::SerializableResource` | `ActiveModelSerializers::SerializableResource` ||
|
67
|
-
| `ActiveModel::Serializer` | `ActiveModelSerializers::Serializer` | The name can be discussed in a future pull request. For example, we can rename this to `Resource` [following this idea](https://github.com/rails-api/active_model_serializers/pull/1301/files#r42963185) more info about naming in the next section|
|
68
|
-
| `ActiveModel::Serializer.config` | `ActiveModelSerializers.config` ||
|
69
|
-
|
70
|
-
## Renaming of class and modules
|
71
|
-
|
72
|
-
When moving some content to the new namespace we can find some names that does
|
73
|
-
not make much sense like `ActiveModel::Serializer::Adapter::JsonApi`.
|
74
|
-
Discussion of renaming existing classes / modules and JsonApi objects will
|
75
|
-
happen in separate pull requests, and issues, and in the google doc
|
76
|
-
https://docs.google.com/document/d/1rcrJr0sVcazY2Opd_6Kmv1iIwuHbI84s1P_NzFn-05c/edit?usp=sharing
|
77
|
-
|
78
|
-
Some of names already have a definition.
|
79
|
-
|
80
|
-
- Adapters get their own namespace under ActiveModelSerializers. E.g
|
81
|
-
`ActiveModelSerializers::Adapter`
|
82
|
-
- Serializers get their own namespace under ActiveModelSerializers. E.g
|
83
|
-
`ActiveModelSerializers::Serializer`
|
84
|
-
|
85
|
-
## Keeping compatibility
|
86
|
-
|
87
|
-
All moved classes or modules be aliased to their old name and location with
|
88
|
-
deprecation warnings, such as
|
89
|
-
[was done for CollectionSerializer](https://github.com/rails-api/active_model_serializers/pull/1251).
|
90
|
-
|
91
|
-
# Drawbacks
|
92
|
-
|
93
|
-
This will be a breaking change, so all users serializers will be broken after a
|
94
|
-
major bump.
|
95
|
-
All pull requests will need to rebase since the architeture will change a lot.
|
96
|
-
|
97
|
-
# Alternatives
|
98
|
-
|
99
|
-
We can keep the way it is, and keep in mind to not add another namespace as a
|
100
|
-
public API.
|
101
|
-
|
102
|
-
# Unresolved questions
|
103
|
-
|
104
|
-
What is the better class name to be used to the class that will be inherited at
|
105
|
-
the creation of a serializer. This can be discussed in other RFC or directly via
|
106
|
-
pull request.
|
data/docs/rfcs/template.md
DELETED
@@ -1,15 +0,0 @@
|
|
1
|
-
- Start Date: (YYYY-MM-DD)
|
2
|
-
- RFC PR: https://github.com/rails-api/active_model_serializers/pull/dddd
|
3
|
-
- ActiveModelSerializers Issue: https://github.com/rails-api/active_model_serializers/issues/dddd
|
4
|
-
|
5
|
-
# Summary
|
6
|
-
|
7
|
-
# Motivation
|
8
|
-
|
9
|
-
# Detailed design
|
10
|
-
|
11
|
-
# Drawbacks
|
12
|
-
|
13
|
-
# Alternatives
|
14
|
-
|
15
|
-
# Unresolved questions
|
@@ -1,102 +0,0 @@
|
|
1
|
-
module ActiveModel
|
2
|
-
class Serializer
|
3
|
-
# Defines an association in the object should be rendered.
|
4
|
-
#
|
5
|
-
# The serializer object should implement the association name
|
6
|
-
# as a method which should return an array when invoked. If a method
|
7
|
-
# with the association name does not exist, the association name is
|
8
|
-
# dispatched to the serialized object.
|
9
|
-
#
|
10
|
-
module Associations
|
11
|
-
extend ActiveSupport::Concern
|
12
|
-
|
13
|
-
included do
|
14
|
-
with_options instance_writer: false, instance_reader: true do |serializer|
|
15
|
-
serializer.class_attribute :_reflections
|
16
|
-
self._reflections ||= {}
|
17
|
-
end
|
18
|
-
|
19
|
-
extend ActiveSupport::Autoload
|
20
|
-
autoload :Association
|
21
|
-
autoload :Reflection
|
22
|
-
autoload :SingularReflection
|
23
|
-
autoload :CollectionReflection
|
24
|
-
autoload :BelongsToReflection
|
25
|
-
autoload :HasOneReflection
|
26
|
-
autoload :HasManyReflection
|
27
|
-
end
|
28
|
-
|
29
|
-
module ClassMethods
|
30
|
-
def inherited(base)
|
31
|
-
super
|
32
|
-
base._reflections = _reflections.dup
|
33
|
-
end
|
34
|
-
|
35
|
-
# @param [Symbol] name of the association
|
36
|
-
# @param [Hash<Symbol => any>] options for the reflection
|
37
|
-
# @return [void]
|
38
|
-
#
|
39
|
-
# @example
|
40
|
-
# has_many :comments, serializer: CommentSummarySerializer
|
41
|
-
#
|
42
|
-
def has_many(name, options = {}, &block) # rubocop:disable Style/PredicateName
|
43
|
-
associate(HasManyReflection.new(name, options, block))
|
44
|
-
end
|
45
|
-
|
46
|
-
# @param [Symbol] name of the association
|
47
|
-
# @param [Hash<Symbol => any>] options for the reflection
|
48
|
-
# @return [void]
|
49
|
-
#
|
50
|
-
# @example
|
51
|
-
# belongs_to :author, serializer: AuthorSerializer
|
52
|
-
#
|
53
|
-
def belongs_to(name, options = {}, &block)
|
54
|
-
associate(BelongsToReflection.new(name, options, block))
|
55
|
-
end
|
56
|
-
|
57
|
-
# @param [Symbol] name of the association
|
58
|
-
# @param [Hash<Symbol => any>] options for the reflection
|
59
|
-
# @return [void]
|
60
|
-
#
|
61
|
-
# @example
|
62
|
-
# has_one :author, serializer: AuthorSerializer
|
63
|
-
#
|
64
|
-
def has_one(name, options = {}, &block) # rubocop:disable Style/PredicateName
|
65
|
-
associate(HasOneReflection.new(name, options, block))
|
66
|
-
end
|
67
|
-
|
68
|
-
private
|
69
|
-
|
70
|
-
# Add reflection and define {name} accessor.
|
71
|
-
# @param [ActiveModel::Serializer::Reflection] reflection
|
72
|
-
# @return [void]
|
73
|
-
#
|
74
|
-
# @api private
|
75
|
-
#
|
76
|
-
def associate(reflection)
|
77
|
-
key = reflection.options[:key] || reflection.name
|
78
|
-
self._reflections[key] = reflection
|
79
|
-
end
|
80
|
-
end
|
81
|
-
|
82
|
-
# @param [JSONAPI::IncludeDirective] include_directive (defaults to the
|
83
|
-
# +default_include_directive+ config value when not provided)
|
84
|
-
# @return [Enumerator<Association>]
|
85
|
-
#
|
86
|
-
def associations(include_directive = ActiveModelSerializers.default_include_directive, include_slice = nil)
|
87
|
-
include_slice ||= include_directive
|
88
|
-
return unless object
|
89
|
-
|
90
|
-
Enumerator.new do |y|
|
91
|
-
self.class._reflections.values.each do |reflection|
|
92
|
-
next if reflection.excluded?(self)
|
93
|
-
key = reflection.options.fetch(:key, reflection.name)
|
94
|
-
next unless include_directive.key?(key)
|
95
|
-
|
96
|
-
y.yield reflection.build_association(self, instance_options, include_slice)
|
97
|
-
end
|
98
|
-
end
|
99
|
-
end
|
100
|
-
end
|
101
|
-
end
|
102
|
-
end
|
@@ -1,82 +0,0 @@
|
|
1
|
-
module ActiveModel
|
2
|
-
class Serializer
|
3
|
-
module Attributes
|
4
|
-
extend ActiveSupport::Concern
|
5
|
-
|
6
|
-
included do
|
7
|
-
with_options instance_writer: false, instance_reader: false do |serializer|
|
8
|
-
serializer.class_attribute :_attributes_data # @api private
|
9
|
-
self._attributes_data ||= {}
|
10
|
-
end
|
11
|
-
|
12
|
-
extend ActiveSupport::Autoload
|
13
|
-
autoload :Attribute
|
14
|
-
|
15
|
-
# Return the +attributes+ of +object+ as presented
|
16
|
-
# by the serializer.
|
17
|
-
def attributes(requested_attrs = nil, reload = false)
|
18
|
-
@attributes = nil if reload
|
19
|
-
@attributes ||= self.class._attributes_data.each_with_object({}) do |(key, attr), hash|
|
20
|
-
next if attr.excluded?(self)
|
21
|
-
next unless requested_attrs.nil? || requested_attrs.include?(key)
|
22
|
-
hash[key] = attr.value(self)
|
23
|
-
end
|
24
|
-
end
|
25
|
-
end
|
26
|
-
|
27
|
-
module ClassMethods
|
28
|
-
def inherited(base)
|
29
|
-
super
|
30
|
-
base._attributes_data = _attributes_data.dup
|
31
|
-
end
|
32
|
-
|
33
|
-
# @example
|
34
|
-
# class AdminAuthorSerializer < ActiveModel::Serializer
|
35
|
-
# attributes :id, :name, :recent_edits
|
36
|
-
def attributes(*attrs)
|
37
|
-
attrs = attrs.first if attrs.first.class == Array
|
38
|
-
|
39
|
-
attrs.each do |attr|
|
40
|
-
attribute(attr)
|
41
|
-
end
|
42
|
-
end
|
43
|
-
|
44
|
-
# @example
|
45
|
-
# class AdminAuthorSerializer < ActiveModel::Serializer
|
46
|
-
# attributes :id, :recent_edits
|
47
|
-
# attribute :name, key: :title
|
48
|
-
#
|
49
|
-
# attribute :full_name do
|
50
|
-
# "#{object.first_name} #{object.last_name}"
|
51
|
-
# end
|
52
|
-
#
|
53
|
-
# def recent_edits
|
54
|
-
# object.edits.last(5)
|
55
|
-
# end
|
56
|
-
def attribute(attr, options = {}, &block)
|
57
|
-
key = options.fetch(:key, attr)
|
58
|
-
_attributes_data[key] = Attribute.new(attr, options, block)
|
59
|
-
end
|
60
|
-
|
61
|
-
# @api private
|
62
|
-
# keys of attributes
|
63
|
-
# @see Serializer::attribute
|
64
|
-
def _attributes
|
65
|
-
_attributes_data.keys
|
66
|
-
end
|
67
|
-
|
68
|
-
# @api private
|
69
|
-
# maps attribute value to explict key name
|
70
|
-
# @see Serializer::attribute
|
71
|
-
# @see FragmentCache#fragment_serializer
|
72
|
-
def _attributes_keys
|
73
|
-
_attributes_data
|
74
|
-
.each_with_object({}) do |(key, attr), hash|
|
75
|
-
next if key == attr.name
|
76
|
-
hash[attr.name] = { key: key }
|
77
|
-
end
|
78
|
-
end
|
79
|
-
end
|
80
|
-
end
|
81
|
-
end
|
82
|
-
end
|
@@ -1,59 +0,0 @@
|
|
1
|
-
module ActiveModel
|
2
|
-
class Serializer
|
3
|
-
module Configuration
|
4
|
-
include ActiveSupport::Configurable
|
5
|
-
extend ActiveSupport::Concern
|
6
|
-
|
7
|
-
# Configuration options may also be set in
|
8
|
-
# Serializers and Adapters
|
9
|
-
included do |base|
|
10
|
-
config = base.config
|
11
|
-
config.collection_serializer = ActiveModel::Serializer::CollectionSerializer
|
12
|
-
config.serializer_lookup_enabled = true
|
13
|
-
|
14
|
-
def config.array_serializer=(collection_serializer)
|
15
|
-
self.collection_serializer = collection_serializer
|
16
|
-
end
|
17
|
-
|
18
|
-
def config.array_serializer
|
19
|
-
collection_serializer
|
20
|
-
end
|
21
|
-
|
22
|
-
config.default_includes = '*'
|
23
|
-
config.adapter = :attributes
|
24
|
-
config.key_transform = nil
|
25
|
-
config.jsonapi_pagination_links_enabled = true
|
26
|
-
config.jsonapi_resource_type = :plural
|
27
|
-
config.jsonapi_namespace_separator = '-'.freeze
|
28
|
-
config.jsonapi_version = '1.0'
|
29
|
-
config.jsonapi_toplevel_meta = {}
|
30
|
-
# Make JSON API top-level jsonapi member opt-in
|
31
|
-
# ref: http://jsonapi.org/format/#document-top-level
|
32
|
-
config.jsonapi_include_toplevel_object = false
|
33
|
-
config.include_data_default = true
|
34
|
-
|
35
|
-
# For configuring how serializers are found.
|
36
|
-
# This should be an array of procs.
|
37
|
-
#
|
38
|
-
# The priority of the output is that the first item
|
39
|
-
# in the evaluated result array will take precedence
|
40
|
-
# over other possible serializer paths.
|
41
|
-
#
|
42
|
-
# i.e.: First match wins.
|
43
|
-
#
|
44
|
-
# @example output
|
45
|
-
# => [
|
46
|
-
# "CustomNamespace::ResourceSerializer",
|
47
|
-
# "ParentSerializer::ResourceSerializer",
|
48
|
-
# "ResourceNamespace::ResourceSerializer" ,
|
49
|
-
# "ResourceSerializer"]
|
50
|
-
#
|
51
|
-
# If CustomNamespace::ResourceSerializer exists, it will be used
|
52
|
-
# for serialization
|
53
|
-
config.serializer_lookup_chain = ActiveModelSerializers::LookupChain::DEFAULT.dup
|
54
|
-
|
55
|
-
config.schema_path = 'test/support/schemas'
|
56
|
-
end
|
57
|
-
end
|
58
|
-
end
|
59
|
-
end
|
@@ -1,35 +0,0 @@
|
|
1
|
-
module ActiveModel
|
2
|
-
class Serializer
|
3
|
-
module Links
|
4
|
-
extend ActiveSupport::Concern
|
5
|
-
|
6
|
-
included do
|
7
|
-
with_options instance_writer: false, instance_reader: true do |serializer|
|
8
|
-
serializer.class_attribute :_links # @api private
|
9
|
-
self._links ||= {}
|
10
|
-
end
|
11
|
-
|
12
|
-
extend ActiveSupport::Autoload
|
13
|
-
end
|
14
|
-
|
15
|
-
module ClassMethods
|
16
|
-
def inherited(base)
|
17
|
-
super
|
18
|
-
base._links = _links.dup
|
19
|
-
end
|
20
|
-
|
21
|
-
# Define a link on a serializer.
|
22
|
-
# @example
|
23
|
-
# link(:self) { resource_url(object) }
|
24
|
-
# @example
|
25
|
-
# link(:self) { "http://example.com/resource/#{object.id}" }
|
26
|
-
# @example
|
27
|
-
# link :resource, "http://example.com/resource"
|
28
|
-
#
|
29
|
-
def link(name, value = nil, &block)
|
30
|
-
_links[name] = block || value
|
31
|
-
end
|
32
|
-
end
|
33
|
-
end
|
34
|
-
end
|
35
|
-
end
|
@@ -1,29 +0,0 @@
|
|
1
|
-
module ActiveModel
|
2
|
-
class Serializer
|
3
|
-
module Meta
|
4
|
-
extend ActiveSupport::Concern
|
5
|
-
|
6
|
-
included do
|
7
|
-
with_options instance_writer: false, instance_reader: true do |serializer|
|
8
|
-
serializer.class_attribute :_meta # @api private
|
9
|
-
end
|
10
|
-
|
11
|
-
extend ActiveSupport::Autoload
|
12
|
-
end
|
13
|
-
|
14
|
-
module ClassMethods
|
15
|
-
# Set the JSON API meta attribute of a serializer.
|
16
|
-
# @example
|
17
|
-
# class AdminAuthorSerializer < ActiveModel::Serializer
|
18
|
-
# meta { stuff: 'value' }
|
19
|
-
# @example
|
20
|
-
# meta do
|
21
|
-
# { comment_count: object.comments.count }
|
22
|
-
# end
|
23
|
-
def meta(value = nil, &block)
|
24
|
-
self._meta = block || value
|
25
|
-
end
|
26
|
-
end
|
27
|
-
end
|
28
|
-
end
|
29
|
-
end
|