active_model_serializers 0.10.0.rc4 → 0.10.0.rc5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.github/ISSUE_TEMPLATE.md +29 -0
- data/.github/PULL_REQUEST_TEMPLATE.md +15 -0
- data/.gitignore +1 -0
- data/.rubocop.yml +19 -1
- data/.rubocop_todo.yml +30 -103
- data/.simplecov +0 -1
- data/.travis.yml +20 -8
- data/CHANGELOG.md +89 -5
- data/CONTRIBUTING.md +54 -179
- data/Gemfile +7 -2
- data/{LICENSE.txt → MIT-LICENSE} +0 -0
- data/README.md +27 -5
- data/Rakefile +44 -16
- data/active_model_serializers.gemspec +9 -1
- data/appveyor.yml +1 -0
- data/bin/bench +171 -0
- data/bin/bench_regression +316 -0
- data/bin/serve_benchmark +39 -0
- data/docs/ARCHITECTURE.md +13 -7
- data/docs/README.md +5 -1
- data/docs/STYLE.md +58 -0
- data/docs/general/adapters.md +99 -16
- data/docs/general/configuration_options.md +87 -14
- data/docs/general/deserialization.md +100 -0
- data/docs/general/getting_started.md +35 -0
- data/docs/general/instrumentation.md +1 -1
- data/docs/general/key_transforms.md +40 -0
- data/docs/general/rendering.md +115 -13
- data/docs/general/serializers.md +138 -6
- data/docs/howto/add_pagination_links.md +36 -18
- data/docs/howto/outside_controller_use.md +4 -4
- data/docs/howto/passing_arbitrary_options.md +27 -0
- data/docs/jsonapi/errors.md +56 -0
- data/docs/jsonapi/schema.md +29 -18
- data/docs/rfcs/0000-namespace.md +106 -0
- data/docs/rfcs/template.md +15 -0
- data/lib/action_controller/serialization.rb +10 -19
- data/lib/active_model/serializable_resource.rb +4 -65
- data/lib/active_model/serializer.rb +73 -18
- data/lib/active_model/serializer/adapter.rb +15 -82
- data/lib/active_model/serializer/adapter/attributes.rb +5 -56
- data/lib/active_model/serializer/adapter/base.rb +5 -47
- data/lib/active_model/serializer/adapter/json.rb +6 -12
- data/lib/active_model/serializer/adapter/json_api.rb +5 -213
- data/lib/active_model/serializer/adapter/null.rb +7 -3
- data/lib/active_model/serializer/array_serializer.rb +3 -3
- data/lib/active_model/serializer/association.rb +4 -5
- data/lib/active_model/serializer/attributes.rb +1 -1
- data/lib/active_model/serializer/caching.rb +56 -5
- data/lib/active_model/serializer/collection_serializer.rb +30 -13
- data/lib/active_model/serializer/configuration.rb +7 -0
- data/lib/active_model/serializer/error_serializer.rb +10 -0
- data/lib/active_model/serializer/errors_serializer.rb +27 -0
- data/lib/active_model/serializer/links.rb +4 -2
- data/lib/active_model/serializer/lint.rb +14 -0
- data/lib/active_model/serializer/meta.rb +29 -0
- data/lib/active_model/serializer/null.rb +17 -0
- data/lib/active_model/serializer/reflection.rb +57 -1
- data/lib/active_model/serializer/type.rb +1 -1
- data/lib/active_model/serializer/version.rb +1 -1
- data/lib/active_model_serializers.rb +17 -0
- data/lib/active_model_serializers/adapter.rb +92 -0
- data/lib/active_model_serializers/adapter/attributes.rb +94 -0
- data/lib/active_model_serializers/adapter/base.rb +90 -0
- data/lib/active_model_serializers/adapter/json.rb +11 -0
- data/lib/active_model_serializers/adapter/json_api.rb +513 -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 +57 -0
- data/lib/active_model_serializers/adapter/json_api/relationship.rb +52 -0
- data/lib/active_model_serializers/adapter/json_api/resource_identifier.rb +37 -0
- data/lib/active_model_serializers/adapter/null.rb +10 -0
- data/lib/active_model_serializers/cached_serializer.rb +87 -0
- data/lib/active_model_serializers/callbacks.rb +1 -1
- data/lib/active_model_serializers/deprecate.rb +55 -0
- data/lib/active_model_serializers/deserialization.rb +2 -2
- data/lib/active_model_serializers/fragment_cache.rb +118 -0
- data/lib/active_model_serializers/json_pointer.rb +14 -0
- data/lib/active_model_serializers/key_transform.rb +70 -0
- data/lib/active_model_serializers/logging.rb +4 -1
- data/lib/active_model_serializers/model.rb +11 -1
- data/lib/active_model_serializers/railtie.rb +9 -1
- data/lib/active_model_serializers/register_jsonapi_renderer.rb +64 -0
- data/lib/active_model_serializers/serializable_resource.rb +81 -0
- data/lib/active_model_serializers/serialization_context.rb +24 -2
- data/lib/active_model_serializers/test/schema.rb +2 -2
- data/lib/grape/formatters/active_model_serializers.rb +1 -1
- data/test/action_controller/adapter_selector_test.rb +1 -1
- data/test/action_controller/json_api/deserialization_test.rb +56 -3
- data/test/action_controller/json_api/errors_test.rb +41 -0
- data/test/action_controller/json_api/linked_test.rb +10 -9
- data/test/action_controller/json_api/pagination_test.rb +2 -2
- data/test/action_controller/json_api/transform_test.rb +180 -0
- data/test/action_controller/serialization_scope_name_test.rb +201 -35
- data/test/action_controller/serialization_test.rb +39 -7
- data/test/active_model_serializers/adapter_for_test.rb +208 -0
- data/test/active_model_serializers/cached_serializer_test.rb +80 -0
- data/test/active_model_serializers/fragment_cache_test.rb +34 -0
- data/test/active_model_serializers/json_pointer_test.rb +20 -0
- data/test/active_model_serializers/key_transform_test.rb +263 -0
- data/test/active_model_serializers/logging_test.rb +8 -8
- data/test/active_model_serializers/railtie_test_isolated.rb +6 -0
- data/test/active_model_serializers/serialization_context_test_isolated.rb +58 -0
- data/test/adapter/deprecation_test.rb +100 -0
- data/test/adapter/json/belongs_to_test.rb +32 -34
- data/test/adapter/json/collection_test.rb +73 -75
- data/test/adapter/json/has_many_test.rb +36 -38
- data/test/adapter/json/transform_test.rb +93 -0
- data/test/adapter/json_api/belongs_to_test.rb +127 -129
- data/test/adapter/json_api/collection_test.rb +80 -82
- data/test/adapter/json_api/errors_test.rb +78 -0
- data/test/adapter/json_api/fields_test.rb +68 -70
- data/test/adapter/json_api/has_many_embed_ids_test.rb +32 -34
- data/test/adapter/json_api/has_many_explicit_serializer_test.rb +75 -77
- data/test/adapter/json_api/has_many_test.rb +121 -123
- data/test/adapter/json_api/has_one_test.rb +59 -61
- data/test/adapter/json_api/json_api_test.rb +28 -30
- data/test/adapter/json_api/linked_test.rb +319 -321
- data/test/adapter/json_api/links_test.rb +75 -50
- data/test/adapter/json_api/pagination_links_test.rb +115 -82
- data/test/adapter/json_api/parse_test.rb +114 -116
- data/test/adapter/json_api/relationship_test.rb +161 -0
- data/test/adapter/json_api/relationships_test.rb +199 -0
- data/test/adapter/json_api/resource_identifier_test.rb +85 -0
- data/test/adapter/json_api/resource_meta_test.rb +100 -0
- data/test/adapter/json_api/toplevel_jsonapi_test.rb +61 -63
- data/test/adapter/json_api/transform_test.rb +500 -0
- data/test/adapter/json_api/type_test.rb +61 -0
- data/test/adapter/json_test.rb +35 -37
- data/test/adapter/null_test.rb +13 -15
- data/test/adapter/polymorphic_test.rb +72 -0
- data/test/adapter_test.rb +27 -29
- data/test/array_serializer_test.rb +7 -8
- data/test/benchmark/app.rb +65 -0
- data/test/benchmark/benchmarking_support.rb +67 -0
- data/test/benchmark/bm_caching.rb +117 -0
- data/test/benchmark/bm_transform.rb +34 -0
- data/test/benchmark/config.ru +3 -0
- data/test/benchmark/controllers.rb +77 -0
- data/test/benchmark/fixtures.rb +167 -0
- data/test/cache_test.rb +388 -0
- data/test/collection_serializer_test.rb +10 -0
- data/test/fixtures/active_record.rb +12 -0
- data/test/fixtures/poro.rb +28 -3
- data/test/grape_test.rb +5 -5
- data/test/lint_test.rb +9 -0
- data/test/serializable_resource_test.rb +59 -3
- data/test/serializers/associations_test.rb +8 -8
- data/test/serializers/attribute_test.rb +7 -7
- data/test/serializers/caching_configuration_test_isolated.rb +170 -0
- data/test/serializers/meta_test.rb +74 -6
- data/test/serializers/read_attribute_for_serialization_test.rb +79 -0
- data/test/serializers/serialization_test.rb +55 -0
- data/test/support/isolated_unit.rb +3 -0
- data/test/support/rails5_shims.rb +26 -8
- data/test/support/rails_app.rb +38 -18
- data/test/support/serialization_testing.rb +5 -5
- data/test/test_helper.rb +6 -10
- metadata +132 -37
- data/docs/DESIGN.textile +7 -1
- data/lib/active_model/serializer/adapter/cached_serializer.rb +0 -45
- data/lib/active_model/serializer/adapter/fragment_cache.rb +0 -111
- data/lib/active_model/serializer/adapter/json/fragment_cache.rb +0 -13
- data/lib/active_model/serializer/adapter/json_api/deserialization.rb +0 -207
- data/lib/active_model/serializer/adapter/json_api/fragment_cache.rb +0 -21
- data/lib/active_model/serializer/adapter/json_api/link.rb +0 -44
- data/lib/active_model/serializer/adapter/json_api/pagination_links.rb +0 -58
- data/test/active_model_serializers/serialization_context_test.rb +0 -18
- data/test/adapter/fragment_cache_test.rb +0 -38
- data/test/adapter/json_api/resource_type_config_test.rb +0 -71
- data/test/serializers/adapter_for_test.rb +0 -166
- data/test/serializers/cache_test.rb +0 -209
- data/test/support/simplecov.rb +0 -6
- data/test/support/stream_capture.rb +0 -50
- data/test/support/test_case.rb +0 -19
@@ -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,70 @@
|
|
1
|
+
require 'active_support/core_ext/hash/keys'
|
2
|
+
|
3
|
+
module ActiveModelSerializers
|
4
|
+
module KeyTransform
|
5
|
+
module_function
|
6
|
+
|
7
|
+
# Transforms values to UpperCamelCase or PascalCase.
|
8
|
+
#
|
9
|
+
# @example:
|
10
|
+
# "some_key" => "SomeKey",
|
11
|
+
# @see {https://github.com/rails/rails/blob/master/activesupport/lib/active_support/inflector/methods.rb#L66-L76 ActiveSupport::Inflector.camelize}
|
12
|
+
def camel(value)
|
13
|
+
case value
|
14
|
+
when Hash then value.deep_transform_keys! { |key| camel(key) }
|
15
|
+
when Symbol then camel(value.to_s).to_sym
|
16
|
+
when String then value.underscore.camelize
|
17
|
+
else value
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
# Transforms values to camelCase.
|
22
|
+
#
|
23
|
+
# @example:
|
24
|
+
# "some_key" => "someKey",
|
25
|
+
# @see {https://github.com/rails/rails/blob/master/activesupport/lib/active_support/inflector/methods.rb#L66-L76 ActiveSupport::Inflector.camelize}
|
26
|
+
def camel_lower(value)
|
27
|
+
case value
|
28
|
+
when Hash then value.deep_transform_keys! { |key| camel_lower(key) }
|
29
|
+
when Symbol then camel_lower(value.to_s).to_sym
|
30
|
+
when String then value.underscore.camelize(:lower)
|
31
|
+
else value
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
# Transforms values to dashed-case.
|
36
|
+
# This is the default case for the JsonApi adapter.
|
37
|
+
#
|
38
|
+
# @example:
|
39
|
+
# "some_key" => "some-key",
|
40
|
+
# @see {https://github.com/rails/rails/blob/master/activesupport/lib/active_support/inflector/methods.rb#L185-L187 ActiveSupport::Inflector.dasherize}
|
41
|
+
def dash(value)
|
42
|
+
case value
|
43
|
+
when Hash then value.deep_transform_keys! { |key| dash(key) }
|
44
|
+
when Symbol then dash(value.to_s).to_sym
|
45
|
+
when String then value.underscore.dasherize
|
46
|
+
else value
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
# Transforms values to underscore_case.
|
51
|
+
# This is the default case for deserialization in the JsonApi adapter.
|
52
|
+
#
|
53
|
+
# @example:
|
54
|
+
# "some-key" => "some_key",
|
55
|
+
# @see {https://github.com/rails/rails/blob/master/activesupport/lib/active_support/inflector/methods.rb#L89-L98 ActiveSupport::Inflector.underscore}
|
56
|
+
def underscore(value)
|
57
|
+
case value
|
58
|
+
when Hash then value.deep_transform_keys! { |key| underscore(key) }
|
59
|
+
when Symbol then underscore(value.to_s).to_sym
|
60
|
+
when String then value.underscore
|
61
|
+
else value
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
# Returns the value unaltered
|
66
|
+
def unaltered(value)
|
67
|
+
value
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
@@ -81,7 +81,10 @@ module ActiveModelSerializers
|
|
81
81
|
end
|
82
82
|
|
83
83
|
def notify_render_payload
|
84
|
-
{
|
84
|
+
{
|
85
|
+
serializer: serializer || ActiveModel::Serializer::Null,
|
86
|
+
adapter: adapter || ActiveModelSerializers::Adapter::Null
|
87
|
+
}
|
85
88
|
end
|
86
89
|
|
87
90
|
private
|
@@ -6,10 +6,11 @@ module ActiveModelSerializers
|
|
6
6
|
include ActiveModel::Model
|
7
7
|
include ActiveModel::Serializers::JSON
|
8
8
|
|
9
|
-
attr_reader :attributes
|
9
|
+
attr_reader :attributes, :errors
|
10
10
|
|
11
11
|
def initialize(attributes = {})
|
12
12
|
@attributes = attributes
|
13
|
+
@errors = ActiveModel::Errors.new(self)
|
13
14
|
super
|
14
15
|
end
|
15
16
|
|
@@ -35,5 +36,14 @@ module ActiveModelSerializers
|
|
35
36
|
attributes[key]
|
36
37
|
end
|
37
38
|
end
|
39
|
+
|
40
|
+
# The following methods are needed to be minimally implemented for ActiveModel::Errors
|
41
|
+
def self.human_attribute_name(attr, _options = {})
|
42
|
+
attr
|
43
|
+
end
|
44
|
+
|
45
|
+
def self.lookup_ancestors
|
46
|
+
[self]
|
47
|
+
end
|
38
48
|
end
|
39
49
|
end
|
@@ -15,13 +15,21 @@ module ActiveModelSerializers
|
|
15
15
|
end
|
16
16
|
end
|
17
17
|
|
18
|
+
initializer 'active_model_serializers.prepare_serialization_context' do
|
19
|
+
SerializationContext.url_helpers = Rails.application.routes.url_helpers
|
20
|
+
SerializationContext.default_url_options = Rails.application.routes.default_url_options
|
21
|
+
end
|
22
|
+
|
18
23
|
# This hook is run after the action_controller railtie has set the configuration
|
19
24
|
# based on the *environment* configuration and before any config/initializers are run
|
20
25
|
# and also before eager_loading (if enabled).
|
21
26
|
initializer 'active_model_serializers.set_configs', :after => 'action_controller.set_configs' do
|
22
27
|
ActiveModelSerializers.logger = Rails.configuration.action_controller.logger
|
23
|
-
ActiveModelSerializers.config.cache_store = Rails.configuration.action_controller.cache_store
|
24
28
|
ActiveModelSerializers.config.perform_caching = Rails.configuration.action_controller.perform_caching
|
29
|
+
# We want this hook to run after the config has been set, even if ActionController has already loaded.
|
30
|
+
ActiveSupport.on_load(:action_controller) do
|
31
|
+
ActiveModelSerializers.config.cache_store = ActionController::Base.cache_store
|
32
|
+
end
|
25
33
|
end
|
26
34
|
|
27
35
|
generators do |app|
|
@@ -0,0 +1,64 @@
|
|
1
|
+
# Based on discussion in https://github.com/rails/rails/pull/23712#issuecomment-184977238,
|
2
|
+
# the JSON API media type will have its own format/renderer.
|
3
|
+
#
|
4
|
+
# > We recommend the media type be registered on its own as jsonapi
|
5
|
+
# when a jsonapi Renderer and deserializer (Http::Parameters::DEFAULT_PARSERS) are added.
|
6
|
+
#
|
7
|
+
# Usage:
|
8
|
+
#
|
9
|
+
# ActiveSupport.on_load(:action_controller) do
|
10
|
+
# require 'active_model_serializers/register_jsonapi_renderer'
|
11
|
+
# end
|
12
|
+
#
|
13
|
+
# And then in controllers, use `render jsonapi: model` rather than `render json: model, adapter: :json_api`.
|
14
|
+
#
|
15
|
+
# For example, in a controller action, we can:
|
16
|
+
# respond_to do |format|
|
17
|
+
# format.jsonapi { render jsonapi: model }
|
18
|
+
# end
|
19
|
+
#
|
20
|
+
# or
|
21
|
+
#
|
22
|
+
# render jsonapi: model
|
23
|
+
#
|
24
|
+
# No wrapper format needed as it does not apply (i.e. no `wrap_parameters format: [jsonapi]`)
|
25
|
+
|
26
|
+
module ActiveModelSerializers::Jsonapi
|
27
|
+
MEDIA_TYPE = 'application/vnd.api+json'.freeze
|
28
|
+
HEADERS = {
|
29
|
+
response: { 'CONTENT_TYPE'.freeze => MEDIA_TYPE },
|
30
|
+
request: { 'ACCEPT'.freeze => MEDIA_TYPE }
|
31
|
+
}.freeze
|
32
|
+
module ControllerSupport
|
33
|
+
def serialize_jsonapi(json, options)
|
34
|
+
options[:adapter] = :json_api
|
35
|
+
options.fetch(:serialization_context) { options[:serialization_context] = ActiveModelSerializers::SerializationContext.new(request) }
|
36
|
+
get_serializer(json, options)
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
# actionpack/lib/action_dispatch/http/mime_types.rb
|
42
|
+
Mime::Type.register ActiveModelSerializers::Jsonapi::MEDIA_TYPE, :jsonapi
|
43
|
+
|
44
|
+
parsers = Rails::VERSION::MAJOR >= 5 ? ActionDispatch::Http::Parameters : ActionDispatch::ParamsParser
|
45
|
+
media_type = Mime::Type.lookup(ActiveModelSerializers::Jsonapi::MEDIA_TYPE)
|
46
|
+
|
47
|
+
# Proposal: should actually deserialize the JSON API params
|
48
|
+
# to the hash format expected by `ActiveModel::Serializers::JSON`
|
49
|
+
# actionpack/lib/action_dispatch/http/parameters.rb
|
50
|
+
parsers::DEFAULT_PARSERS[media_type] = lambda do |body|
|
51
|
+
data = JSON.parse(body)
|
52
|
+
data = { :_json => data } unless data.is_a?(Hash)
|
53
|
+
data.with_indifferent_access
|
54
|
+
end
|
55
|
+
|
56
|
+
# ref https://github.com/rails/rails/pull/21496
|
57
|
+
ActionController::Renderers.add :jsonapi do |json, options|
|
58
|
+
json = serialize_jsonapi(json, options).to_json(options) unless json.is_a?(String)
|
59
|
+
self.content_type ||= media_type
|
60
|
+
headers.merge! ActiveModelSerializers::Jsonapi::HEADERS[:response]
|
61
|
+
self.response_body = json
|
62
|
+
end
|
63
|
+
|
64
|
+
ActionController::Base.send :include, ActiveModelSerializers::Jsonapi::ControllerSupport
|
@@ -0,0 +1,81 @@
|
|
1
|
+
require 'set'
|
2
|
+
|
3
|
+
module ActiveModelSerializers
|
4
|
+
class SerializableResource
|
5
|
+
ADAPTER_OPTION_KEYS = Set.new([:include, :fields, :adapter, :meta, :meta_key, :links])
|
6
|
+
include ActiveModelSerializers::Logging
|
7
|
+
|
8
|
+
delegate :serializable_hash, :as_json, :to_json, to: :adapter
|
9
|
+
notify :serializable_hash, :render
|
10
|
+
notify :as_json, :render
|
11
|
+
notify :to_json, :render
|
12
|
+
|
13
|
+
# Primary interface to composing a resource with a serializer and adapter.
|
14
|
+
# @return the serializable_resource, ready for #as_json/#to_json/#serializable_hash.
|
15
|
+
def initialize(resource, options = {})
|
16
|
+
@resource = resource
|
17
|
+
@adapter_opts, @serializer_opts =
|
18
|
+
options.partition { |k, _| ADAPTER_OPTION_KEYS.include? k }.map { |h| Hash[h] }
|
19
|
+
end
|
20
|
+
|
21
|
+
def serialization_scope=(scope)
|
22
|
+
serializer_opts[:scope] = scope
|
23
|
+
end
|
24
|
+
|
25
|
+
def serialization_scope
|
26
|
+
serializer_opts[:scope]
|
27
|
+
end
|
28
|
+
|
29
|
+
def serialization_scope_name=(scope_name)
|
30
|
+
serializer_opts[:scope_name] = scope_name
|
31
|
+
end
|
32
|
+
|
33
|
+
# NOTE: if no adapter is available, returns the resource itself. (i.e. adapter is a no-op)
|
34
|
+
def adapter
|
35
|
+
@adapter ||= find_adapter
|
36
|
+
end
|
37
|
+
alias adapter_instance adapter
|
38
|
+
|
39
|
+
def find_adapter
|
40
|
+
return resource unless serializer?
|
41
|
+
ActiveModelSerializers::Adapter.create(serializer_instance, adapter_opts)
|
42
|
+
rescue ActiveModel::Serializer::CollectionSerializer::NoSerializerError
|
43
|
+
resource
|
44
|
+
end
|
45
|
+
|
46
|
+
def serializer_instance
|
47
|
+
@serializer_instance ||= serializer.new(resource, serializer_opts)
|
48
|
+
end
|
49
|
+
|
50
|
+
# Get serializer either explicitly :serializer or implicitly from resource
|
51
|
+
# Remove :serializer key from serializer_opts
|
52
|
+
# Replace :serializer key with :each_serializer if present
|
53
|
+
def serializer
|
54
|
+
@serializer ||=
|
55
|
+
begin
|
56
|
+
@serializer = serializer_opts.delete(:serializer)
|
57
|
+
@serializer ||= ActiveModel::Serializer.serializer_for(resource)
|
58
|
+
|
59
|
+
if serializer_opts.key?(:each_serializer)
|
60
|
+
serializer_opts[:serializer] = serializer_opts.delete(:each_serializer)
|
61
|
+
end
|
62
|
+
@serializer
|
63
|
+
end
|
64
|
+
end
|
65
|
+
alias serializer_class serializer
|
66
|
+
|
67
|
+
# True when no explicit adapter given, or explicit appear is truthy (non-nil)
|
68
|
+
# False when explicit adapter is falsy (nil or false)
|
69
|
+
def use_adapter?
|
70
|
+
!(adapter_opts.key?(:adapter) && !adapter_opts[:adapter])
|
71
|
+
end
|
72
|
+
|
73
|
+
def serializer?
|
74
|
+
use_adapter? && !serializer.nil?
|
75
|
+
end
|
76
|
+
|
77
|
+
protected
|
78
|
+
|
79
|
+
attr_reader :resource, :adapter_opts, :serializer_opts
|
80
|
+
end
|
81
|
+
end
|
@@ -1,10 +1,32 @@
|
|
1
1
|
module ActiveModelSerializers
|
2
2
|
class SerializationContext
|
3
|
-
|
3
|
+
class << self
|
4
|
+
attr_writer :url_helpers, :default_url_options
|
5
|
+
def url_helpers
|
6
|
+
@url_helpers ||= Module.new
|
7
|
+
end
|
4
8
|
|
5
|
-
|
9
|
+
def default_url_options
|
10
|
+
@default_url_options ||= {}
|
11
|
+
end
|
12
|
+
end
|
13
|
+
module UrlHelpers
|
14
|
+
def self.included(base)
|
15
|
+
base.send(:include, SerializationContext.url_helpers)
|
16
|
+
end
|
17
|
+
|
18
|
+
def default_url_options
|
19
|
+
SerializationContext.default_url_options
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
attr_reader :request_url, :query_parameters, :key_transform
|
24
|
+
|
25
|
+
def initialize(request, options = {})
|
6
26
|
@request_url = request.original_url[/\A[^?]+/]
|
7
27
|
@query_parameters = request.query_parameters
|
28
|
+
@url_helpers = options.delete(:url_helpers) || self.class.url_helpers
|
29
|
+
@default_url_options = options.delete(:default_url_options) || self.class.default_url_options
|
8
30
|
end
|
9
31
|
end
|
10
32
|
end
|
@@ -2,8 +2,8 @@ module ActiveModelSerializers
|
|
2
2
|
module Test
|
3
3
|
module Schema
|
4
4
|
# A Minitest Assertion that test the response is valid against a schema.
|
5
|
-
# @
|
6
|
-
# @
|
5
|
+
# @param schema_path [String] a custom schema path
|
6
|
+
# @param message [String] a custom error message
|
7
7
|
# @return [Boolean] true when the response is valid
|
8
8
|
# @return [Minitest::Assertion] when the response is invalid
|
9
9
|
# @example
|
@@ -8,7 +8,7 @@ module Grape
|
|
8
8
|
def self.call(resource, env)
|
9
9
|
serializer_options = {}
|
10
10
|
serializer_options.merge!(env[:active_model_serializer_options]) if env[:active_model_serializer_options]
|
11
|
-
|
11
|
+
::ActiveModelSerializers::SerializableResource.new(resource, serializer_options).to_json
|
12
12
|
end
|
13
13
|
end
|
14
14
|
end
|
@@ -9,10 +9,50 @@ module ActionController
|
|
9
9
|
parsed_hash = ActiveModelSerializers::Deserialization.jsonapi_parse(params)
|
10
10
|
render json: parsed_hash
|
11
11
|
end
|
12
|
+
|
13
|
+
def render_polymorphic_parsed_payload
|
14
|
+
parsed_hash = ActiveModelSerializers::Deserialization.jsonapi_parse(
|
15
|
+
params,
|
16
|
+
polymorphic: [:restriction_for, :restricted_to]
|
17
|
+
)
|
18
|
+
render json: parsed_hash
|
19
|
+
end
|
12
20
|
end
|
13
21
|
|
14
22
|
tests DeserializationTestController
|
15
23
|
|
24
|
+
def test_deserialization_of_relationship_only_object
|
25
|
+
hash = {
|
26
|
+
'data' => {
|
27
|
+
'type' => 'restraints',
|
28
|
+
'relationships' => {
|
29
|
+
'restriction_for' => {
|
30
|
+
'data' => {
|
31
|
+
'type' => 'discounts',
|
32
|
+
'id' => '67'
|
33
|
+
}
|
34
|
+
},
|
35
|
+
'restricted_to' => {
|
36
|
+
'data' => nil
|
37
|
+
}
|
38
|
+
}
|
39
|
+
},
|
40
|
+
'restraint' => {}
|
41
|
+
}
|
42
|
+
|
43
|
+
post :render_polymorphic_parsed_payload, params: hash
|
44
|
+
|
45
|
+
response = JSON.parse(@response.body)
|
46
|
+
expected = {
|
47
|
+
'restriction_for_id' => '67',
|
48
|
+
'restriction_for_type' => 'discounts',
|
49
|
+
'restricted_to_id' => nil,
|
50
|
+
'restricted_to_type' => nil
|
51
|
+
}
|
52
|
+
|
53
|
+
assert_equal(expected, response)
|
54
|
+
end
|
55
|
+
|
16
56
|
def test_deserialization
|
17
57
|
hash = {
|
18
58
|
'data' => {
|
@@ -20,7 +60,10 @@ module ActionController
|
|
20
60
|
'id' => 'zorglub',
|
21
61
|
'attributes' => {
|
22
62
|
'title' => 'Ember Hamster',
|
23
|
-
'src' => 'http://example.com/images/productivity.png'
|
63
|
+
'src' => 'http://example.com/images/productivity.png',
|
64
|
+
'image-width' => '200',
|
65
|
+
'imageHeight' => '200',
|
66
|
+
'ImageSize' => '1024'
|
24
67
|
},
|
25
68
|
'relationships' => {
|
26
69
|
'author' => {
|
@@ -34,21 +77,31 @@ module ActionController
|
|
34
77
|
{ 'type' => 'comments', 'id' => '1' },
|
35
78
|
{ 'type' => 'comments', 'id' => '2' }
|
36
79
|
]
|
80
|
+
},
|
81
|
+
'related-images' => {
|
82
|
+
'data' => [
|
83
|
+
{ 'type' => 'image', 'id' => '7' },
|
84
|
+
{ 'type' => 'image', 'id' => '8' }
|
85
|
+
]
|
37
86
|
}
|
38
87
|
}
|
39
88
|
}
|
40
89
|
}
|
41
90
|
|
42
|
-
post :render_parsed_payload, hash
|
91
|
+
post :render_parsed_payload, params: hash
|
43
92
|
|
44
93
|
response = JSON.parse(@response.body)
|
45
94
|
expected = {
|
46
95
|
'id' => 'zorglub',
|
47
96
|
'title' => 'Ember Hamster',
|
48
97
|
'src' => 'http://example.com/images/productivity.png',
|
98
|
+
'image_width' => '200',
|
99
|
+
'image_height' => '200',
|
100
|
+
'image_size' => '1024',
|
49
101
|
'author_id' => nil,
|
50
102
|
'photographer_id' => '9',
|
51
|
-
'comment_ids' => %w(1 2)
|
103
|
+
'comment_ids' => %w(1 2),
|
104
|
+
'related_image_ids' => %w(7 8)
|
52
105
|
}
|
53
106
|
|
54
107
|
assert_equal(expected, response)
|