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
@@ -0,0 +1,39 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ActiveModelSerializers
|
4
|
+
module Adapter
|
5
|
+
class JsonApi
|
6
|
+
# meta
|
7
|
+
# definition:
|
8
|
+
# JSON Object
|
9
|
+
#
|
10
|
+
# description:
|
11
|
+
# Non-standard meta-information that can not be represented as an attribute or relationship.
|
12
|
+
# structure:
|
13
|
+
# {
|
14
|
+
# attitude: 'adjustable'
|
15
|
+
# }
|
16
|
+
class Meta
|
17
|
+
def initialize(serializer)
|
18
|
+
@object = serializer.object
|
19
|
+
@scope = serializer.scope
|
20
|
+
|
21
|
+
# Use the return value of the block unless it is nil.
|
22
|
+
if serializer._meta.respond_to?(:call)
|
23
|
+
@value = instance_eval(&serializer._meta)
|
24
|
+
else
|
25
|
+
@value = serializer._meta
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
def as_json
|
30
|
+
@value
|
31
|
+
end
|
32
|
+
|
33
|
+
protected
|
34
|
+
|
35
|
+
attr_reader :object, :scope
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
@@ -0,0 +1,90 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ActiveModelSerializers
|
4
|
+
module Adapter
|
5
|
+
class JsonApi < Base
|
6
|
+
class PaginationLinks
|
7
|
+
MissingSerializationContextError = Class.new(KeyError)
|
8
|
+
FIRST_PAGE = 1
|
9
|
+
|
10
|
+
attr_reader :collection, :context
|
11
|
+
|
12
|
+
def initialize(collection, adapter_options)
|
13
|
+
@collection = collection
|
14
|
+
@adapter_options = adapter_options
|
15
|
+
@context = adapter_options.fetch(:serialization_context) do
|
16
|
+
fail MissingSerializationContextError, <<-EOF.freeze
|
17
|
+
JsonApi::PaginationLinks requires a ActiveModelSerializers::SerializationContext.
|
18
|
+
Please pass a ':serialization_context' option or
|
19
|
+
override CollectionSerializer#paginated? to return 'false'.
|
20
|
+
EOF
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
def as_json
|
25
|
+
{
|
26
|
+
self: location_url,
|
27
|
+
first: first_page_url,
|
28
|
+
prev: prev_page_url,
|
29
|
+
next: next_page_url,
|
30
|
+
last: last_page_url
|
31
|
+
}
|
32
|
+
end
|
33
|
+
|
34
|
+
protected
|
35
|
+
|
36
|
+
attr_reader :adapter_options
|
37
|
+
|
38
|
+
private
|
39
|
+
|
40
|
+
def location_url
|
41
|
+
url_for_page(collection.current_page)
|
42
|
+
end
|
43
|
+
|
44
|
+
def first_page_url
|
45
|
+
url_for_page(1)
|
46
|
+
end
|
47
|
+
|
48
|
+
def last_page_url
|
49
|
+
if collection.total_pages == 0
|
50
|
+
url_for_page(FIRST_PAGE)
|
51
|
+
else
|
52
|
+
url_for_page(collection.total_pages)
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
def prev_page_url
|
57
|
+
return nil if collection.current_page == FIRST_PAGE
|
58
|
+
url_for_page(collection.current_page - FIRST_PAGE)
|
59
|
+
end
|
60
|
+
|
61
|
+
def next_page_url
|
62
|
+
return nil if collection.total_pages == 0 || collection.current_page == collection.total_pages
|
63
|
+
url_for_page(collection.next_page)
|
64
|
+
end
|
65
|
+
|
66
|
+
def url_for_page(number)
|
67
|
+
params = query_parameters.dup
|
68
|
+
params[:page] = { size: per_page, number: number }
|
69
|
+
"#{url(adapter_options)}?#{params.to_query}"
|
70
|
+
end
|
71
|
+
|
72
|
+
def url(options = {})
|
73
|
+
@url ||= options.fetch(:links, {}).fetch(:self, nil) || request_url
|
74
|
+
end
|
75
|
+
|
76
|
+
def request_url
|
77
|
+
@request_url ||= context.request_url
|
78
|
+
end
|
79
|
+
|
80
|
+
def query_parameters
|
81
|
+
@query_parameters ||= context.query_parameters
|
82
|
+
end
|
83
|
+
|
84
|
+
def per_page
|
85
|
+
@per_page ||= collection.try(:per_page) || collection.try(:limit_value) || collection.size
|
86
|
+
end
|
87
|
+
end
|
88
|
+
end
|
89
|
+
end
|
90
|
+
end
|
@@ -0,0 +1,106 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ActiveModelSerializers
|
4
|
+
module Adapter
|
5
|
+
class JsonApi
|
6
|
+
class Relationship
|
7
|
+
# {http://jsonapi.org/format/#document-resource-object-related-resource-links Document Resource Object Related Resource Links}
|
8
|
+
# {http://jsonapi.org/format/#document-links Document Links}
|
9
|
+
# {http://jsonapi.org/format/#document-resource-object-linkage Document Resource Relationship Linkage}
|
10
|
+
# {http://jsonapi.org/format/#document-meta Document Meta}
|
11
|
+
def initialize(parent_serializer, serializable_resource_options, association)
|
12
|
+
@parent_serializer = parent_serializer
|
13
|
+
@association = association
|
14
|
+
@serializable_resource_options = serializable_resource_options
|
15
|
+
end
|
16
|
+
|
17
|
+
def as_json
|
18
|
+
hash = {}
|
19
|
+
|
20
|
+
hash[:data] = data_for(association) if association.include_data?
|
21
|
+
|
22
|
+
links = links_for(association)
|
23
|
+
hash[:links] = links if links.any?
|
24
|
+
|
25
|
+
meta = meta_for(association)
|
26
|
+
hash[:meta] = meta if meta
|
27
|
+
hash[:meta] = {} if hash.empty?
|
28
|
+
|
29
|
+
hash
|
30
|
+
end
|
31
|
+
|
32
|
+
protected
|
33
|
+
|
34
|
+
attr_reader :parent_serializer, :serializable_resource_options, :association
|
35
|
+
|
36
|
+
private
|
37
|
+
|
38
|
+
# TODO(BF): Avoid db hit on belong_to_ releationship by using foreign_key on self
|
39
|
+
def data_for(association)
|
40
|
+
if association.collection?
|
41
|
+
data_for_many(association)
|
42
|
+
else
|
43
|
+
data_for_one(association)
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
def data_for_one(association)
|
48
|
+
if belongs_to_id_on_self?(association)
|
49
|
+
id = parent_serializer.read_attribute_for_serialization(association.reflection.foreign_key)
|
50
|
+
type =
|
51
|
+
if association.polymorphic?
|
52
|
+
# We can't infer resource type for polymorphic relationships from the serializer.
|
53
|
+
# We can ONLY know a polymorphic resource type by inspecting each resource.
|
54
|
+
association.lazy_association.serializer.json_key
|
55
|
+
else
|
56
|
+
association.reflection.type.to_s
|
57
|
+
end
|
58
|
+
ResourceIdentifier.for_type_with_id(type, id, serializable_resource_options)
|
59
|
+
else
|
60
|
+
# TODO(BF): Process relationship without evaluating lazy_association
|
61
|
+
serializer = association.lazy_association.serializer
|
62
|
+
if (virtual_value = association.virtual_value)
|
63
|
+
virtual_value
|
64
|
+
elsif serializer && association.object
|
65
|
+
ResourceIdentifier.new(serializer, serializable_resource_options).as_json
|
66
|
+
else
|
67
|
+
nil
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
def data_for_many(association)
|
73
|
+
# TODO(BF): Process relationship without evaluating lazy_association
|
74
|
+
collection_serializer = association.lazy_association.serializer
|
75
|
+
if collection_serializer.respond_to?(:each)
|
76
|
+
collection_serializer.map do |serializer|
|
77
|
+
ResourceIdentifier.new(serializer, serializable_resource_options).as_json
|
78
|
+
end
|
79
|
+
elsif (virtual_value = association.virtual_value)
|
80
|
+
virtual_value
|
81
|
+
else
|
82
|
+
[]
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
86
|
+
def links_for(association)
|
87
|
+
association.links.each_with_object({}) do |(key, value), hash|
|
88
|
+
result = Link.new(parent_serializer, value).as_json
|
89
|
+
hash[key] = result if result
|
90
|
+
end
|
91
|
+
end
|
92
|
+
|
93
|
+
def meta_for(association)
|
94
|
+
meta = association.meta
|
95
|
+
meta.respond_to?(:call) ? parent_serializer.instance_eval(&meta) : meta
|
96
|
+
end
|
97
|
+
|
98
|
+
def belongs_to_id_on_self?(association)
|
99
|
+
parent_serializer.config.jsonapi_use_foreign_key_on_belongs_to_relationship &&
|
100
|
+
association.belongs_to? &&
|
101
|
+
parent_serializer.object.respond_to?(association.reflection.foreign_key)
|
102
|
+
end
|
103
|
+
end
|
104
|
+
end
|
105
|
+
end
|
106
|
+
end
|
@@ -0,0 +1,68 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ActiveModelSerializers
|
4
|
+
module Adapter
|
5
|
+
class JsonApi
|
6
|
+
class ResourceIdentifier
|
7
|
+
def self.type_for(serializer, serializer_type = nil, transform_options = {})
|
8
|
+
raw_type = serializer_type ? serializer_type : raw_type_from_serializer_object(serializer.object)
|
9
|
+
JsonApi.send(:transform_key_casing!, raw_type, transform_options)
|
10
|
+
end
|
11
|
+
|
12
|
+
def self.for_type_with_id(type, id, options)
|
13
|
+
type = inflect_type(type)
|
14
|
+
type = type_for(:no_class_needed, type, options)
|
15
|
+
if id.blank?
|
16
|
+
{ type: type }
|
17
|
+
else
|
18
|
+
{ id: id.to_s, type: type }
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
def self.raw_type_from_serializer_object(object)
|
23
|
+
class_name = object.class.name # should use model_name
|
24
|
+
raw_type = class_name.underscore
|
25
|
+
raw_type = inflect_type(raw_type)
|
26
|
+
raw_type
|
27
|
+
.gsub!('/'.freeze, ActiveModelSerializers.config.jsonapi_namespace_separator)
|
28
|
+
raw_type
|
29
|
+
end
|
30
|
+
|
31
|
+
def self.inflect_type(type)
|
32
|
+
singularize = ActiveModelSerializers.config.jsonapi_resource_type == :singular
|
33
|
+
inflection = singularize ? :singularize : :pluralize
|
34
|
+
ActiveSupport::Inflector.public_send(inflection, type)
|
35
|
+
end
|
36
|
+
|
37
|
+
# {http://jsonapi.org/format/#document-resource-identifier-objects Resource Identifier Objects}
|
38
|
+
def initialize(serializer, options)
|
39
|
+
@id = id_for(serializer)
|
40
|
+
@type = type_for(serializer, options)
|
41
|
+
end
|
42
|
+
|
43
|
+
def as_json
|
44
|
+
if id.blank?
|
45
|
+
{ type: type }
|
46
|
+
else
|
47
|
+
{ id: id.to_s, type: type }
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
protected
|
52
|
+
|
53
|
+
attr_reader :id, :type
|
54
|
+
|
55
|
+
private
|
56
|
+
|
57
|
+
def type_for(serializer, transform_options)
|
58
|
+
serializer_type = serializer._type
|
59
|
+
self.class.type_for(serializer, serializer_type, transform_options)
|
60
|
+
end
|
61
|
+
|
62
|
+
def id_for(serializer)
|
63
|
+
serializer.read_attribute_for_serialization(:id).to_s
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
@@ -0,0 +1,57 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# Adapted from
|
4
|
+
# https://github.com/rails/rails/blob/7f18ea14c8/activejob/lib/active_job/callbacks.rb
|
5
|
+
require 'active_support/callbacks'
|
6
|
+
|
7
|
+
module ActiveModelSerializers
|
8
|
+
# = ActiveModelSerializers Callbacks
|
9
|
+
#
|
10
|
+
# ActiveModelSerializers provides hooks during the life cycle of serialization and
|
11
|
+
# allow you to trigger logic. Available callbacks are:
|
12
|
+
#
|
13
|
+
# * <tt>around_render</tt>
|
14
|
+
#
|
15
|
+
module Callbacks
|
16
|
+
extend ActiveSupport::Concern
|
17
|
+
include ActiveSupport::Callbacks
|
18
|
+
|
19
|
+
included do
|
20
|
+
define_callbacks :render
|
21
|
+
end
|
22
|
+
|
23
|
+
# These methods will be included into any ActiveModelSerializers object, adding
|
24
|
+
# callbacks for +render+.
|
25
|
+
module ClassMethods
|
26
|
+
# Defines a callback that will get called around the render method,
|
27
|
+
# whether it is as_json, to_json, or serializable_hash
|
28
|
+
#
|
29
|
+
# class ActiveModelSerializers::SerializableResource
|
30
|
+
# include ActiveModelSerializers::Callbacks
|
31
|
+
#
|
32
|
+
# around_render do |args, block|
|
33
|
+
# tag_logger do
|
34
|
+
# notify_render do
|
35
|
+
# block.call(args)
|
36
|
+
# end
|
37
|
+
# end
|
38
|
+
# end
|
39
|
+
#
|
40
|
+
# def as_json
|
41
|
+
# run_callbacks :render do
|
42
|
+
# adapter.as_json
|
43
|
+
# end
|
44
|
+
# end
|
45
|
+
# # Note: So that we can re-use the instrumenter for as_json, to_json, and
|
46
|
+
# # serializable_hash, we aren't using the usual format, which would be:
|
47
|
+
# # def render(args)
|
48
|
+
# # adapter.as_json
|
49
|
+
# # end
|
50
|
+
# end
|
51
|
+
#
|
52
|
+
def around_render(*filters, &blk)
|
53
|
+
set_callback(:render, :around, *filters, &blk)
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
@@ -0,0 +1,56 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
##
|
4
|
+
# Provides a single method +deprecate+ to be used to declare when
|
5
|
+
# something is going away.
|
6
|
+
#
|
7
|
+
# class Legacy
|
8
|
+
# def self.klass_method
|
9
|
+
# # ...
|
10
|
+
# end
|
11
|
+
#
|
12
|
+
# def instance_method
|
13
|
+
# # ...
|
14
|
+
# end
|
15
|
+
#
|
16
|
+
# extend ActiveModelSerializers::Deprecate
|
17
|
+
# deprecate :instance_method, "ActiveModelSerializers::NewPlace#new_method"
|
18
|
+
#
|
19
|
+
# class << self
|
20
|
+
# extend ActiveModelSerializers::Deprecate
|
21
|
+
# deprecate :klass_method, :none
|
22
|
+
# end
|
23
|
+
# end
|
24
|
+
#
|
25
|
+
# Adapted from https://github.com/rubygems/rubygems/blob/1591331/lib/rubygems/deprecate.rb
|
26
|
+
module ActiveModelSerializers
|
27
|
+
module Deprecate
|
28
|
+
##
|
29
|
+
# Simple deprecation method that deprecates +name+ by wrapping it up
|
30
|
+
# in a dummy method. It warns on each call to the dummy method
|
31
|
+
# telling the user of +replacement+ (unless +replacement+ is :none) that it is planned to go away.
|
32
|
+
|
33
|
+
def deprecate(name, replacement)
|
34
|
+
old = "_deprecated_#{name}"
|
35
|
+
alias_method old, name
|
36
|
+
class_eval do
|
37
|
+
define_method(name) do |*args, &block|
|
38
|
+
target = is_a?(Module) ? "#{self}." : "#{self.class}#"
|
39
|
+
msg = ["NOTE: #{target}#{name} is deprecated",
|
40
|
+
replacement == :none ? ' with no replacement' : "; use #{replacement} instead",
|
41
|
+
"\n#{target}#{name} called from #{ActiveModelSerializers.location_of_caller.join(':')}"]
|
42
|
+
warn "#{msg.join}."
|
43
|
+
send old, *args, &block
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
def delegate_and_deprecate(method, delegee)
|
49
|
+
delegate method, to: delegee
|
50
|
+
deprecate method, "#{delegee.name}."
|
51
|
+
end
|
52
|
+
|
53
|
+
module_function :deprecate
|
54
|
+
module_function :delegate_and_deprecate
|
55
|
+
end
|
56
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ActiveModelSerializers
|
4
|
+
module Deserialization
|
5
|
+
module_function
|
6
|
+
|
7
|
+
def jsonapi_parse(*args)
|
8
|
+
Adapter::JsonApi::Deserialization.parse(*args)
|
9
|
+
end
|
10
|
+
|
11
|
+
# :nocov:
|
12
|
+
def jsonapi_parse!(*args)
|
13
|
+
Adapter::JsonApi::Deserialization.parse!(*args)
|
14
|
+
end
|
15
|
+
# :nocov:
|
16
|
+
end
|
17
|
+
end
|