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.
Files changed (215) hide show
  1. checksums.yaml +5 -5
  2. data/CHANGELOG.md +227 -2
  3. data/README.md +171 -33
  4. data/lib/action_controller/serialization.rb +23 -3
  5. data/lib/active_model/serializable_resource.rb +2 -0
  6. data/lib/active_model/serializer/adapter/attributes.rb +2 -0
  7. data/lib/active_model/serializer/adapter/base.rb +4 -0
  8. data/lib/active_model/serializer/adapter/json.rb +2 -0
  9. data/lib/active_model/serializer/adapter/json_api.rb +2 -0
  10. data/lib/active_model/serializer/adapter/null.rb +2 -0
  11. data/lib/active_model/serializer/adapter.rb +2 -0
  12. data/lib/active_model/serializer/array_serializer.rb +10 -5
  13. data/lib/active_model/serializer/association.rb +64 -10
  14. data/lib/active_model/serializer/attribute.rb +2 -0
  15. data/lib/active_model/serializer/belongs_to_reflection.rb +6 -3
  16. data/lib/active_model/serializer/collection_serializer.rb +39 -13
  17. data/lib/active_model/serializer/{caching.rb → concerns/caching.rb} +89 -117
  18. data/lib/active_model/serializer/error_serializer.rb +13 -7
  19. data/lib/active_model/serializer/errors_serializer.rb +27 -20
  20. data/lib/active_model/serializer/field.rb +2 -0
  21. data/lib/active_model/serializer/fieldset.rb +3 -1
  22. data/lib/active_model/serializer/has_many_reflection.rb +5 -3
  23. data/lib/active_model/serializer/has_one_reflection.rb +3 -4
  24. data/lib/active_model/serializer/lazy_association.rb +99 -0
  25. data/lib/active_model/serializer/link.rb +23 -0
  26. data/lib/active_model/serializer/lint.rb +136 -130
  27. data/lib/active_model/serializer/null.rb +2 -0
  28. data/lib/active_model/serializer/reflection.rb +130 -65
  29. data/lib/active_model/serializer/version.rb +3 -1
  30. data/lib/active_model/serializer.rb +319 -86
  31. data/lib/active_model_serializers/adapter/attributes.rb +17 -57
  32. data/lib/active_model_serializers/adapter/base.rb +41 -39
  33. data/lib/active_model_serializers/adapter/json.rb +2 -0
  34. data/lib/active_model_serializers/adapter/json_api/deserialization.rb +4 -2
  35. data/lib/active_model_serializers/adapter/json_api/error.rb +2 -0
  36. data/lib/active_model_serializers/adapter/json_api/jsonapi.rb +2 -0
  37. data/lib/active_model_serializers/adapter/json_api/link.rb +3 -1
  38. data/lib/active_model_serializers/adapter/json_api/meta.rb +2 -0
  39. data/lib/active_model_serializers/adapter/json_api/pagination_links.rb +49 -21
  40. data/lib/active_model_serializers/adapter/json_api/relationship.rb +77 -23
  41. data/lib/active_model_serializers/adapter/json_api/resource_identifier.rb +41 -10
  42. data/lib/active_model_serializers/adapter/json_api.rb +84 -65
  43. data/lib/active_model_serializers/adapter/null.rb +2 -0
  44. data/lib/active_model_serializers/adapter.rb +9 -1
  45. data/lib/active_model_serializers/callbacks.rb +2 -0
  46. data/lib/active_model_serializers/deprecate.rb +3 -2
  47. data/lib/active_model_serializers/deserialization.rb +4 -0
  48. data/lib/active_model_serializers/json_pointer.rb +2 -0
  49. data/lib/active_model_serializers/logging.rb +2 -0
  50. data/lib/active_model_serializers/lookup_chain.rb +82 -0
  51. data/lib/active_model_serializers/model/caching.rb +25 -0
  52. data/lib/active_model_serializers/model.rb +111 -28
  53. data/lib/active_model_serializers/railtie.rb +7 -1
  54. data/lib/active_model_serializers/register_jsonapi_renderer.rb +46 -31
  55. data/lib/active_model_serializers/serializable_resource.rb +10 -7
  56. data/lib/active_model_serializers/serialization_context.rb +12 -3
  57. data/lib/active_model_serializers/test/schema.rb +4 -2
  58. data/lib/active_model_serializers/test/serializer.rb +2 -0
  59. data/lib/active_model_serializers/test.rb +2 -0
  60. data/lib/active_model_serializers.rb +35 -10
  61. data/lib/generators/rails/resource_override.rb +3 -1
  62. data/lib/generators/rails/serializer_generator.rb +6 -4
  63. data/lib/grape/active_model_serializers.rb +9 -5
  64. data/lib/grape/formatters/active_model_serializers.rb +21 -2
  65. data/lib/grape/helpers/active_model_serializers.rb +3 -0
  66. data/lib/tasks/rubocop.rake +55 -0
  67. metadata +110 -276
  68. data/.github/ISSUE_TEMPLATE.md +0 -29
  69. data/.github/PULL_REQUEST_TEMPLATE.md +0 -15
  70. data/.gitignore +0 -35
  71. data/.rubocop.yml +0 -104
  72. data/.rubocop_todo.yml +0 -167
  73. data/.simplecov +0 -110
  74. data/.travis.yml +0 -43
  75. data/CONTRIBUTING.md +0 -105
  76. data/Gemfile +0 -53
  77. data/Rakefile +0 -103
  78. data/active_model_serializers.gemspec +0 -66
  79. data/appveyor.yml +0 -24
  80. data/bin/bench +0 -171
  81. data/bin/bench_regression +0 -316
  82. data/bin/serve_benchmark +0 -39
  83. data/docs/ARCHITECTURE.md +0 -126
  84. data/docs/README.md +0 -40
  85. data/docs/STYLE.md +0 -58
  86. data/docs/general/adapters.md +0 -245
  87. data/docs/general/caching.md +0 -52
  88. data/docs/general/configuration_options.md +0 -100
  89. data/docs/general/deserialization.md +0 -100
  90. data/docs/general/getting_started.md +0 -133
  91. data/docs/general/instrumentation.md +0 -40
  92. data/docs/general/key_transforms.md +0 -40
  93. data/docs/general/logging.md +0 -14
  94. data/docs/general/rendering.md +0 -255
  95. data/docs/general/serializers.md +0 -372
  96. data/docs/how-open-source-maintained.jpg +0 -0
  97. data/docs/howto/add_pagination_links.md +0 -139
  98. data/docs/howto/add_root_key.md +0 -51
  99. data/docs/howto/outside_controller_use.md +0 -58
  100. data/docs/howto/passing_arbitrary_options.md +0 -27
  101. data/docs/howto/serialize_poro.md +0 -32
  102. data/docs/howto/test.md +0 -152
  103. data/docs/integrations/ember-and-json-api.md +0 -112
  104. data/docs/integrations/grape.md +0 -19
  105. data/docs/jsonapi/errors.md +0 -56
  106. data/docs/jsonapi/schema/schema.json +0 -366
  107. data/docs/jsonapi/schema.md +0 -151
  108. data/docs/rfcs/0000-namespace.md +0 -106
  109. data/docs/rfcs/template.md +0 -15
  110. data/lib/active_model/serializer/associations.rb +0 -100
  111. data/lib/active_model/serializer/attributes.rb +0 -82
  112. data/lib/active_model/serializer/collection_reflection.rb +0 -7
  113. data/lib/active_model/serializer/configuration.rb +0 -35
  114. data/lib/active_model/serializer/include_tree.rb +0 -111
  115. data/lib/active_model/serializer/links.rb +0 -35
  116. data/lib/active_model/serializer/meta.rb +0 -29
  117. data/lib/active_model/serializer/singular_reflection.rb +0 -7
  118. data/lib/active_model/serializer/type.rb +0 -25
  119. data/lib/active_model_serializers/key_transform.rb +0 -70
  120. data/test/action_controller/adapter_selector_test.rb +0 -53
  121. data/test/action_controller/explicit_serializer_test.rb +0 -134
  122. data/test/action_controller/json/include_test.rb +0 -167
  123. data/test/action_controller/json_api/deserialization_test.rb +0 -112
  124. data/test/action_controller/json_api/errors_test.rb +0 -41
  125. data/test/action_controller/json_api/linked_test.rb +0 -197
  126. data/test/action_controller/json_api/pagination_test.rb +0 -116
  127. data/test/action_controller/json_api/transform_test.rb +0 -181
  128. data/test/action_controller/serialization_scope_name_test.rb +0 -229
  129. data/test/action_controller/serialization_test.rb +0 -469
  130. data/test/active_model_serializers/adapter_for_test.rb +0 -208
  131. data/test/active_model_serializers/json_pointer_test.rb +0 -20
  132. data/test/active_model_serializers/key_transform_test.rb +0 -263
  133. data/test/active_model_serializers/logging_test.rb +0 -77
  134. data/test/active_model_serializers/model_test.rb +0 -9
  135. data/test/active_model_serializers/railtie_test_isolated.rb +0 -63
  136. data/test/active_model_serializers/serialization_context_test_isolated.rb +0 -58
  137. data/test/active_model_serializers/test/schema_test.rb +0 -130
  138. data/test/active_model_serializers/test/serializer_test.rb +0 -62
  139. data/test/active_record_test.rb +0 -9
  140. data/test/adapter/deprecation_test.rb +0 -100
  141. data/test/adapter/json/belongs_to_test.rb +0 -45
  142. data/test/adapter/json/collection_test.rb +0 -90
  143. data/test/adapter/json/has_many_test.rb +0 -45
  144. data/test/adapter/json/transform_test.rb +0 -93
  145. data/test/adapter/json_api/belongs_to_test.rb +0 -155
  146. data/test/adapter/json_api/collection_test.rb +0 -95
  147. data/test/adapter/json_api/errors_test.rb +0 -78
  148. data/test/adapter/json_api/fields_test.rb +0 -87
  149. data/test/adapter/json_api/has_many_embed_ids_test.rb +0 -43
  150. data/test/adapter/json_api/has_many_explicit_serializer_test.rb +0 -96
  151. data/test/adapter/json_api/has_many_test.rb +0 -144
  152. data/test/adapter/json_api/has_one_test.rb +0 -80
  153. data/test/adapter/json_api/json_api_test.rb +0 -35
  154. data/test/adapter/json_api/linked_test.rb +0 -392
  155. data/test/adapter/json_api/links_test.rb +0 -93
  156. data/test/adapter/json_api/pagination_links_test.rb +0 -166
  157. data/test/adapter/json_api/parse_test.rb +0 -137
  158. data/test/adapter/json_api/relationship_test.rb +0 -161
  159. data/test/adapter/json_api/relationships_test.rb +0 -199
  160. data/test/adapter/json_api/resource_identifier_test.rb +0 -85
  161. data/test/adapter/json_api/resource_meta_test.rb +0 -100
  162. data/test/adapter/json_api/toplevel_jsonapi_test.rb +0 -82
  163. data/test/adapter/json_api/transform_test.rb +0 -502
  164. data/test/adapter/json_api/type_test.rb +0 -61
  165. data/test/adapter/json_test.rb +0 -45
  166. data/test/adapter/null_test.rb +0 -23
  167. data/test/adapter/polymorphic_test.rb +0 -171
  168. data/test/adapter_test.rb +0 -67
  169. data/test/array_serializer_test.rb +0 -22
  170. data/test/benchmark/app.rb +0 -65
  171. data/test/benchmark/benchmarking_support.rb +0 -67
  172. data/test/benchmark/bm_caching.rb +0 -119
  173. data/test/benchmark/bm_transform.rb +0 -34
  174. data/test/benchmark/config.ru +0 -3
  175. data/test/benchmark/controllers.rb +0 -84
  176. data/test/benchmark/fixtures.rb +0 -219
  177. data/test/cache_test.rb +0 -485
  178. data/test/collection_serializer_test.rb +0 -110
  179. data/test/fixtures/active_record.rb +0 -78
  180. data/test/fixtures/poro.rb +0 -282
  181. data/test/generators/scaffold_controller_generator_test.rb +0 -24
  182. data/test/generators/serializer_generator_test.rb +0 -57
  183. data/test/grape_test.rb +0 -82
  184. data/test/include_tree/from_include_args_test.rb +0 -26
  185. data/test/include_tree/from_string_test.rb +0 -94
  186. data/test/include_tree/include_args_to_hash_test.rb +0 -64
  187. data/test/lint_test.rb +0 -49
  188. data/test/logger_test.rb +0 -18
  189. data/test/poro_test.rb +0 -9
  190. data/test/serializable_resource_test.rb +0 -83
  191. data/test/serializers/association_macros_test.rb +0 -36
  192. data/test/serializers/associations_test.rb +0 -295
  193. data/test/serializers/attribute_test.rb +0 -151
  194. data/test/serializers/attributes_test.rb +0 -52
  195. data/test/serializers/caching_configuration_test_isolated.rb +0 -170
  196. data/test/serializers/configuration_test.rb +0 -32
  197. data/test/serializers/fieldset_test.rb +0 -14
  198. data/test/serializers/meta_test.rb +0 -196
  199. data/test/serializers/options_test.rb +0 -21
  200. data/test/serializers/read_attribute_for_serialization_test.rb +0 -79
  201. data/test/serializers/root_test.rb +0 -21
  202. data/test/serializers/serialization_test.rb +0 -55
  203. data/test/serializers/serializer_for_test.rb +0 -134
  204. data/test/support/custom_schemas/active_model_serializers/test/schema_test/my/index.json +0 -6
  205. data/test/support/isolated_unit.rb +0 -79
  206. data/test/support/rails5_shims.rb +0 -47
  207. data/test/support/rails_app.rb +0 -45
  208. data/test/support/schemas/active_model_serializers/test/schema_test/my/index.json +0 -6
  209. data/test/support/schemas/active_model_serializers/test/schema_test/my/show.json +0 -6
  210. data/test/support/schemas/custom/show.json +0 -7
  211. data/test/support/schemas/hyper_schema.json +0 -93
  212. data/test/support/schemas/render_using_json_api.json +0 -43
  213. data/test/support/schemas/simple_json_pointers.json +0 -10
  214. data/test/support/serialization_testing.rb +0 -53
  215. 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
- 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
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
- # actionpack/lib/action_dispatch/http/mime_types.rb
42
- Mime::Type.register ActiveModelSerializers::Jsonapi::MEDIA_TYPE, :jsonapi
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
- parsers = Rails::VERSION::MAJOR >= 5 ? ActionDispatch::Http::Parameters : ActionDispatch::ParamsParser
45
- media_type = Mime::Type.lookup(ActiveModelSerializers::Jsonapi::MEDIA_TYPE)
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
- # 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
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
- # 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
- 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, @serializer_opts =
18
- options.partition { |k, _| ADAPTER_OPTION_KEYS.include? k }.map { |h| Hash[h] }
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
- ActiveModelSerializers::Adapter.create(serializer_instance, adapter_opts)
42
- rescue ActiveModel::Serializer::CollectionSerializer::NoSerializerError
43
- resource
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
- # Replace :serializer key with :each_serializer if present
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(request, options = {})
26
- @request_url = request.original_url[/\A[^?]+/]
27
- @query_parameters = request.query_parameters
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 'set'
2
4
  module ActiveModelSerializers
3
5
  module Test
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module ActiveModelSerializers
2
4
  module Test
3
5
  extend ActiveSupport::Autoload
@@ -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
- autoload :Model
9
- autoload :Callbacks
10
- autoload :Deserialization
11
- autoload :SerializableResource
12
- autoload :Logging
13
- autoload :Test
14
- autoload :Adapter
15
- autoload :JsonPointer
16
- autoload :Deprecate
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, boolean: 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 :suffix => 'Serializer'
7
+ check_class_collision suffix: 'Serializer'
6
8
 
7
- argument :attributes, :type => :array, :default => [], :banner => 'field:type field:type'
9
+ argument :attributes, type: :array, default: [], banner: 'field:type field:type'
8
10
 
9
- class_option :parent, :type => :string, :desc => 'The parent class for the generated serializer'
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 defined?(::ApplicationSerializer)
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::ActiveModelSerializers
8
- extend ActiveSupport::Concern
9
+ module Grape
10
+ module ActiveModelSerializers
11
+ extend ActiveSupport::Concern
9
12
 
10
- included do
11
- formatter :json, Grape::Formatters::ActiveModelSerializers
12
- helpers Grape::Helpers::ActiveModelSerializers
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
@@ -1,4 +1,7 @@
1
+ # frozen_string_literal: true
2
+
1
3
  # Helpers can be included in your Grape endpoint as: helpers Grape::Helpers::ActiveModelSerializers
4
+
2
5
  module Grape
3
6
  module Helpers
4
7
  module ActiveModelSerializers
@@ -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