active_model_serializers 0.10.0 → 0.10.11
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/CHANGELOG.md +227 -2
- data/README.md +171 -33
- data/lib/action_controller/serialization.rb +23 -3
- data/lib/active_model/serializable_resource.rb +2 -0
- data/lib/active_model/serializer/adapter/attributes.rb +2 -0
- data/lib/active_model/serializer/adapter/base.rb +4 -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/adapter.rb +2 -0
- data/lib/active_model/serializer/array_serializer.rb +10 -5
- data/lib/active_model/serializer/association.rb +64 -10
- data/lib/active_model/serializer/attribute.rb +2 -0
- data/lib/active_model/serializer/belongs_to_reflection.rb +6 -3
- data/lib/active_model/serializer/collection_serializer.rb +39 -13
- data/lib/active_model/serializer/{caching.rb → concerns/caching.rb} +89 -117
- data/lib/active_model/serializer/error_serializer.rb +13 -7
- data/lib/active_model/serializer/errors_serializer.rb +27 -20
- 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 +5 -3
- data/lib/active_model/serializer/has_one_reflection.rb +3 -4
- 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 +136 -130
- data/lib/active_model/serializer/null.rb +2 -0
- data/lib/active_model/serializer/reflection.rb +130 -65
- data/lib/active_model/serializer/version.rb +3 -1
- data/lib/active_model/serializer.rb +319 -86
- data/lib/active_model_serializers/adapter/attributes.rb +17 -57
- data/lib/active_model_serializers/adapter/base.rb +41 -39
- data/lib/active_model_serializers/adapter/json.rb +2 -0
- 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 +3 -1
- data/lib/active_model_serializers/adapter/json_api/meta.rb +2 -0
- data/lib/active_model_serializers/adapter/json_api/pagination_links.rb +49 -21
- data/lib/active_model_serializers/adapter/json_api/relationship.rb +77 -23
- data/lib/active_model_serializers/adapter/json_api/resource_identifier.rb +41 -10
- data/lib/active_model_serializers/adapter/json_api.rb +84 -65
- data/lib/active_model_serializers/adapter/null.rb +2 -0
- data/lib/active_model_serializers/adapter.rb +9 -1
- data/lib/active_model_serializers/callbacks.rb +2 -0
- data/lib/active_model_serializers/deprecate.rb +3 -2
- data/lib/active_model_serializers/deserialization.rb +4 -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 +82 -0
- data/lib/active_model_serializers/model/caching.rb +25 -0
- data/lib/active_model_serializers/model.rb +111 -28
- data/lib/active_model_serializers/railtie.rb +7 -1
- data/lib/active_model_serializers/register_jsonapi_renderer.rb +46 -31
- data/lib/active_model_serializers/serializable_resource.rb +10 -7
- data/lib/active_model_serializers/serialization_context.rb +12 -3
- data/lib/active_model_serializers/test/schema.rb +4 -2
- data/lib/active_model_serializers/test/serializer.rb +2 -0
- data/lib/active_model_serializers/test.rb +2 -0
- data/lib/active_model_serializers.rb +35 -10
- data/lib/generators/rails/resource_override.rb +3 -1
- data/lib/generators/rails/serializer_generator.rb +6 -4
- data/lib/grape/active_model_serializers.rb +9 -5
- data/lib/grape/formatters/active_model_serializers.rb +21 -2
- data/lib/grape/helpers/active_model_serializers.rb +3 -0
- data/lib/tasks/rubocop.rake +55 -0
- metadata +110 -276
- data/.github/ISSUE_TEMPLATE.md +0 -29
- data/.github/PULL_REQUEST_TEMPLATE.md +0 -15
- data/.gitignore +0 -35
- data/.rubocop.yml +0 -104
- data/.rubocop_todo.yml +0 -167
- data/.simplecov +0 -110
- data/.travis.yml +0 -43
- data/CONTRIBUTING.md +0 -105
- data/Gemfile +0 -53
- data/Rakefile +0 -103
- data/active_model_serializers.gemspec +0 -66
- 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 -126
- data/docs/README.md +0 -40
- data/docs/STYLE.md +0 -58
- data/docs/general/adapters.md +0 -245
- data/docs/general/caching.md +0 -52
- data/docs/general/configuration_options.md +0 -100
- data/docs/general/deserialization.md +0 -100
- 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 -255
- data/docs/general/serializers.md +0 -372
- data/docs/how-open-source-maintained.jpg +0 -0
- data/docs/howto/add_pagination_links.md +0 -139
- data/docs/howto/add_root_key.md +0 -51
- data/docs/howto/outside_controller_use.md +0 -58
- data/docs/howto/passing_arbitrary_options.md +0 -27
- data/docs/howto/serialize_poro.md +0 -32
- data/docs/howto/test.md +0 -152
- data/docs/integrations/ember-and-json-api.md +0 -112
- data/docs/integrations/grape.md +0 -19
- data/docs/jsonapi/errors.md +0 -56
- data/docs/jsonapi/schema/schema.json +0 -366
- data/docs/jsonapi/schema.md +0 -151
- data/docs/rfcs/0000-namespace.md +0 -106
- data/docs/rfcs/template.md +0 -15
- data/lib/active_model/serializer/associations.rb +0 -100
- data/lib/active_model/serializer/attributes.rb +0 -82
- data/lib/active_model/serializer/collection_reflection.rb +0 -7
- data/lib/active_model/serializer/configuration.rb +0 -35
- data/lib/active_model/serializer/include_tree.rb +0 -111
- data/lib/active_model/serializer/links.rb +0 -35
- data/lib/active_model/serializer/meta.rb +0 -29
- data/lib/active_model/serializer/singular_reflection.rb +0 -7
- data/lib/active_model/serializer/type.rb +0 -25
- data/lib/active_model_serializers/key_transform.rb +0 -70
- data/test/action_controller/adapter_selector_test.rb +0 -53
- data/test/action_controller/explicit_serializer_test.rb +0 -134
- data/test/action_controller/json/include_test.rb +0 -167
- data/test/action_controller/json_api/deserialization_test.rb +0 -112
- data/test/action_controller/json_api/errors_test.rb +0 -41
- data/test/action_controller/json_api/linked_test.rb +0 -197
- 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/serialization_scope_name_test.rb +0 -229
- data/test/action_controller/serialization_test.rb +0 -469
- data/test/active_model_serializers/adapter_for_test.rb +0 -208
- data/test/active_model_serializers/json_pointer_test.rb +0 -20
- data/test/active_model_serializers/key_transform_test.rb +0 -263
- data/test/active_model_serializers/logging_test.rb +0 -77
- data/test/active_model_serializers/model_test.rb +0 -9
- data/test/active_model_serializers/railtie_test_isolated.rb +0 -63
- data/test/active_model_serializers/serialization_context_test_isolated.rb +0 -58
- 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/deprecation_test.rb +0 -100
- data/test/adapter/json/belongs_to_test.rb +0 -45
- data/test/adapter/json/collection_test.rb +0 -90
- 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 -95
- data/test/adapter/json_api/errors_test.rb +0 -78
- data/test/adapter/json_api/fields_test.rb +0 -87
- 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 -144
- data/test/adapter/json_api/has_one_test.rb +0 -80
- data/test/adapter/json_api/json_api_test.rb +0 -35
- data/test/adapter/json_api/linked_test.rb +0 -392
- data/test/adapter/json_api/links_test.rb +0 -93
- data/test/adapter/json_api/pagination_links_test.rb +0 -166
- data/test/adapter/json_api/parse_test.rb +0 -137
- data/test/adapter/json_api/relationship_test.rb +0 -161
- data/test/adapter/json_api/relationships_test.rb +0 -199
- data/test/adapter/json_api/resource_identifier_test.rb +0 -85
- 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 -502
- data/test/adapter/json_api/type_test.rb +0 -61
- data/test/adapter/json_test.rb +0 -45
- data/test/adapter/null_test.rb +0 -23
- 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_caching.rb +0 -119
- data/test/benchmark/bm_transform.rb +0 -34
- data/test/benchmark/config.ru +0 -3
- data/test/benchmark/controllers.rb +0 -84
- data/test/benchmark/fixtures.rb +0 -219
- data/test/cache_test.rb +0 -485
- data/test/collection_serializer_test.rb +0 -110
- data/test/fixtures/active_record.rb +0 -78
- data/test/fixtures/poro.rb +0 -282
- data/test/generators/scaffold_controller_generator_test.rb +0 -24
- data/test/generators/serializer_generator_test.rb +0 -57
- data/test/grape_test.rb +0 -82
- data/test/include_tree/from_include_args_test.rb +0 -26
- data/test/include_tree/from_string_test.rb +0 -94
- data/test/include_tree/include_args_to_hash_test.rb +0 -64
- data/test/lint_test.rb +0 -49
- data/test/logger_test.rb +0 -18
- data/test/poro_test.rb +0 -9
- data/test/serializable_resource_test.rb +0 -83
- data/test/serializers/association_macros_test.rb +0 -36
- data/test/serializers/associations_test.rb +0 -295
- 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 -196
- 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 -134
- data/test/support/custom_schemas/active_model_serializers/test/schema_test/my/index.json +0 -6
- data/test/support/isolated_unit.rb +0 -79
- data/test/support/rails5_shims.rb +0 -47
- data/test/support/rails_app.rb +0 -45
- 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 -53
- data/test/test_helper.rb +0 -57
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
# Based on discussion in https://github.com/rails/rails/pull/23712#issuecomment-184977238,
|
2
4
|
# the JSON API media type will have its own format/renderer.
|
3
5
|
#
|
@@ -22,43 +24,56 @@
|
|
22
24
|
# render jsonapi: model
|
23
25
|
#
|
24
26
|
# No wrapper format needed as it does not apply (i.e. no `wrap_parameters format: [jsonapi]`)
|
27
|
+
module ActiveModelSerializers
|
28
|
+
module Jsonapi
|
29
|
+
MEDIA_TYPE = 'application/vnd.api+json'.freeze
|
30
|
+
HEADERS = {
|
31
|
+
response: { 'CONTENT_TYPE'.freeze => MEDIA_TYPE },
|
32
|
+
request: { 'ACCEPT'.freeze => MEDIA_TYPE }
|
33
|
+
}.freeze
|
25
34
|
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
options.fetch(:serialization_context) { options[:serialization_context] = ActiveModelSerializers::SerializationContext.new(request) }
|
36
|
-
get_serializer(json, options)
|
37
|
-
end
|
38
|
-
end
|
39
|
-
end
|
35
|
+
def self.install
|
36
|
+
# actionpack/lib/action_dispatch/http/mime_types.rb
|
37
|
+
Mime::Type.register MEDIA_TYPE, :jsonapi
|
38
|
+
|
39
|
+
if Rails::VERSION::MAJOR >= 5
|
40
|
+
ActionDispatch::Request.parameter_parsers[:jsonapi] = parser
|
41
|
+
else
|
42
|
+
ActionDispatch::ParamsParser::DEFAULT_PARSERS[Mime[:jsonapi]] = parser
|
43
|
+
end
|
40
44
|
|
41
|
-
#
|
42
|
-
|
45
|
+
# ref https://github.com/rails/rails/pull/21496
|
46
|
+
ActionController::Renderers.add :jsonapi do |json, options|
|
47
|
+
json = serialize_jsonapi(json, options).to_json(options) unless json.is_a?(String)
|
48
|
+
self.content_type ||= Mime[:jsonapi]
|
49
|
+
self.response_body = json
|
50
|
+
end
|
51
|
+
end
|
43
52
|
|
44
|
-
|
45
|
-
|
53
|
+
# Proposal: should actually deserialize the JSON API params
|
54
|
+
# to the hash format expected by `ActiveModel::Serializers::JSON`
|
55
|
+
# actionpack/lib/action_dispatch/http/parameters.rb
|
56
|
+
def self.parser
|
57
|
+
lambda do |body|
|
58
|
+
data = JSON.parse(body)
|
59
|
+
data = { _json: data } unless data.is_a?(Hash)
|
60
|
+
data.with_indifferent_access
|
61
|
+
end
|
62
|
+
end
|
46
63
|
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
64
|
+
module ControllerSupport
|
65
|
+
def serialize_jsonapi(json, options)
|
66
|
+
options[:adapter] = :json_api
|
67
|
+
options.fetch(:serialization_context) do
|
68
|
+
options[:serialization_context] = ActiveModelSerializers::SerializationContext.new(request)
|
69
|
+
end
|
70
|
+
get_serializer(json, options)
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
54
74
|
end
|
55
75
|
|
56
|
-
|
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
|
-
self.response_body = json
|
61
|
-
end
|
76
|
+
ActiveModelSerializers::Jsonapi.install
|
62
77
|
|
63
78
|
ActiveSupport.on_load(:action_controller) do
|
64
79
|
include ActiveModelSerializers::Jsonapi::ControllerSupport
|
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'set'
|
2
4
|
|
3
5
|
module ActiveModelSerializers
|
@@ -14,8 +16,8 @@ module ActiveModelSerializers
|
|
14
16
|
# @return the serializable_resource, ready for #as_json/#to_json/#serializable_hash.
|
15
17
|
def initialize(resource, options = {})
|
16
18
|
@resource = resource
|
17
|
-
@adapter_opts,
|
18
|
-
|
19
|
+
@adapter_opts = options.select { |k, _| ADAPTER_OPTION_KEYS.include? k }
|
20
|
+
@serializer_opts = options.reject { |k, _| ADAPTER_OPTION_KEYS.include? k }
|
19
21
|
end
|
20
22
|
|
21
23
|
def serialization_scope=(scope)
|
@@ -38,9 +40,10 @@ module ActiveModelSerializers
|
|
38
40
|
|
39
41
|
def find_adapter
|
40
42
|
return resource unless serializer?
|
41
|
-
|
42
|
-
|
43
|
-
|
43
|
+
adapter = catch :no_serializer do
|
44
|
+
ActiveModelSerializers::Adapter.create(serializer_instance, adapter_opts)
|
45
|
+
end
|
46
|
+
adapter || resource
|
44
47
|
end
|
45
48
|
|
46
49
|
def serializer_instance
|
@@ -49,12 +52,12 @@ module ActiveModelSerializers
|
|
49
52
|
|
50
53
|
# Get serializer either explicitly :serializer or implicitly from resource
|
51
54
|
# Remove :serializer key from serializer_opts
|
52
|
-
#
|
55
|
+
# Remove :each_serializer if present and set as :serializer key
|
53
56
|
def serializer
|
54
57
|
@serializer ||=
|
55
58
|
begin
|
56
59
|
@serializer = serializer_opts.delete(:serializer)
|
57
|
-
@serializer ||= ActiveModel::Serializer.serializer_for(resource)
|
60
|
+
@serializer ||= ActiveModel::Serializer.serializer_for(resource, serializer_opts)
|
58
61
|
|
59
62
|
if serializer_opts.key?(:each_serializer)
|
60
63
|
serializer_opts[:serializer] = serializer_opts.delete(:each_serializer)
|
@@ -1,3 +1,6 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'active_support/core_ext/array/extract_options'
|
1
4
|
module ActiveModelSerializers
|
2
5
|
class SerializationContext
|
3
6
|
class << self
|
@@ -22,9 +25,15 @@ module ActiveModelSerializers
|
|
22
25
|
|
23
26
|
attr_reader :request_url, :query_parameters, :key_transform
|
24
27
|
|
25
|
-
def initialize(
|
26
|
-
|
27
|
-
|
28
|
+
def initialize(*args)
|
29
|
+
options = args.extract_options!
|
30
|
+
if args.size == 1
|
31
|
+
request = args.pop
|
32
|
+
options[:request_url] = request.original_url[/\A[^?]+/]
|
33
|
+
options[:query_parameters] = request.query_parameters
|
34
|
+
end
|
35
|
+
@request_url = options.delete(:request_url)
|
36
|
+
@query_parameters = options.delete(:query_parameters)
|
28
37
|
@url_helpers = options.delete(:url_helpers) || self.class.url_helpers
|
29
38
|
@default_url_options = options.delete(:default_url_options) || self.class.default_url_options
|
30
39
|
end
|
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module ActiveModelSerializers
|
2
4
|
module Test
|
3
5
|
module Schema
|
@@ -60,11 +62,11 @@ module ActiveModelSerializers
|
|
60
62
|
attr_reader :document_store
|
61
63
|
|
62
64
|
def controller_path
|
63
|
-
request.filtered_parameters[:controller]
|
65
|
+
request.filtered_parameters.with_indifferent_access[:controller]
|
64
66
|
end
|
65
67
|
|
66
68
|
def action
|
67
|
-
request.filtered_parameters[:action]
|
69
|
+
request.filtered_parameters.with_indifferent_access[:action]
|
68
70
|
end
|
69
71
|
|
70
72
|
def schema_directory
|
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'active_model'
|
2
4
|
require 'active_support'
|
3
5
|
require 'active_support/core_ext/object/with_options'
|
@@ -5,15 +7,19 @@ require 'active_support/core_ext/string/inflections'
|
|
5
7
|
require 'active_support/json'
|
6
8
|
module ActiveModelSerializers
|
7
9
|
extend ActiveSupport::Autoload
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
10
|
+
eager_autoload do
|
11
|
+
autoload :Model
|
12
|
+
autoload :Callbacks
|
13
|
+
autoload :SerializableResource
|
14
|
+
autoload :SerializationContext
|
15
|
+
autoload :Logging
|
16
|
+
autoload :Test
|
17
|
+
autoload :Adapter
|
18
|
+
autoload :JsonPointer
|
19
|
+
autoload :Deprecate
|
20
|
+
autoload :LookupChain
|
21
|
+
autoload :Deserialization
|
22
|
+
end
|
17
23
|
|
18
24
|
class << self; attr_accessor :logger; end
|
19
25
|
self.logger = ActiveSupport::TaggedLogging.new(ActiveSupport::Logger.new(STDOUT))
|
@@ -31,8 +37,27 @@ module ActiveModelSerializers
|
|
31
37
|
[file, lineno]
|
32
38
|
end
|
33
39
|
|
40
|
+
# Memoized default include directive
|
41
|
+
# @return [JSONAPI::IncludeDirective]
|
42
|
+
def self.default_include_directive
|
43
|
+
@default_include_directive ||= JSONAPI::IncludeDirective.new(config.default_includes, allow_wildcard: true)
|
44
|
+
end
|
45
|
+
|
46
|
+
def self.silence_warnings
|
47
|
+
original_verbose = $VERBOSE
|
48
|
+
$VERBOSE = nil
|
49
|
+
yield
|
50
|
+
ensure
|
51
|
+
$VERBOSE = original_verbose
|
52
|
+
end
|
53
|
+
|
54
|
+
def self.eager_load!
|
55
|
+
super
|
56
|
+
ActiveModel::Serializer.eager_load!
|
57
|
+
end
|
58
|
+
|
34
59
|
require 'active_model/serializer/version'
|
35
60
|
require 'active_model/serializer'
|
36
61
|
require 'active_model/serializable_resource'
|
37
|
-
require 'active_model_serializers/railtie' if defined?(::Rails)
|
62
|
+
require 'active_model_serializers/railtie' if defined?(::Rails::Railtie)
|
38
63
|
end
|
@@ -1,10 +1,12 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'rails/generators'
|
2
4
|
require 'rails/generators/rails/resource/resource_generator'
|
3
5
|
|
4
6
|
module Rails
|
5
7
|
module Generators
|
6
8
|
class ResourceGenerator
|
7
|
-
hook_for :serializer, default: true,
|
9
|
+
hook_for :serializer, default: true, type: :boolean
|
8
10
|
end
|
9
11
|
end
|
10
12
|
end
|
@@ -1,12 +1,14 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module Rails
|
2
4
|
module Generators
|
3
5
|
class SerializerGenerator < NamedBase
|
4
6
|
source_root File.expand_path('../templates', __FILE__)
|
5
|
-
check_class_collision :
|
7
|
+
check_class_collision suffix: 'Serializer'
|
6
8
|
|
7
|
-
argument :attributes, :
|
9
|
+
argument :attributes, type: :array, default: [], banner: 'field:type field:type'
|
8
10
|
|
9
|
-
class_option :parent, :
|
11
|
+
class_option :parent, type: :string, desc: 'The parent class for the generated serializer'
|
10
12
|
|
11
13
|
def create_serializer_file
|
12
14
|
template 'serializer.rb.erb', File.join('app/serializers', class_path, "#{file_name}_serializer.rb")
|
@@ -25,7 +27,7 @@ module Rails
|
|
25
27
|
def parent_class_name
|
26
28
|
if options[:parent]
|
27
29
|
options[:parent]
|
28
|
-
elsif
|
30
|
+
elsif 'ApplicationSerializer'.safe_constantize
|
29
31
|
'ApplicationSerializer'
|
30
32
|
else
|
31
33
|
'ActiveModel::Serializer'
|
@@ -1,14 +1,18 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
# To add Grape support, require 'grape/active_model_serializers' in the base of your Grape endpoints
|
2
4
|
# Then add 'include Grape::ActiveModelSerializers' to enable the formatter and helpers
|
3
5
|
require 'active_model_serializers'
|
4
6
|
require 'grape/formatters/active_model_serializers'
|
5
7
|
require 'grape/helpers/active_model_serializers'
|
6
8
|
|
7
|
-
module Grape
|
8
|
-
|
9
|
+
module Grape
|
10
|
+
module ActiveModelSerializers
|
11
|
+
extend ActiveSupport::Concern
|
9
12
|
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
+
included do
|
14
|
+
formatter :json, Grape::Formatters::ActiveModelSerializers
|
15
|
+
helpers Grape::Helpers::ActiveModelSerializers
|
16
|
+
end
|
13
17
|
end
|
14
18
|
end
|
@@ -1,15 +1,34 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
# A Grape response formatter that can be used as 'formatter :json, Grape::Formatters::ActiveModelSerializers'
|
2
4
|
#
|
3
5
|
# Serializer options can be passed as a hash from your Grape endpoint using env[:active_model_serializer_options],
|
4
6
|
# or better yet user the render helper in Grape::Helpers::ActiveModelSerializers
|
7
|
+
|
8
|
+
require 'active_model_serializers/serialization_context'
|
9
|
+
|
5
10
|
module Grape
|
6
11
|
module Formatters
|
7
12
|
module ActiveModelSerializers
|
8
13
|
def self.call(resource, env)
|
9
|
-
serializer_options =
|
10
|
-
serializer_options.merge!(env[:active_model_serializer_options]) if env[:active_model_serializer_options]
|
14
|
+
serializer_options = build_serializer_options(env)
|
11
15
|
::ActiveModelSerializers::SerializableResource.new(resource, serializer_options).to_json
|
12
16
|
end
|
17
|
+
|
18
|
+
def self.build_serializer_options(env)
|
19
|
+
ams_options = env[:active_model_serializer_options] || {}
|
20
|
+
|
21
|
+
# Add serialization context
|
22
|
+
ams_options.fetch(:serialization_context) do
|
23
|
+
request = env['grape.request']
|
24
|
+
ams_options[:serialization_context] = ::ActiveModelSerializers::SerializationContext.new(
|
25
|
+
request_url: request.url[/\A[^?]+/],
|
26
|
+
query_parameters: request.params
|
27
|
+
)
|
28
|
+
end
|
29
|
+
|
30
|
+
ams_options
|
31
|
+
end
|
13
32
|
end
|
14
33
|
end
|
15
34
|
end
|
@@ -0,0 +1,55 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
begin
|
4
|
+
require 'rubocop'
|
5
|
+
require 'rubocop/rake_task'
|
6
|
+
rescue LoadError # rubocop:disable Lint/HandleExceptions
|
7
|
+
else
|
8
|
+
require 'rbconfig'
|
9
|
+
# https://github.com/bundler/bundler/blob/1b3eb2465a/lib/bundler/constants.rb#L2
|
10
|
+
windows_platforms = /(msdos|mswin|djgpp|mingw)/
|
11
|
+
if RbConfig::CONFIG['host_os'] =~ windows_platforms
|
12
|
+
desc 'No-op rubocop on Windows-- unsupported platform'
|
13
|
+
task :rubocop do
|
14
|
+
puts 'Skipping rubocop on Windows'
|
15
|
+
end
|
16
|
+
elsif defined?(::Rubinius)
|
17
|
+
desc 'No-op rubocop to avoid rbx segfault'
|
18
|
+
task :rubocop do
|
19
|
+
puts 'Skipping rubocop on rbx due to segfault'
|
20
|
+
puts 'https://github.com/rubinius/rubinius/issues/3499'
|
21
|
+
end
|
22
|
+
else
|
23
|
+
Rake::Task[:rubocop].clear if Rake::Task.task_defined?(:rubocop)
|
24
|
+
patterns = [
|
25
|
+
'Gemfile',
|
26
|
+
'Rakefile',
|
27
|
+
'lib/**/*.{rb,rake}',
|
28
|
+
'config/**/*.rb',
|
29
|
+
'app/**/*.rb',
|
30
|
+
'test/**/*.rb'
|
31
|
+
]
|
32
|
+
desc 'Execute rubocop'
|
33
|
+
RuboCop::RakeTask.new(:rubocop) do |task|
|
34
|
+
task.options = ['--rails', '--display-cop-names', '--display-style-guide']
|
35
|
+
task.formatters = ['progress']
|
36
|
+
task.patterns = patterns
|
37
|
+
task.fail_on_error = true
|
38
|
+
end
|
39
|
+
|
40
|
+
namespace :rubocop do
|
41
|
+
desc 'Auto-gen rubocop config'
|
42
|
+
task :auto_gen_config do
|
43
|
+
options = ['--auto-gen-config'].concat patterns
|
44
|
+
require 'benchmark'
|
45
|
+
result = 0
|
46
|
+
cli = RuboCop::CLI.new
|
47
|
+
time = Benchmark.realtime do
|
48
|
+
result = cli.run(options)
|
49
|
+
end
|
50
|
+
puts "Finished in #{time} seconds" if cli.options[:debug]
|
51
|
+
abort('RuboCop failed!') if result.nonzero?
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|