agi_active_model_serializers 0.10.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- 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 +102 -0
- data/.simplecov +110 -0
- data/.travis.yml +51 -0
- data/CHANGELOG.md +612 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/CONTRIBUTING.md +105 -0
- data/Gemfile +56 -0
- data/MIT-LICENSE +22 -0
- data/README.md +307 -0
- data/Rakefile +103 -0
- data/active_model_serializers.gemspec +63 -0
- data/appveyor.yml +24 -0
- data/bin/bench +171 -0
- data/bin/bench_regression +316 -0
- data/bin/serve_benchmark +39 -0
- data/docs/README.md +41 -0
- data/docs/STYLE.md +58 -0
- data/docs/general/adapters.md +247 -0
- data/docs/general/caching.md +58 -0
- data/docs/general/configuration_options.md +169 -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 +14 -0
- data/docs/general/rendering.md +279 -0
- data/docs/general/serializers.md +461 -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 +137 -0
- data/docs/howto/add_root_key.md +55 -0
- data/docs/howto/grape_integration.md +42 -0
- data/docs/howto/outside_controller_use.md +65 -0
- data/docs/howto/passing_arbitrary_options.md +27 -0
- data/docs/howto/serialize_poro.md +32 -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 +144 -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 +66 -0
- data/lib/active_model/serializable_resource.rb +11 -0
- data/lib/active_model/serializer.rb +231 -0
- data/lib/active_model/serializer/adapter.rb +24 -0
- data/lib/active_model/serializer/adapter/attributes.rb +15 -0
- data/lib/active_model/serializer/adapter/base.rb +18 -0
- data/lib/active_model/serializer/adapter/json.rb +15 -0
- data/lib/active_model/serializer/adapter/json_api.rb +15 -0
- data/lib/active_model/serializer/adapter/null.rb +15 -0
- data/lib/active_model/serializer/array_serializer.rb +12 -0
- data/lib/active_model/serializer/association.rb +34 -0
- data/lib/active_model/serializer/attribute.rb +25 -0
- data/lib/active_model/serializer/belongs_to_reflection.rb +7 -0
- data/lib/active_model/serializer/collection_reflection.rb +7 -0
- data/lib/active_model/serializer/collection_serializer.rb +87 -0
- data/lib/active_model/serializer/concerns/associations.rb +102 -0
- data/lib/active_model/serializer/concerns/attributes.rb +82 -0
- data/lib/active_model/serializer/concerns/caching.rb +292 -0
- data/lib/active_model/serializer/concerns/configuration.rb +59 -0
- data/lib/active_model/serializer/concerns/links.rb +35 -0
- data/lib/active_model/serializer/concerns/meta.rb +29 -0
- data/lib/active_model/serializer/concerns/type.rb +25 -0
- data/lib/active_model/serializer/error_serializer.rb +14 -0
- data/lib/active_model/serializer/errors_serializer.rb +32 -0
- data/lib/active_model/serializer/field.rb +90 -0
- data/lib/active_model/serializer/fieldset.rb +31 -0
- data/lib/active_model/serializer/has_many_reflection.rb +7 -0
- data/lib/active_model/serializer/has_one_reflection.rb +7 -0
- data/lib/active_model/serializer/lint.rb +150 -0
- data/lib/active_model/serializer/null.rb +17 -0
- data/lib/active_model/serializer/reflection.rb +163 -0
- data/lib/active_model/serializer/singular_reflection.rb +7 -0
- data/lib/active_model/serializer/version.rb +5 -0
- data/lib/active_model_serializers.rb +53 -0
- data/lib/active_model_serializers/adapter.rb +98 -0
- data/lib/active_model_serializers/adapter/attributes.rb +13 -0
- data/lib/active_model_serializers/adapter/base.rb +83 -0
- data/lib/active_model_serializers/adapter/json.rb +21 -0
- data/lib/active_model_serializers/adapter/json_api.rb +517 -0
- data/lib/active_model_serializers/adapter/json_api/deserialization.rb +213 -0
- data/lib/active_model_serializers/adapter/json_api/error.rb +96 -0
- data/lib/active_model_serializers/adapter/json_api/jsonapi.rb +49 -0
- data/lib/active_model_serializers/adapter/json_api/link.rb +83 -0
- data/lib/active_model_serializers/adapter/json_api/meta.rb +37 -0
- data/lib/active_model_serializers/adapter/json_api/pagination_links.rb +69 -0
- data/lib/active_model_serializers/adapter/json_api/relationship.rb +63 -0
- data/lib/active_model_serializers/adapter/json_api/resource_identifier.rb +51 -0
- data/lib/active_model_serializers/adapter/null.rb +9 -0
- data/lib/active_model_serializers/callbacks.rb +55 -0
- data/lib/active_model_serializers/deprecate.rb +54 -0
- data/lib/active_model_serializers/deserialization.rb +15 -0
- data/lib/active_model_serializers/json_pointer.rb +14 -0
- data/lib/active_model_serializers/logging.rb +122 -0
- data/lib/active_model_serializers/lookup_chain.rb +80 -0
- data/lib/active_model_serializers/model.rb +71 -0
- data/lib/active_model_serializers/railtie.rb +48 -0
- data/lib/active_model_serializers/register_jsonapi_renderer.rb +78 -0
- data/lib/active_model_serializers/serializable_resource.rb +82 -0
- data/lib/active_model_serializers/serialization_context.rb +39 -0
- data/lib/active_model_serializers/test.rb +7 -0
- data/lib/active_model_serializers/test/schema.rb +138 -0
- data/lib/active_model_serializers/test/serializer.rb +125 -0
- data/lib/generators/rails/USAGE +6 -0
- data/lib/generators/rails/resource_override.rb +10 -0
- data/lib/generators/rails/serializer_generator.rb +36 -0
- data/lib/generators/rails/templates/serializer.rb.erb +15 -0
- data/lib/grape/active_model_serializers.rb +16 -0
- data/lib/grape/formatters/active_model_serializers.rb +32 -0
- data/lib/grape/helpers/active_model_serializers.rb +17 -0
- data/test/action_controller/adapter_selector_test.rb +53 -0
- data/test/action_controller/explicit_serializer_test.rb +135 -0
- data/test/action_controller/json/include_test.rb +246 -0
- data/test/action_controller/json_api/deserialization_test.rb +112 -0
- data/test/action_controller/json_api/errors_test.rb +40 -0
- data/test/action_controller/json_api/fields_test.rb +66 -0
- data/test/action_controller/json_api/linked_test.rb +202 -0
- data/test/action_controller/json_api/pagination_test.rb +116 -0
- data/test/action_controller/json_api/transform_test.rb +189 -0
- data/test/action_controller/lookup_proc_test.rb +49 -0
- data/test/action_controller/namespace_lookup_test.rb +232 -0
- data/test/action_controller/serialization_scope_name_test.rb +229 -0
- data/test/action_controller/serialization_test.rb +472 -0
- data/test/active_model_serializers/adapter_for_test.rb +208 -0
- data/test/active_model_serializers/json_pointer_test.rb +22 -0
- data/test/active_model_serializers/logging_test.rb +77 -0
- data/test/active_model_serializers/model_test.rb +69 -0
- data/test/active_model_serializers/railtie_test_isolated.rb +63 -0
- data/test/active_model_serializers/register_jsonapi_renderer_test_isolated.rb +161 -0
- data/test/active_model_serializers/serialization_context_test_isolated.rb +71 -0
- data/test/active_model_serializers/test/schema_test.rb +131 -0
- data/test/active_model_serializers/test/serializer_test.rb +62 -0
- data/test/active_record_test.rb +9 -0
- data/test/adapter/attributes_test.rb +43 -0
- data/test/adapter/deprecation_test.rb +100 -0
- data/test/adapter/json/belongs_to_test.rb +45 -0
- data/test/adapter/json/collection_test.rb +104 -0
- data/test/adapter/json/has_many_test.rb +45 -0
- data/test/adapter/json/transform_test.rb +93 -0
- data/test/adapter/json_api/belongs_to_test.rb +155 -0
- data/test/adapter/json_api/collection_test.rb +96 -0
- data/test/adapter/json_api/errors_test.rb +76 -0
- data/test/adapter/json_api/fields_test.rb +96 -0
- data/test/adapter/json_api/has_many_embed_ids_test.rb +43 -0
- data/test/adapter/json_api/has_many_explicit_serializer_test.rb +96 -0
- data/test/adapter/json_api/has_many_test.rb +165 -0
- data/test/adapter/json_api/has_one_test.rb +80 -0
- data/test/adapter/json_api/include_data_if_sideloaded_test.rb +168 -0
- data/test/adapter/json_api/json_api_test.rb +33 -0
- data/test/adapter/json_api/linked_test.rb +413 -0
- data/test/adapter/json_api/links_test.rb +95 -0
- data/test/adapter/json_api/pagination_links_test.rb +193 -0
- data/test/adapter/json_api/parse_test.rb +137 -0
- data/test/adapter/json_api/relationship_test.rb +397 -0
- data/test/adapter/json_api/resource_identifier_test.rb +110 -0
- data/test/adapter/json_api/resource_meta_test.rb +100 -0
- data/test/adapter/json_api/toplevel_jsonapi_test.rb +82 -0
- data/test/adapter/json_api/transform_test.rb +512 -0
- data/test/adapter/json_api/type_test.rb +61 -0
- data/test/adapter/json_test.rb +46 -0
- data/test/adapter/null_test.rb +22 -0
- data/test/adapter/polymorphic_test.rb +171 -0
- data/test/adapter_test.rb +67 -0
- data/test/array_serializer_test.rb +22 -0
- data/test/benchmark/app.rb +65 -0
- data/test/benchmark/benchmarking_support.rb +67 -0
- data/test/benchmark/bm_active_record.rb +81 -0
- data/test/benchmark/bm_adapter.rb +38 -0
- data/test/benchmark/bm_caching.rb +119 -0
- data/test/benchmark/bm_lookup_chain.rb +83 -0
- data/test/benchmark/bm_transform.rb +45 -0
- data/test/benchmark/config.ru +3 -0
- data/test/benchmark/controllers.rb +83 -0
- data/test/benchmark/fixtures.rb +219 -0
- data/test/cache_test.rb +595 -0
- data/test/collection_serializer_test.rb +123 -0
- data/test/fixtures/active_record.rb +113 -0
- data/test/fixtures/poro.rb +232 -0
- data/test/generators/scaffold_controller_generator_test.rb +24 -0
- data/test/generators/serializer_generator_test.rb +74 -0
- data/test/grape_test.rb +178 -0
- data/test/lint_test.rb +49 -0
- data/test/logger_test.rb +20 -0
- data/test/poro_test.rb +9 -0
- data/test/serializable_resource_test.rb +79 -0
- data/test/serializers/association_macros_test.rb +37 -0
- data/test/serializers/associations_test.rb +383 -0
- data/test/serializers/attribute_test.rb +153 -0
- data/test/serializers/attributes_test.rb +52 -0
- data/test/serializers/caching_configuration_test_isolated.rb +170 -0
- data/test/serializers/configuration_test.rb +32 -0
- data/test/serializers/fieldset_test.rb +14 -0
- data/test/serializers/meta_test.rb +202 -0
- data/test/serializers/options_test.rb +32 -0
- data/test/serializers/read_attribute_for_serialization_test.rb +79 -0
- data/test/serializers/root_test.rb +21 -0
- data/test/serializers/serialization_test.rb +55 -0
- data/test/serializers/serializer_for_test.rb +136 -0
- data/test/serializers/serializer_for_with_namespace_test.rb +88 -0
- data/test/support/custom_schemas/active_model_serializers/test/schema_test/my/index.json +6 -0
- data/test/support/isolated_unit.rb +82 -0
- data/test/support/rails5_shims.rb +53 -0
- data/test/support/rails_app.rb +36 -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 +71 -0
- data/test/test_helper.rb +58 -0
- metadata +602 -0
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
module ActiveModelSerializers
|
|
2
|
+
module Adapter
|
|
3
|
+
class JsonApi < Base
|
|
4
|
+
class PaginationLinks
|
|
5
|
+
MissingSerializationContextError = Class.new(KeyError)
|
|
6
|
+
FIRST_PAGE = 1
|
|
7
|
+
|
|
8
|
+
attr_reader :collection, :context
|
|
9
|
+
|
|
10
|
+
def initialize(collection, adapter_options)
|
|
11
|
+
@collection = collection
|
|
12
|
+
@adapter_options = adapter_options
|
|
13
|
+
@context = adapter_options.fetch(:serialization_context) do
|
|
14
|
+
fail MissingSerializationContextError, <<-EOF.freeze
|
|
15
|
+
JsonApi::PaginationLinks requires a ActiveModelSerializers::SerializationContext.
|
|
16
|
+
Please pass a ':serialization_context' option or
|
|
17
|
+
override CollectionSerializer#paginated? to return 'false'.
|
|
18
|
+
EOF
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
def as_json
|
|
23
|
+
per_page = collection.try(:per_page) || collection.try(:limit_value) || collection.size
|
|
24
|
+
pages_from.each_with_object({}) do |(key, value), hash|
|
|
25
|
+
params = query_parameters.merge(page: { number: value, size: per_page }).to_query
|
|
26
|
+
|
|
27
|
+
hash[key] = "#{url(adapter_options)}?#{params}"
|
|
28
|
+
end
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
protected
|
|
32
|
+
|
|
33
|
+
attr_reader :adapter_options
|
|
34
|
+
|
|
35
|
+
private
|
|
36
|
+
|
|
37
|
+
def pages_from
|
|
38
|
+
return {} if collection.total_pages <= FIRST_PAGE
|
|
39
|
+
|
|
40
|
+
{}.tap do |pages|
|
|
41
|
+
pages[:self] = collection.current_page
|
|
42
|
+
|
|
43
|
+
unless collection.current_page == FIRST_PAGE
|
|
44
|
+
pages[:first] = FIRST_PAGE
|
|
45
|
+
pages[:prev] = collection.current_page - FIRST_PAGE
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
unless collection.current_page == collection.total_pages
|
|
49
|
+
pages[:next] = collection.current_page + FIRST_PAGE
|
|
50
|
+
pages[:last] = collection.total_pages
|
|
51
|
+
end
|
|
52
|
+
end
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
def url(options)
|
|
56
|
+
@url ||= options.fetch(:links, {}).fetch(:self, nil) || request_url
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
def request_url
|
|
60
|
+
@request_url ||= context.request_url
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
def query_parameters
|
|
64
|
+
@query_parameters ||= context.query_parameters
|
|
65
|
+
end
|
|
66
|
+
end
|
|
67
|
+
end
|
|
68
|
+
end
|
|
69
|
+
end
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
module ActiveModelSerializers
|
|
2
|
+
module Adapter
|
|
3
|
+
class JsonApi
|
|
4
|
+
class Relationship
|
|
5
|
+
# {http://jsonapi.org/format/#document-resource-object-related-resource-links Document Resource Object Related Resource Links}
|
|
6
|
+
# {http://jsonapi.org/format/#document-links Document Links}
|
|
7
|
+
# {http://jsonapi.org/format/#document-resource-object-linkage Document Resource Relationship Linkage}
|
|
8
|
+
# {http://jsonapi.org/format/#document-meta Document Meta}
|
|
9
|
+
def initialize(parent_serializer, serializable_resource_options, association)
|
|
10
|
+
@parent_serializer = parent_serializer
|
|
11
|
+
@association = association
|
|
12
|
+
@serializable_resource_options = serializable_resource_options
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
def as_json
|
|
16
|
+
hash = {}
|
|
17
|
+
|
|
18
|
+
if association.options[:include_data]
|
|
19
|
+
hash[:data] = data_for(association)
|
|
20
|
+
end
|
|
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
|
+
def data_for(association)
|
|
39
|
+
serializer = association.serializer
|
|
40
|
+
if serializer.respond_to?(:each)
|
|
41
|
+
serializer.map { |s| ResourceIdentifier.new(s, serializable_resource_options).as_json }
|
|
42
|
+
elsif (virtual_value = association.options[:virtual_value])
|
|
43
|
+
virtual_value
|
|
44
|
+
elsif serializer && serializer.object
|
|
45
|
+
ResourceIdentifier.new(serializer, serializable_resource_options).as_json
|
|
46
|
+
end
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
def links_for(association)
|
|
50
|
+
association.links.each_with_object({}) do |(key, value), hash|
|
|
51
|
+
result = Link.new(parent_serializer, value).as_json
|
|
52
|
+
hash[key] = result if result
|
|
53
|
+
end
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
def meta_for(association)
|
|
57
|
+
meta = association.meta
|
|
58
|
+
meta.respond_to?(:call) ? parent_serializer.instance_eval(&meta) : meta
|
|
59
|
+
end
|
|
60
|
+
end
|
|
61
|
+
end
|
|
62
|
+
end
|
|
63
|
+
end
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
module ActiveModelSerializers
|
|
2
|
+
module Adapter
|
|
3
|
+
class JsonApi
|
|
4
|
+
class ResourceIdentifier
|
|
5
|
+
def self.type_for(class_name, serializer_type = nil, transform_options = {})
|
|
6
|
+
if serializer_type
|
|
7
|
+
raw_type = serializer_type
|
|
8
|
+
else
|
|
9
|
+
inflection =
|
|
10
|
+
if ActiveModelSerializers.config.jsonapi_resource_type == :singular
|
|
11
|
+
:singularize
|
|
12
|
+
else
|
|
13
|
+
:pluralize
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
raw_type = class_name.underscore
|
|
17
|
+
raw_type = ActiveSupport::Inflector.public_send(inflection, raw_type)
|
|
18
|
+
raw_type
|
|
19
|
+
.gsub!('/'.freeze, ActiveModelSerializers.config.jsonapi_namespace_separator)
|
|
20
|
+
raw_type
|
|
21
|
+
end
|
|
22
|
+
JsonApi.send(:transform_key_casing!, raw_type, transform_options)
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
# {http://jsonapi.org/format/#document-resource-identifier-objects Resource Identifier Objects}
|
|
26
|
+
def initialize(serializer, options)
|
|
27
|
+
@id = id_for(serializer)
|
|
28
|
+
@type = type_for(serializer, options)
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def as_json
|
|
32
|
+
{ id: id, type: type }
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
protected
|
|
36
|
+
|
|
37
|
+
attr_reader :id, :type
|
|
38
|
+
|
|
39
|
+
private
|
|
40
|
+
|
|
41
|
+
def type_for(serializer, transform_options)
|
|
42
|
+
self.class.type_for(serializer.object.class.name, serializer._type, transform_options)
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
def id_for(serializer)
|
|
46
|
+
serializer.read_attribute_for_serialization(:id).to_s
|
|
47
|
+
end
|
|
48
|
+
end
|
|
49
|
+
end
|
|
50
|
+
end
|
|
51
|
+
end
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
# Adapted from
|
|
2
|
+
# https://github.com/rails/rails/blob/7f18ea14c8/activejob/lib/active_job/callbacks.rb
|
|
3
|
+
require 'active_support/callbacks'
|
|
4
|
+
|
|
5
|
+
module ActiveModelSerializers
|
|
6
|
+
# = ActiveModelSerializers Callbacks
|
|
7
|
+
#
|
|
8
|
+
# ActiveModelSerializers provides hooks during the life cycle of serialization and
|
|
9
|
+
# allow you to trigger logic. Available callbacks are:
|
|
10
|
+
#
|
|
11
|
+
# * <tt>around_render</tt>
|
|
12
|
+
#
|
|
13
|
+
module Callbacks
|
|
14
|
+
extend ActiveSupport::Concern
|
|
15
|
+
include ActiveSupport::Callbacks
|
|
16
|
+
|
|
17
|
+
included do
|
|
18
|
+
define_callbacks :render
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
# These methods will be included into any ActiveModelSerializers object, adding
|
|
22
|
+
# callbacks for +render+.
|
|
23
|
+
module ClassMethods
|
|
24
|
+
# Defines a callback that will get called around the render method,
|
|
25
|
+
# whether it is as_json, to_json, or serializable_hash
|
|
26
|
+
#
|
|
27
|
+
# class ActiveModelSerializers::SerializableResource
|
|
28
|
+
# include ActiveModelSerializers::Callbacks
|
|
29
|
+
#
|
|
30
|
+
# around_render do |args, block|
|
|
31
|
+
# tag_logger do
|
|
32
|
+
# notify_render do
|
|
33
|
+
# block.call(args)
|
|
34
|
+
# end
|
|
35
|
+
# end
|
|
36
|
+
# end
|
|
37
|
+
#
|
|
38
|
+
# def as_json
|
|
39
|
+
# run_callbacks :render do
|
|
40
|
+
# adapter.as_json
|
|
41
|
+
# end
|
|
42
|
+
# end
|
|
43
|
+
# # Note: So that we can re-use the instrumenter for as_json, to_json, and
|
|
44
|
+
# # serializable_hash, we aren't using the usual format, which would be:
|
|
45
|
+
# # def render(args)
|
|
46
|
+
# # adapter.as_json
|
|
47
|
+
# # end
|
|
48
|
+
# end
|
|
49
|
+
#
|
|
50
|
+
def around_render(*filters, &blk)
|
|
51
|
+
set_callback(:render, :around, *filters, &blk)
|
|
52
|
+
end
|
|
53
|
+
end
|
|
54
|
+
end
|
|
55
|
+
end
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
##
|
|
2
|
+
# Provides a single method +deprecate+ to be used to declare when
|
|
3
|
+
# something is going away.
|
|
4
|
+
#
|
|
5
|
+
# class Legacy
|
|
6
|
+
# def self.klass_method
|
|
7
|
+
# # ...
|
|
8
|
+
# end
|
|
9
|
+
#
|
|
10
|
+
# def instance_method
|
|
11
|
+
# # ...
|
|
12
|
+
# end
|
|
13
|
+
#
|
|
14
|
+
# extend ActiveModelSerializers::Deprecate
|
|
15
|
+
# deprecate :instance_method, "ActiveModelSerializers::NewPlace#new_method"
|
|
16
|
+
#
|
|
17
|
+
# class << self
|
|
18
|
+
# extend ActiveModelSerializers::Deprecate
|
|
19
|
+
# deprecate :klass_method, :none
|
|
20
|
+
# end
|
|
21
|
+
# end
|
|
22
|
+
#
|
|
23
|
+
# Adapted from https://github.com/rubygems/rubygems/blob/1591331/lib/rubygems/deprecate.rb
|
|
24
|
+
module ActiveModelSerializers
|
|
25
|
+
module Deprecate
|
|
26
|
+
##
|
|
27
|
+
# Simple deprecation method that deprecates +name+ by wrapping it up
|
|
28
|
+
# in a dummy method. It warns on each call to the dummy method
|
|
29
|
+
# telling the user of +replacement+ (unless +replacement+ is :none) that it is planned to go away.
|
|
30
|
+
|
|
31
|
+
def deprecate(name, replacement)
|
|
32
|
+
old = "_deprecated_#{name}"
|
|
33
|
+
alias_method old, name
|
|
34
|
+
class_eval do
|
|
35
|
+
define_method(name) do |*args, &block|
|
|
36
|
+
target = is_a?(Module) ? "#{self}." : "#{self.class}#"
|
|
37
|
+
msg = ["NOTE: #{target}#{name} is deprecated",
|
|
38
|
+
replacement == :none ? ' with no replacement' : "; use #{replacement} instead",
|
|
39
|
+
"\n#{target}#{name} called from #{ActiveModelSerializers.location_of_caller.join(':')}"]
|
|
40
|
+
warn "#{msg.join}."
|
|
41
|
+
send old, *args, &block
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
def delegate_and_deprecate(method, delegee)
|
|
47
|
+
delegate method, to: delegee
|
|
48
|
+
deprecate method, "#{delegee.name}."
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
module_function :deprecate
|
|
52
|
+
module_function :delegate_and_deprecate
|
|
53
|
+
end
|
|
54
|
+
end
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
module ActiveModelSerializers
|
|
2
|
+
module Deserialization
|
|
3
|
+
module_function
|
|
4
|
+
|
|
5
|
+
def jsonapi_parse(*args)
|
|
6
|
+
Adapter::JsonApi::Deserialization.parse(*args)
|
|
7
|
+
end
|
|
8
|
+
|
|
9
|
+
# :nocov:
|
|
10
|
+
def jsonapi_parse!(*args)
|
|
11
|
+
Adapter::JsonApi::Deserialization.parse!(*args)
|
|
12
|
+
end
|
|
13
|
+
# :nocov:
|
|
14
|
+
end
|
|
15
|
+
end
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
module ActiveModelSerializers
|
|
2
|
+
module JsonPointer
|
|
3
|
+
module_function
|
|
4
|
+
|
|
5
|
+
POINTERS = {
|
|
6
|
+
attribute: '/data/attributes/%s'.freeze,
|
|
7
|
+
primary_data: '/data%s'.freeze
|
|
8
|
+
}.freeze
|
|
9
|
+
|
|
10
|
+
def new(pointer_type, value = nil)
|
|
11
|
+
format(POINTERS[pointer_type], value)
|
|
12
|
+
end
|
|
13
|
+
end
|
|
14
|
+
end
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
##
|
|
2
|
+
# ActiveModelSerializers::Logging
|
|
3
|
+
#
|
|
4
|
+
# https://github.com/rails/rails/blob/280654ef88/activejob/lib/active_job/logging.rb
|
|
5
|
+
#
|
|
6
|
+
module ActiveModelSerializers
|
|
7
|
+
module Logging
|
|
8
|
+
RENDER_EVENT = 'render.active_model_serializers'.freeze
|
|
9
|
+
extend ActiveSupport::Concern
|
|
10
|
+
|
|
11
|
+
included do
|
|
12
|
+
include ActiveModelSerializers::Callbacks
|
|
13
|
+
extend Macros
|
|
14
|
+
instrument_rendering
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
module ClassMethods
|
|
18
|
+
def instrument_rendering
|
|
19
|
+
around_render do |args, block|
|
|
20
|
+
tag_logger do
|
|
21
|
+
notify_render do
|
|
22
|
+
block.call(args)
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
# Macros that can be used to customize the logging of class or instance methods,
|
|
30
|
+
# by extending the class or its singleton.
|
|
31
|
+
#
|
|
32
|
+
# Adapted from:
|
|
33
|
+
# https://github.com/rubygems/rubygems/blob/cb28f5e991/lib/rubygems/deprecate.rb
|
|
34
|
+
#
|
|
35
|
+
# Provides a single method +notify+ to be used to declare when
|
|
36
|
+
# something a method notifies, with the argument +callback_name+ of the notification callback.
|
|
37
|
+
#
|
|
38
|
+
# class Adapter
|
|
39
|
+
# def self.klass_method
|
|
40
|
+
# # ...
|
|
41
|
+
# end
|
|
42
|
+
#
|
|
43
|
+
# def instance_method
|
|
44
|
+
# # ...
|
|
45
|
+
# end
|
|
46
|
+
#
|
|
47
|
+
# include ActiveModelSerializers::Logging::Macros
|
|
48
|
+
# notify :instance_method, :render
|
|
49
|
+
#
|
|
50
|
+
# class << self
|
|
51
|
+
# extend ActiveModelSerializers::Logging::Macros
|
|
52
|
+
# notify :klass_method, :render
|
|
53
|
+
# end
|
|
54
|
+
# end
|
|
55
|
+
module Macros
|
|
56
|
+
##
|
|
57
|
+
# Simple notify method that wraps up +name+
|
|
58
|
+
# in a dummy method. It notifies on with the +callback_name+ notifier on
|
|
59
|
+
# each call to the dummy method, telling what the current serializer and adapter
|
|
60
|
+
# are being rendered.
|
|
61
|
+
# Adapted from:
|
|
62
|
+
# https://github.com/rubygems/rubygems/blob/cb28f5e991/lib/rubygems/deprecate.rb
|
|
63
|
+
def notify(name, callback_name)
|
|
64
|
+
class_eval do
|
|
65
|
+
old = "_notifying_#{callback_name}_#{name}"
|
|
66
|
+
alias_method old, name
|
|
67
|
+
define_method name do |*args, &block|
|
|
68
|
+
run_callbacks callback_name do
|
|
69
|
+
send old, *args, &block
|
|
70
|
+
end
|
|
71
|
+
end
|
|
72
|
+
end
|
|
73
|
+
end
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
def notify_render(*)
|
|
77
|
+
event_name = RENDER_EVENT
|
|
78
|
+
ActiveSupport::Notifications.instrument(event_name, notify_render_payload) do
|
|
79
|
+
yield
|
|
80
|
+
end
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
def notify_render_payload
|
|
84
|
+
{
|
|
85
|
+
serializer: serializer || ActiveModel::Serializer::Null,
|
|
86
|
+
adapter: adapter || ActiveModelSerializers::Adapter::Null
|
|
87
|
+
}
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
private
|
|
91
|
+
|
|
92
|
+
def tag_logger(*tags)
|
|
93
|
+
if ActiveModelSerializers.logger.respond_to?(:tagged)
|
|
94
|
+
tags.unshift 'active_model_serializers'.freeze unless logger_tagged_by_active_model_serializers?
|
|
95
|
+
ActiveModelSerializers.logger.tagged(*tags) { yield }
|
|
96
|
+
else
|
|
97
|
+
yield
|
|
98
|
+
end
|
|
99
|
+
end
|
|
100
|
+
|
|
101
|
+
def logger_tagged_by_active_model_serializers?
|
|
102
|
+
ActiveModelSerializers.logger.formatter.current_tags.include?('active_model_serializers'.freeze)
|
|
103
|
+
end
|
|
104
|
+
|
|
105
|
+
class LogSubscriber < ActiveSupport::LogSubscriber
|
|
106
|
+
def render(event)
|
|
107
|
+
info do
|
|
108
|
+
serializer = event.payload[:serializer]
|
|
109
|
+
adapter = event.payload[:adapter]
|
|
110
|
+
duration = event.duration.round(2)
|
|
111
|
+
"Rendered #{serializer.name} with #{adapter.class} (#{duration}ms)"
|
|
112
|
+
end
|
|
113
|
+
end
|
|
114
|
+
|
|
115
|
+
def logger
|
|
116
|
+
ActiveModelSerializers.logger
|
|
117
|
+
end
|
|
118
|
+
end
|
|
119
|
+
end
|
|
120
|
+
end
|
|
121
|
+
|
|
122
|
+
ActiveModelSerializers::Logging::LogSubscriber.attach_to :active_model_serializers
|