active_model_serializers 0.10.7 → 0.10.9

Sign up to get free protection for your applications and to get access to all the features.
Files changed (164) hide show
  1. checksums.yaml +4 -4
  2. data/.rubocop.yml +4 -0
  3. data/.travis.yml +14 -3
  4. data/CHANGELOG.md +29 -1
  5. data/Gemfile +5 -3
  6. data/README.md +2 -2
  7. data/Rakefile +2 -0
  8. data/active_model_serializers.gemspec +2 -1
  9. data/docs/general/serializers.md +16 -4
  10. data/lib/action_controller/serialization.rb +2 -0
  11. data/lib/active_model/serializable_resource.rb +2 -0
  12. data/lib/active_model/serializer/adapter/attributes.rb +2 -0
  13. data/lib/active_model/serializer/adapter/base.rb +2 -0
  14. data/lib/active_model/serializer/adapter/json.rb +2 -0
  15. data/lib/active_model/serializer/adapter/json_api.rb +2 -0
  16. data/lib/active_model/serializer/adapter/null.rb +2 -0
  17. data/lib/active_model/serializer/adapter.rb +2 -0
  18. data/lib/active_model/serializer/array_serializer.rb +2 -0
  19. data/lib/active_model/serializer/association.rb +2 -0
  20. data/lib/active_model/serializer/attribute.rb +2 -0
  21. data/lib/active_model/serializer/belongs_to_reflection.rb +2 -0
  22. data/lib/active_model/serializer/collection_serializer.rb +6 -6
  23. data/lib/active_model/serializer/concerns/caching.rb +5 -1
  24. data/lib/active_model/serializer/error_serializer.rb +2 -0
  25. data/lib/active_model/serializer/errors_serializer.rb +2 -0
  26. data/lib/active_model/serializer/field.rb +2 -0
  27. data/lib/active_model/serializer/fieldset.rb +2 -0
  28. data/lib/active_model/serializer/has_many_reflection.rb +2 -0
  29. data/lib/active_model/serializer/has_one_reflection.rb +2 -0
  30. data/lib/active_model/serializer/lazy_association.rb +6 -2
  31. data/lib/active_model/serializer/link.rb +23 -0
  32. data/lib/active_model/serializer/lint.rb +2 -0
  33. data/lib/active_model/serializer/null.rb +2 -0
  34. data/lib/active_model/serializer/reflection.rb +5 -0
  35. data/lib/active_model/serializer/version.rb +3 -1
  36. data/lib/active_model/serializer.rb +22 -14
  37. data/lib/active_model_serializers/adapter/attributes.rb +2 -0
  38. data/lib/active_model_serializers/adapter/base.rb +2 -0
  39. data/lib/active_model_serializers/adapter/json.rb +2 -0
  40. data/lib/active_model_serializers/adapter/json_api/deserialization.rb +2 -0
  41. data/lib/active_model_serializers/adapter/json_api/error.rb +2 -0
  42. data/lib/active_model_serializers/adapter/json_api/jsonapi.rb +2 -0
  43. data/lib/active_model_serializers/adapter/json_api/link.rb +2 -0
  44. data/lib/active_model_serializers/adapter/json_api/meta.rb +2 -0
  45. data/lib/active_model_serializers/adapter/json_api/pagination_links.rb +2 -0
  46. data/lib/active_model_serializers/adapter/json_api/relationship.rb +2 -0
  47. data/lib/active_model_serializers/adapter/json_api/resource_identifier.rb +2 -0
  48. data/lib/active_model_serializers/adapter/json_api.rb +14 -9
  49. data/lib/active_model_serializers/adapter/null.rb +2 -0
  50. data/lib/active_model_serializers/adapter.rb +3 -1
  51. data/lib/active_model_serializers/callbacks.rb +2 -0
  52. data/lib/active_model_serializers/deprecate.rb +2 -0
  53. data/lib/active_model_serializers/deserialization.rb +2 -0
  54. data/lib/active_model_serializers/json_pointer.rb +2 -0
  55. data/lib/active_model_serializers/logging.rb +2 -0
  56. data/lib/active_model_serializers/lookup_chain.rb +2 -0
  57. data/lib/active_model_serializers/model.rb +2 -0
  58. data/lib/active_model_serializers/railtie.rb +4 -0
  59. data/lib/active_model_serializers/register_jsonapi_renderer.rb +2 -0
  60. data/lib/active_model_serializers/serializable_resource.rb +4 -2
  61. data/lib/active_model_serializers/serialization_context.rb +2 -0
  62. data/lib/active_model_serializers/test/schema.rb +2 -0
  63. data/lib/active_model_serializers/test/serializer.rb +2 -0
  64. data/lib/active_model_serializers/test.rb +2 -0
  65. data/lib/active_model_serializers.rb +20 -10
  66. data/lib/generators/rails/resource_override.rb +2 -0
  67. data/lib/generators/rails/serializer_generator.rb +2 -0
  68. data/lib/grape/active_model_serializers.rb +2 -0
  69. data/lib/grape/formatters/active_model_serializers.rb +2 -0
  70. data/lib/grape/helpers/active_model_serializers.rb +2 -0
  71. data/lib/tasks/rubocop.rake +2 -0
  72. data/test/action_controller/adapter_selector_test.rb +2 -0
  73. data/test/action_controller/explicit_serializer_test.rb +2 -0
  74. data/test/action_controller/json/include_test.rb +2 -0
  75. data/test/action_controller/json_api/deserialization_test.rb +2 -0
  76. data/test/action_controller/json_api/errors_test.rb +2 -0
  77. data/test/action_controller/json_api/fields_test.rb +2 -0
  78. data/test/action_controller/json_api/linked_test.rb +2 -0
  79. data/test/action_controller/json_api/pagination_test.rb +2 -0
  80. data/test/action_controller/json_api/transform_test.rb +2 -0
  81. data/test/action_controller/lookup_proc_test.rb +2 -0
  82. data/test/action_controller/namespace_lookup_test.rb +2 -0
  83. data/test/action_controller/serialization_scope_name_test.rb +2 -0
  84. data/test/action_controller/serialization_test.rb +11 -3
  85. data/test/active_model_serializers/adapter_for_test.rb +2 -0
  86. data/test/active_model_serializers/json_pointer_test.rb +2 -0
  87. data/test/active_model_serializers/logging_test.rb +2 -0
  88. data/test/active_model_serializers/model_test.rb +2 -0
  89. data/test/active_model_serializers/railtie_test_isolated.rb +2 -0
  90. data/test/active_model_serializers/register_jsonapi_renderer_test_isolated.rb +2 -0
  91. data/test/active_model_serializers/serialization_context_test_isolated.rb +2 -0
  92. data/test/active_model_serializers/test/schema_test.rb +2 -0
  93. data/test/active_model_serializers/test/serializer_test.rb +2 -0
  94. data/test/active_record_test.rb +2 -0
  95. data/test/adapter/attributes_test.rb +2 -0
  96. data/test/adapter/deprecation_test.rb +2 -0
  97. data/test/adapter/json/belongs_to_test.rb +2 -0
  98. data/test/adapter/json/collection_test.rb +2 -0
  99. data/test/adapter/json/has_many_test.rb +2 -0
  100. data/test/adapter/json/transform_test.rb +2 -0
  101. data/test/adapter/json_api/belongs_to_test.rb +2 -0
  102. data/test/adapter/json_api/collection_test.rb +2 -0
  103. data/test/adapter/json_api/errors_test.rb +2 -0
  104. data/test/adapter/json_api/fields_test.rb +2 -0
  105. data/test/adapter/json_api/has_many_explicit_serializer_test.rb +2 -0
  106. data/test/adapter/json_api/has_many_test.rb +2 -0
  107. data/test/adapter/json_api/has_one_test.rb +2 -0
  108. data/test/adapter/json_api/include_data_if_sideloaded_test.rb +2 -0
  109. data/test/adapter/json_api/json_api_test.rb +2 -0
  110. data/test/adapter/json_api/linked_test.rb +2 -0
  111. data/test/adapter/json_api/links_test.rb +18 -1
  112. data/test/adapter/json_api/pagination_links_test.rb +2 -0
  113. data/test/adapter/json_api/parse_test.rb +2 -0
  114. data/test/adapter/json_api/relationship_test.rb +2 -0
  115. data/test/adapter/json_api/resource_meta_test.rb +2 -0
  116. data/test/adapter/json_api/toplevel_jsonapi_test.rb +2 -0
  117. data/test/adapter/json_api/transform_test.rb +2 -0
  118. data/test/adapter/json_api/type_test.rb +2 -0
  119. data/test/adapter/json_test.rb +2 -0
  120. data/test/adapter/null_test.rb +2 -0
  121. data/test/adapter/polymorphic_test.rb +2 -0
  122. data/test/adapter_test.rb +2 -0
  123. data/test/array_serializer_test.rb +2 -0
  124. data/test/benchmark/app.rb +2 -0
  125. data/test/benchmark/benchmarking_support.rb +2 -0
  126. data/test/benchmark/bm_active_record.rb +2 -0
  127. data/test/benchmark/bm_adapter.rb +2 -0
  128. data/test/benchmark/bm_caching.rb +2 -0
  129. data/test/benchmark/bm_lookup_chain.rb +2 -0
  130. data/test/benchmark/bm_transform.rb +2 -0
  131. data/test/benchmark/controllers.rb +2 -0
  132. data/test/benchmark/fixtures.rb +2 -0
  133. data/test/cache_test.rb +69 -3
  134. data/test/collection_serializer_test.rb +2 -0
  135. data/test/fixtures/active_record.rb +2 -0
  136. data/test/fixtures/poro.rb +2 -0
  137. data/test/generators/scaffold_controller_generator_test.rb +2 -0
  138. data/test/generators/serializer_generator_test.rb +2 -0
  139. data/test/grape_test.rb +2 -0
  140. data/test/lint_test.rb +2 -0
  141. data/test/logger_test.rb +2 -0
  142. data/test/poro_test.rb +2 -0
  143. data/test/serializable_resource_test.rb +2 -0
  144. data/test/serializers/association_macros_test.rb +2 -0
  145. data/test/serializers/associations_test.rb +52 -0
  146. data/test/serializers/attribute_test.rb +2 -0
  147. data/test/serializers/attributes_test.rb +2 -0
  148. data/test/serializers/caching_configuration_test_isolated.rb +2 -0
  149. data/test/serializers/configuration_test.rb +2 -0
  150. data/test/serializers/fieldset_test.rb +2 -0
  151. data/test/serializers/meta_test.rb +2 -0
  152. data/test/serializers/options_test.rb +2 -0
  153. data/test/serializers/read_attribute_for_serialization_test.rb +2 -0
  154. data/test/serializers/reflection_test.rb +54 -0
  155. data/test/serializers/root_test.rb +2 -0
  156. data/test/serializers/serialization_test.rb +2 -0
  157. data/test/serializers/serializer_for_test.rb +2 -0
  158. data/test/serializers/serializer_for_with_namespace_test.rb +2 -0
  159. data/test/support/isolated_unit.rb +2 -0
  160. data/test/support/rails5_shims.rb +2 -0
  161. data/test/support/rails_app.rb +2 -0
  162. data/test/support/serialization_testing.rb +2 -0
  163. data/test/test_helper.rb +2 -0
  164. metadata +56 -35
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'test_helper'
2
4
 
3
5
  module ActiveModelSerializers
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'test_helper'
2
4
 
3
5
  module ActiveModel
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'test_helper'
2
4
 
3
5
  module ActiveModelSerializers
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'test_helper'
2
4
 
3
5
  module ActiveModelSerializers
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'test_helper'
2
4
 
3
5
  module ActiveModelSerializers
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'test_helper'
2
4
 
3
5
  module ActiveModelSerializers
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'test_helper'
2
4
 
3
5
  module ActiveModelSerializers
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'test_helper'
2
4
 
3
5
  module ActiveModel
data/test/adapter_test.rb CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'test_helper'
2
4
 
3
5
  module ActiveModelSerializers
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'test_helper'
2
4
  require_relative 'collection_serializer_test'
3
5
 
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  # https://github.com/rails-api/active_model_serializers/pull/872
2
4
  # approx ref 792fb8a9053f8db3c562dae4f40907a582dd1720 to test against
3
5
  require 'bundler/setup'
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'benchmark/ips'
2
4
  require 'json'
3
5
 
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require_relative './benchmarking_support'
2
4
  require_relative './app'
3
5
 
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require_relative './benchmarking_support'
2
4
  require_relative './app'
3
5
 
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require_relative './benchmarking_support'
2
4
  require_relative './app'
3
5
 
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require_relative './benchmarking_support'
2
4
  require_relative './app'
3
5
 
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require_relative './benchmarking_support'
2
4
  require_relative './app'
3
5
 
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class PrimaryResourceController < ActionController::Base
2
4
  PRIMARY_RESOURCE =
3
5
  begin
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  Rails.configuration.serializers = []
2
4
  class HasOneRelationshipSerializer < ActiveModel::Serializer
3
5
  attributes :id, :first_name, :last_name
data/test/cache_test.rb CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'test_helper'
2
4
  require 'tmpdir'
3
5
  require 'tempfile'
@@ -53,6 +55,11 @@ module ActiveModelSerializers
53
55
  has_many :roles
54
56
  has_one :bio
55
57
  end
58
+ class AuthorSerializerWithCache < ActiveModel::Serializer
59
+ cache
60
+
61
+ attributes :name
62
+ end
56
63
 
57
64
  class Blog < ::Model
58
65
  attributes :name
@@ -144,6 +151,65 @@ module ActiveModelSerializers
144
151
  @blog_serializer = BlogSerializer.new(@blog)
145
152
  end
146
153
 
154
+ def test_expiring_of_cache_at_update_of_record
155
+ original_cache_versioning = :none
156
+
157
+ if ARModels::Author.respond_to?(:cache_versioning)
158
+ original_cache_versioning = ARModels::Author.cache_versioning
159
+ ARModels::Author.cache_versioning = true
160
+ end
161
+
162
+ author = ARModels::Author.create(name: 'Foo')
163
+ author_json = AuthorSerializerWithCache.new(author).as_json
164
+
165
+ assert_equal 'Foo', author_json[:name]
166
+
167
+ author.update_attributes(name: 'Bar')
168
+ author_json = AuthorSerializerWithCache.new(author).as_json
169
+
170
+ expected = 'Bar'
171
+ actual = author_json[:name]
172
+ if ENV['APPVEYOR'] && actual != expected
173
+ skip('Cache expiration tests sometimes fail on Appveyor. FIXME :)')
174
+ else
175
+ assert_equal expected, actual
176
+ end
177
+ ensure
178
+ ARModels::Author.cache_versioning = original_cache_versioning unless original_cache_versioning == :none
179
+ end
180
+
181
+ def test_cache_expiration_in_collection_on_update_of_record
182
+ original_cache_versioning = :none
183
+
184
+ if ARModels::Author.respond_to?(:cache_versioning)
185
+ original_cache_versioning = ARModels::Author.cache_versioning
186
+ ARModels::Author.cache_versioning = true
187
+ end
188
+
189
+ foo = 'Foo'
190
+ foo2 = 'Foo2'
191
+ author = ARModels::Author.create(name: foo)
192
+ author2 = ARModels::Author.create(name: foo2)
193
+ author_collection = [author, author, author2]
194
+
195
+ collection_json = render_object_with_cache(author_collection, each_serializer: AuthorSerializerWithCache)
196
+ actual = collection_json
197
+ expected = [{ name: foo }, { name: foo }, { name: foo2 }]
198
+ if ENV['APPVEYOR'] && actual != expected
199
+ skip('Cache expiration tests sometimes fail on Appveyor. FIXME :)')
200
+ else
201
+ assert_equal expected, actual
202
+ end
203
+
204
+ bar = 'Bar'
205
+ author.update!(name: bar)
206
+
207
+ collection_json = render_object_with_cache(author_collection, each_serializer: AuthorSerializerWithCache)
208
+ assert_equal [{ name: bar }, { name: bar }, { name: foo2 }], collection_json
209
+ ensure
210
+ ARModels::Author.cache_versioning = original_cache_versioning unless original_cache_versioning == :none
211
+ end
212
+
147
213
  def test_explicit_cache_store
148
214
  default_store = Class.new(ActiveModel::Serializer) do
149
215
  cache
@@ -415,7 +481,7 @@ module ActiveModelSerializers
415
481
  adapter_options = {}
416
482
  adapter_instance = ActiveModelSerializers::Adapter::Attributes.new(serializers, adapter_options)
417
483
  serializers.serializable_hash(adapter_options, options, adapter_instance)
418
- cached_attributes = adapter_options.fetch(:cached_attributes).with_indifferent_access
484
+ cached_attributes = options.fetch(:cached_attributes).with_indifferent_access
419
485
 
420
486
  include_directive = ActiveModelSerializers.default_include_directive
421
487
  manual_cached_attributes = ActiveModel::Serializer.cache_read_multi(serializers, adapter_instance, include_directive).with_indifferent_access
@@ -446,9 +512,9 @@ module ActiveModelSerializers
446
512
  serializers.serializable_hash(adapter_options, options, adapter_instance)
447
513
 
448
514
  # Should find something with read_multi now
449
- adapter_options = {}
515
+ options = {}
450
516
  serializers.serializable_hash(adapter_options, options, adapter_instance)
451
- cached_attributes = adapter_options.fetch(:cached_attributes)
517
+ cached_attributes = options.fetch(:cached_attributes)
452
518
 
453
519
  include_directive = ActiveModelSerializers.default_include_directive
454
520
  manual_cached_attributes = ActiveModel::Serializer.cache_read_multi(serializers, adapter_instance, include_directive)
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'test_helper'
2
4
 
3
5
  module ActiveModel
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'active_record'
2
4
 
3
5
  ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: ':memory:')
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class Model < ActiveModelSerializers::Model
2
4
  rand(2).zero? && derive_attributes_from_names_and_fix_accessors
3
5
 
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'test_helper'
2
4
  require 'generators/rails/resource_override'
3
5
 
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'test_helper'
2
4
  require 'generators/rails/resource_override'
3
5
  require 'generators/rails/serializer_generator'
data/test/grape_test.rb CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'test_helper'
2
4
  TestHelper.silence_warnings do
3
5
  require 'grape'
data/test/lint_test.rb CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'test_helper'
2
4
 
3
5
  module ActiveModel
data/test/logger_test.rb CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'test_helper'
2
4
 
3
5
  module ActiveModelSerializers
data/test/poro_test.rb CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'test_helper'
2
4
 
3
5
  class PoroTest < ActiveSupport::TestCase
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'test_helper'
2
4
 
3
5
  module ActiveModelSerializers
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'test_helper'
2
4
 
3
5
  module ActiveModel
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'test_helper'
2
4
  module ActiveModel
3
5
  class Serializer
@@ -285,6 +287,56 @@ module ActiveModel
285
287
  end
286
288
  end
287
289
 
290
+ class AssociationsNamespacedSerializersTest < ActiveSupport::TestCase
291
+ class Post < ::Model
292
+ associations :comments, :author, :description
293
+
294
+ def latest_comments
295
+ comments[0..3]
296
+ end
297
+ end
298
+ class Comment < ::Model; end
299
+ class Author < ::Model; end
300
+ class Description < ::Model; end
301
+
302
+ class ResourceNamespace
303
+ class PostSerializer < ActiveModel::Serializer
304
+ has_many :comments, namespace: ResourceNamespace
305
+ has_many :latest_comments, namespace: ResourceNamespace
306
+ belongs_to :author, namespace: ResourceNamespace
307
+ has_one :description, namespace: ResourceNamespace
308
+ end
309
+ class CommentSerializer < ActiveModel::Serializer; end
310
+ class AuthorSerializer < ActiveModel::Serializer; end
311
+ class DescriptionSerializer < ActiveModel::Serializer; end
312
+ end
313
+
314
+ def setup
315
+ @comment = Comment.new
316
+ @author = Author.new
317
+ @description = Description.new
318
+ @post = Post.new(comments: [@comment],
319
+ author: @author,
320
+ description: @description)
321
+ @post_serializer = ResourceNamespace::PostSerializer.new(@post)
322
+ end
323
+
324
+ def test_associations_namespaced_serializers
325
+ @post_serializer.associations.each do |association|
326
+ case association.key
327
+ when :comments, :latest_comments
328
+ assert_instance_of(ResourceNamespace::CommentSerializer, association.lazy_association.serializer.first)
329
+ when :author
330
+ assert_instance_of(ResourceNamespace::AuthorSerializer, association.lazy_association.serializer)
331
+ when :description
332
+ assert_instance_of(ResourceNamespace::DescriptionSerializer, association.lazy_association.serializer)
333
+ else
334
+ flunk "Unknown association: #{key}"
335
+ end
336
+ end
337
+ end
338
+ end
339
+
288
340
  class NestedSerializersTest < ActiveSupport::TestCase
289
341
  class Post < ::Model
290
342
  associations :comments, :author, :description
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'test_helper'
2
4
 
3
5
  module ActiveModel
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'test_helper'
2
4
 
3
5
  module ActiveModel
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  # Execute this test in isolation
2
4
  require 'support/isolated_unit'
3
5
 
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'test_helper'
2
4
 
3
5
  module ActiveModel
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'test_helper'
2
4
 
3
5
  module ActiveModel
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'test_helper'
2
4
 
3
5
  module ActiveModel
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'test_helper'
2
4
 
3
5
  module ActiveModel
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'test_helper'
2
4
 
3
5
  module ActiveModel
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'test_helper'
2
4
  module ActiveModel
3
5
  class Serializer
@@ -423,5 +425,57 @@ module ActiveModel
423
425
  end
424
426
  # rubocop:enable Metrics/AbcSize
425
427
  end
428
+ class ThreadedReflectionTest < ActiveSupport::TestCase
429
+ class Post < ::Model
430
+ attributes :id, :title, :body
431
+ associations :comments
432
+ end
433
+ class Comment < ::Model
434
+ attributes :id, :body
435
+ associations :post
436
+ end
437
+ class CommentSerializer < ActiveModel::Serializer
438
+ type 'comment'
439
+ attributes :id, :body
440
+ has_one :post
441
+ end
442
+ class PostSerializer < ActiveModel::Serializer
443
+ type 'post'
444
+ attributes :id, :title, :body
445
+ has_many :comments, serializer: CommentSerializer do
446
+ sleep 0.1
447
+ object.comments
448
+ end
449
+ end
450
+
451
+ # per https://github.com/rails-api/active_model_serializers/issues/2270
452
+ def test_concurrent_serialization
453
+ post1 = Post.new(id: 1, title: 'Post 1 Title', body: 'Post 1 Body')
454
+ post1.comments = [Comment.new(id: 1, body: 'Comment on Post 1', post: post1)]
455
+ post2 = Post.new(id: 2, title: 'Post 2 Title', body: 'Post 2 Body')
456
+ post2.comments = [Comment.new(id: 2, body: 'Comment on Post 2', post: post2)]
457
+ serialized_posts = {
458
+ first: Set.new,
459
+ second: Set.new
460
+ }
461
+ t1 = Thread.new do
462
+ 10.times do
463
+ serialized_posts[:first] << PostSerializer.new(post1, {}).to_json
464
+ end
465
+ end
466
+ t2 = Thread.new do
467
+ 10.times do
468
+ serialized_posts[:second] << PostSerializer.new(post2, {}).to_json
469
+ end
470
+ end
471
+ t1.join
472
+ t2.join
473
+ expected_first_post_serialization = '{"id":1,"title":"Post 1 Title","body":"Post 1 Body","comments":[{"id":1,"body":"Comment on Post 1"}]}'
474
+ expected_second_post_serialization = '{"id":2,"title":"Post 2 Title","body":"Post 2 Body","comments":[{"id":2,"body":"Comment on Post 2"}]}'
475
+
476
+ assert_equal [expected_second_post_serialization], serialized_posts[:second].to_a
477
+ assert_equal [expected_first_post_serialization], serialized_posts[:first].to_a
478
+ end
479
+ end
426
480
  end
427
481
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'test_helper'
2
4
 
3
5
  module ActiveModel
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module ActiveModel
2
4
  class Serializer
3
5
  class SerializationTest < ActiveSupport::TestCase
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'test_helper'
2
4
 
3
5
  module ActiveModel
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'test_helper'
2
4
 
3
5
  module ActiveModel
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  # https://github.com/rails/rails/blob/v5.0.0.beta1/railties/test/isolation/abstract_unit.rb
2
4
 
3
5
  # Usage Example:
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Rails5Shims
2
4
  module ControllerTests
3
5
  # https://github.com/rails/rails/blob/b217354/actionpack/lib/action_controller/test_case.rb
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'support/isolated_unit'
2
4
  module ActiveModelSerializers
3
5
  RailsApplication = TestHelpers::Generation.make_basic_app do |app|
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module SerializationTesting
2
4
  def config
3
5
  ActiveModelSerializers.config
data/test/test_helper.rb CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  # Configure Rails Environment
2
4
  ENV['RAILS_ENV'] = 'test'
3
5
  require 'bundler/setup'