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.
Files changed (179) hide show
  1. checksums.yaml +4 -4
  2. data/.github/ISSUE_TEMPLATE.md +29 -0
  3. data/.github/PULL_REQUEST_TEMPLATE.md +15 -0
  4. data/.gitignore +1 -0
  5. data/.rubocop.yml +19 -1
  6. data/.rubocop_todo.yml +30 -103
  7. data/.simplecov +0 -1
  8. data/.travis.yml +20 -8
  9. data/CHANGELOG.md +89 -5
  10. data/CONTRIBUTING.md +54 -179
  11. data/Gemfile +7 -2
  12. data/{LICENSE.txt → MIT-LICENSE} +0 -0
  13. data/README.md +27 -5
  14. data/Rakefile +44 -16
  15. data/active_model_serializers.gemspec +9 -1
  16. data/appveyor.yml +1 -0
  17. data/bin/bench +171 -0
  18. data/bin/bench_regression +316 -0
  19. data/bin/serve_benchmark +39 -0
  20. data/docs/ARCHITECTURE.md +13 -7
  21. data/docs/README.md +5 -1
  22. data/docs/STYLE.md +58 -0
  23. data/docs/general/adapters.md +99 -16
  24. data/docs/general/configuration_options.md +87 -14
  25. data/docs/general/deserialization.md +100 -0
  26. data/docs/general/getting_started.md +35 -0
  27. data/docs/general/instrumentation.md +1 -1
  28. data/docs/general/key_transforms.md +40 -0
  29. data/docs/general/rendering.md +115 -13
  30. data/docs/general/serializers.md +138 -6
  31. data/docs/howto/add_pagination_links.md +36 -18
  32. data/docs/howto/outside_controller_use.md +4 -4
  33. data/docs/howto/passing_arbitrary_options.md +27 -0
  34. data/docs/jsonapi/errors.md +56 -0
  35. data/docs/jsonapi/schema.md +29 -18
  36. data/docs/rfcs/0000-namespace.md +106 -0
  37. data/docs/rfcs/template.md +15 -0
  38. data/lib/action_controller/serialization.rb +10 -19
  39. data/lib/active_model/serializable_resource.rb +4 -65
  40. data/lib/active_model/serializer.rb +73 -18
  41. data/lib/active_model/serializer/adapter.rb +15 -82
  42. data/lib/active_model/serializer/adapter/attributes.rb +5 -56
  43. data/lib/active_model/serializer/adapter/base.rb +5 -47
  44. data/lib/active_model/serializer/adapter/json.rb +6 -12
  45. data/lib/active_model/serializer/adapter/json_api.rb +5 -213
  46. data/lib/active_model/serializer/adapter/null.rb +7 -3
  47. data/lib/active_model/serializer/array_serializer.rb +3 -3
  48. data/lib/active_model/serializer/association.rb +4 -5
  49. data/lib/active_model/serializer/attributes.rb +1 -1
  50. data/lib/active_model/serializer/caching.rb +56 -5
  51. data/lib/active_model/serializer/collection_serializer.rb +30 -13
  52. data/lib/active_model/serializer/configuration.rb +7 -0
  53. data/lib/active_model/serializer/error_serializer.rb +10 -0
  54. data/lib/active_model/serializer/errors_serializer.rb +27 -0
  55. data/lib/active_model/serializer/links.rb +4 -2
  56. data/lib/active_model/serializer/lint.rb +14 -0
  57. data/lib/active_model/serializer/meta.rb +29 -0
  58. data/lib/active_model/serializer/null.rb +17 -0
  59. data/lib/active_model/serializer/reflection.rb +57 -1
  60. data/lib/active_model/serializer/type.rb +1 -1
  61. data/lib/active_model/serializer/version.rb +1 -1
  62. data/lib/active_model_serializers.rb +17 -0
  63. data/lib/active_model_serializers/adapter.rb +92 -0
  64. data/lib/active_model_serializers/adapter/attributes.rb +94 -0
  65. data/lib/active_model_serializers/adapter/base.rb +90 -0
  66. data/lib/active_model_serializers/adapter/json.rb +11 -0
  67. data/lib/active_model_serializers/adapter/json_api.rb +513 -0
  68. data/lib/active_model_serializers/adapter/json_api/deserialization.rb +213 -0
  69. data/lib/active_model_serializers/adapter/json_api/error.rb +96 -0
  70. data/lib/active_model_serializers/adapter/json_api/jsonapi.rb +49 -0
  71. data/lib/active_model_serializers/adapter/json_api/link.rb +83 -0
  72. data/lib/active_model_serializers/adapter/json_api/meta.rb +37 -0
  73. data/lib/active_model_serializers/adapter/json_api/pagination_links.rb +57 -0
  74. data/lib/active_model_serializers/adapter/json_api/relationship.rb +52 -0
  75. data/lib/active_model_serializers/adapter/json_api/resource_identifier.rb +37 -0
  76. data/lib/active_model_serializers/adapter/null.rb +10 -0
  77. data/lib/active_model_serializers/cached_serializer.rb +87 -0
  78. data/lib/active_model_serializers/callbacks.rb +1 -1
  79. data/lib/active_model_serializers/deprecate.rb +55 -0
  80. data/lib/active_model_serializers/deserialization.rb +2 -2
  81. data/lib/active_model_serializers/fragment_cache.rb +118 -0
  82. data/lib/active_model_serializers/json_pointer.rb +14 -0
  83. data/lib/active_model_serializers/key_transform.rb +70 -0
  84. data/lib/active_model_serializers/logging.rb +4 -1
  85. data/lib/active_model_serializers/model.rb +11 -1
  86. data/lib/active_model_serializers/railtie.rb +9 -1
  87. data/lib/active_model_serializers/register_jsonapi_renderer.rb +64 -0
  88. data/lib/active_model_serializers/serializable_resource.rb +81 -0
  89. data/lib/active_model_serializers/serialization_context.rb +24 -2
  90. data/lib/active_model_serializers/test/schema.rb +2 -2
  91. data/lib/grape/formatters/active_model_serializers.rb +1 -1
  92. data/test/action_controller/adapter_selector_test.rb +1 -1
  93. data/test/action_controller/json_api/deserialization_test.rb +56 -3
  94. data/test/action_controller/json_api/errors_test.rb +41 -0
  95. data/test/action_controller/json_api/linked_test.rb +10 -9
  96. data/test/action_controller/json_api/pagination_test.rb +2 -2
  97. data/test/action_controller/json_api/transform_test.rb +180 -0
  98. data/test/action_controller/serialization_scope_name_test.rb +201 -35
  99. data/test/action_controller/serialization_test.rb +39 -7
  100. data/test/active_model_serializers/adapter_for_test.rb +208 -0
  101. data/test/active_model_serializers/cached_serializer_test.rb +80 -0
  102. data/test/active_model_serializers/fragment_cache_test.rb +34 -0
  103. data/test/active_model_serializers/json_pointer_test.rb +20 -0
  104. data/test/active_model_serializers/key_transform_test.rb +263 -0
  105. data/test/active_model_serializers/logging_test.rb +8 -8
  106. data/test/active_model_serializers/railtie_test_isolated.rb +6 -0
  107. data/test/active_model_serializers/serialization_context_test_isolated.rb +58 -0
  108. data/test/adapter/deprecation_test.rb +100 -0
  109. data/test/adapter/json/belongs_to_test.rb +32 -34
  110. data/test/adapter/json/collection_test.rb +73 -75
  111. data/test/adapter/json/has_many_test.rb +36 -38
  112. data/test/adapter/json/transform_test.rb +93 -0
  113. data/test/adapter/json_api/belongs_to_test.rb +127 -129
  114. data/test/adapter/json_api/collection_test.rb +80 -82
  115. data/test/adapter/json_api/errors_test.rb +78 -0
  116. data/test/adapter/json_api/fields_test.rb +68 -70
  117. data/test/adapter/json_api/has_many_embed_ids_test.rb +32 -34
  118. data/test/adapter/json_api/has_many_explicit_serializer_test.rb +75 -77
  119. data/test/adapter/json_api/has_many_test.rb +121 -123
  120. data/test/adapter/json_api/has_one_test.rb +59 -61
  121. data/test/adapter/json_api/json_api_test.rb +28 -30
  122. data/test/adapter/json_api/linked_test.rb +319 -321
  123. data/test/adapter/json_api/links_test.rb +75 -50
  124. data/test/adapter/json_api/pagination_links_test.rb +115 -82
  125. data/test/adapter/json_api/parse_test.rb +114 -116
  126. data/test/adapter/json_api/relationship_test.rb +161 -0
  127. data/test/adapter/json_api/relationships_test.rb +199 -0
  128. data/test/adapter/json_api/resource_identifier_test.rb +85 -0
  129. data/test/adapter/json_api/resource_meta_test.rb +100 -0
  130. data/test/adapter/json_api/toplevel_jsonapi_test.rb +61 -63
  131. data/test/adapter/json_api/transform_test.rb +500 -0
  132. data/test/adapter/json_api/type_test.rb +61 -0
  133. data/test/adapter/json_test.rb +35 -37
  134. data/test/adapter/null_test.rb +13 -15
  135. data/test/adapter/polymorphic_test.rb +72 -0
  136. data/test/adapter_test.rb +27 -29
  137. data/test/array_serializer_test.rb +7 -8
  138. data/test/benchmark/app.rb +65 -0
  139. data/test/benchmark/benchmarking_support.rb +67 -0
  140. data/test/benchmark/bm_caching.rb +117 -0
  141. data/test/benchmark/bm_transform.rb +34 -0
  142. data/test/benchmark/config.ru +3 -0
  143. data/test/benchmark/controllers.rb +77 -0
  144. data/test/benchmark/fixtures.rb +167 -0
  145. data/test/cache_test.rb +388 -0
  146. data/test/collection_serializer_test.rb +10 -0
  147. data/test/fixtures/active_record.rb +12 -0
  148. data/test/fixtures/poro.rb +28 -3
  149. data/test/grape_test.rb +5 -5
  150. data/test/lint_test.rb +9 -0
  151. data/test/serializable_resource_test.rb +59 -3
  152. data/test/serializers/associations_test.rb +8 -8
  153. data/test/serializers/attribute_test.rb +7 -7
  154. data/test/serializers/caching_configuration_test_isolated.rb +170 -0
  155. data/test/serializers/meta_test.rb +74 -6
  156. data/test/serializers/read_attribute_for_serialization_test.rb +79 -0
  157. data/test/serializers/serialization_test.rb +55 -0
  158. data/test/support/isolated_unit.rb +3 -0
  159. data/test/support/rails5_shims.rb +26 -8
  160. data/test/support/rails_app.rb +38 -18
  161. data/test/support/serialization_testing.rb +5 -5
  162. data/test/test_helper.rb +6 -10
  163. metadata +132 -37
  164. data/docs/DESIGN.textile +7 -1
  165. data/lib/active_model/serializer/adapter/cached_serializer.rb +0 -45
  166. data/lib/active_model/serializer/adapter/fragment_cache.rb +0 -111
  167. data/lib/active_model/serializer/adapter/json/fragment_cache.rb +0 -13
  168. data/lib/active_model/serializer/adapter/json_api/deserialization.rb +0 -207
  169. data/lib/active_model/serializer/adapter/json_api/fragment_cache.rb +0 -21
  170. data/lib/active_model/serializer/adapter/json_api/link.rb +0 -44
  171. data/lib/active_model/serializer/adapter/json_api/pagination_links.rb +0 -58
  172. data/test/active_model_serializers/serialization_context_test.rb +0 -18
  173. data/test/adapter/fragment_cache_test.rb +0 -38
  174. data/test/adapter/json_api/resource_type_config_test.rb +0 -71
  175. data/test/serializers/adapter_for_test.rb +0 -166
  176. data/test/serializers/cache_test.rb +0 -209
  177. data/test/support/simplecov.rb +0 -6
  178. data/test/support/stream_capture.rb +0 -50
  179. data/test/support/test_case.rb +0 -19
@@ -3,6 +3,10 @@ require 'test_helper'
3
3
  module ActiveModel
4
4
  class Serializer
5
5
  class CollectionSerializerTest < ActiveSupport::TestCase
6
+ MessagesSerializer = Class.new(ActiveModel::Serializer) do
7
+ type 'messages'
8
+ end
9
+
6
10
  def setup
7
11
  @comment = Comment.new
8
12
  @post = Post.new
@@ -84,6 +88,12 @@ module ActiveModel
84
88
  assert_nil serializer.json_key
85
89
  end
86
90
 
91
+ def test_json_key_with_empty_resources_with_serializer
92
+ resource = []
93
+ serializer = collection_serializer.new(resource, serializer: MessagesSerializer)
94
+ assert_equal 'messages', serializer.json_key
95
+ end
96
+
87
97
  def test_json_key_with_root
88
98
  expected = 'custom_root'
89
99
  serializer = collection_serializer.new(@resource, root: expected)
@@ -2,6 +2,7 @@ require 'active_record'
2
2
 
3
3
  ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: ':memory:')
4
4
  ActiveRecord::Schema.define do
5
+ self.verbose = false
5
6
  create_table :posts, force: true do |t|
6
7
  t.string :title
7
8
  t.text :body
@@ -18,6 +19,17 @@ ActiveRecord::Schema.define do
18
19
  t.references :post
19
20
  t.timestamp null: false
20
21
  end
22
+ create_table :employees, force: true do |t|
23
+ t.string :name
24
+ t.string :email
25
+ t.timestamp null: false
26
+ end
27
+ create_table :pictures, force: true do |t|
28
+ t.string :title
29
+ t.string :imageable_type
30
+ t.string :imageable_id
31
+ t.timestamp null: false
32
+ end
21
33
  end
22
34
 
23
35
  module ARModels
@@ -21,10 +21,17 @@ class Model < ActiveModelSerializers::Model
21
21
  def respond_to_missing?(method_name, _include_private = false)
22
22
  attributes.key?(method_name.to_s.tr('=', '').to_sym) || super
23
23
  end
24
+ end
24
25
 
25
- def cache_key_with_digest
26
- "#{cache_key}/#{FILE_DIGEST}"
27
- end
26
+ # see
27
+ # https://github.com/rails/rails/blob/4-2-stable/activemodel/lib/active_model/errors.rb
28
+ # The below allows you to do:
29
+ #
30
+ # model = ModelWithErrors.new
31
+ # model.validate! # => ["cannot be nil"]
32
+ # model.errors.full_messages # => ["name cannot be nil"]
33
+ class ModelWithErrors < ::ActiveModelSerializers::Model
34
+ attr_accessor :name
28
35
  end
29
36
 
30
37
  class Profile < Model
@@ -61,6 +68,14 @@ Comment = Class.new(Model) do
61
68
  end
62
69
  end
63
70
 
71
+ class Employee < ActiveRecord::Base
72
+ has_many :pictures, as: :imageable
73
+ end
74
+
75
+ class Picture < ActiveRecord::Base
76
+ belongs_to :imageable, polymorphic: true
77
+ end
78
+
64
79
  module Spam; end
65
80
  Spam::UnrelatedLink = Class.new(Model)
66
81
 
@@ -222,6 +237,16 @@ VirtualValueSerializer = Class.new(ActiveModel::Serializer) do
222
237
  end
223
238
  end
224
239
 
240
+ PolymorphicHasManySerializer = Class.new(ActiveModel::Serializer) do
241
+ attributes :id, :name
242
+ end
243
+
244
+ PolymorphicBelongsToSerializer = Class.new(ActiveModel::Serializer) do
245
+ attributes :id, :title
246
+
247
+ has_one :imageable, serializer: PolymorphicHasManySerializer
248
+ end
249
+
225
250
  Spam::UnrelatedLinkSerializer = Class.new(ActiveModel::Serializer) do
226
251
  cache only: [:id]
227
252
  attributes :id
@@ -15,11 +15,11 @@ class ActiveModelSerializers::GrapeTest < ActiveSupport::TestCase
15
15
 
16
16
  def self.all
17
17
  @all ||=
18
- begin
19
- model1.save!
20
- model2.save!
21
- ARModels::Post.all
22
- end
18
+ begin
19
+ model1.save!
20
+ model2.save!
21
+ ARModels::Post.all
22
+ end
23
23
  end
24
24
  end
25
25
 
@@ -27,6 +27,15 @@ module ActiveModel
27
27
  def updated_at
28
28
  end
29
29
 
30
+ def errors
31
+ end
32
+
33
+ def self.human_attribute_name(attr, options = {})
34
+ end
35
+
36
+ def self.lookup_ancestors
37
+ end
38
+
30
39
  def self.model_name
31
40
  @_model_name ||= ActiveModel::Name.new(self)
32
41
  end
@@ -1,12 +1,19 @@
1
1
  require 'test_helper'
2
2
 
3
- module ActiveModel
3
+ module ActiveModelSerializers
4
4
  class SerializableResourceTest < ActiveSupport::TestCase
5
5
  def setup
6
6
  @resource = Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' })
7
7
  @serializer = ProfileSerializer.new(@resource)
8
- @adapter = ActiveModel::Serializer::Adapter.create(@serializer)
9
- @serializable_resource = ActiveModel::SerializableResource.new(@resource)
8
+ @adapter = ActiveModelSerializers::Adapter.create(@serializer)
9
+ @serializable_resource = SerializableResource.new(@resource)
10
+ end
11
+
12
+ def test_deprecation
13
+ assert_output(nil, /deprecated/) do
14
+ deprecated_serializable_resource = ActiveModel::SerializableResource.new(@resource)
15
+ assert_equal(@serializable_resource.as_json, deprecated_serializable_resource.as_json)
16
+ end
10
17
  end
11
18
 
12
19
  def test_serializable_resource_delegates_serializable_hash_to_the_adapter
@@ -23,5 +30,54 @@ module ActiveModel
23
30
  options = nil
24
31
  assert_equal @adapter.as_json(options), @serializable_resource.as_json(options)
25
32
  end
33
+
34
+ def test_use_adapter_with_adapter_option
35
+ assert SerializableResource.new(@resource, { adapter: 'json' }).use_adapter?
36
+ end
37
+
38
+ def test_use_adapter_with_adapter_option_as_false
39
+ refute SerializableResource.new(@resource, { adapter: false }).use_adapter?
40
+ end
41
+
42
+ class SerializableResourceErrorsTest < Minitest::Test
43
+ def test_serializable_resource_with_errors
44
+ options = nil
45
+ resource = ModelWithErrors.new
46
+ resource.errors.add(:name, 'must be awesome')
47
+ serializable_resource = ActiveModelSerializers::SerializableResource.new(
48
+ resource, {
49
+ serializer: ActiveModel::Serializer::ErrorSerializer,
50
+ adapter: :json_api
51
+ })
52
+ expected_response_document =
53
+ { :errors =>
54
+ [
55
+ { :source => { :pointer => '/data/attributes/name' }, :detail => 'must be awesome' }
56
+ ]
57
+ }
58
+ assert_equal serializable_resource.as_json(options), expected_response_document
59
+ end
60
+
61
+ def test_serializable_resource_with_collection_containing_errors
62
+ options = nil
63
+ resources = []
64
+ resources << resource = ModelWithErrors.new
65
+ resource.errors.add(:title, 'must be amazing')
66
+ resources << ModelWithErrors.new
67
+ serializable_resource = SerializableResource.new(
68
+ resources, {
69
+ serializer: ActiveModel::Serializer::ErrorsSerializer,
70
+ each_serializer: ActiveModel::Serializer::ErrorSerializer,
71
+ adapter: :json_api
72
+ })
73
+ expected_response_document =
74
+ { :errors =>
75
+ [
76
+ { :source => { :pointer => '/data/attributes/title' }, :detail => 'must be amazing' }
77
+ ]
78
+ }
79
+ assert_equal serializable_resource.as_json(options), expected_response_document
80
+ end
81
+ end
26
82
  end
27
83
  end
@@ -32,13 +32,13 @@ module ActiveModel
32
32
 
33
33
  case key
34
34
  when :posts
35
- assert_equal({}, options)
35
+ assert_equal({ include_data: true }, options)
36
36
  assert_kind_of(ActiveModelSerializers.config.collection_serializer, serializer)
37
37
  when :bio
38
- assert_equal({}, options)
38
+ assert_equal({ include_data: true }, options)
39
39
  assert_nil serializer
40
40
  when :roles
41
- assert_equal({}, options)
41
+ assert_equal({ include_data: true }, options)
42
42
  assert_kind_of(ActiveModelSerializers.config.collection_serializer, serializer)
43
43
  else
44
44
  flunk "Unknown association: #{key}"
@@ -60,8 +60,8 @@ module ActiveModel
60
60
 
61
61
  def test_serializer_options_are_passed_into_associations_serializers
62
62
  association = @post_serializer
63
- .associations
64
- .detect { |assoc| assoc.key == :comments }
63
+ .associations
64
+ .detect { |assoc| assoc.key == :comments }
65
65
 
66
66
  assert association.serializer.first.custom_options[:custom_options]
67
67
  end
@@ -80,7 +80,7 @@ module ActiveModel
80
80
  flunk "Unknown association: #{key}"
81
81
  end
82
82
 
83
- assert_equal({}, association.options)
83
+ assert_equal({ include_data: true }, association.options)
84
84
  end
85
85
  end
86
86
 
@@ -143,11 +143,11 @@ module ActiveModel
143
143
  )
144
144
  actual = serializable(post, adapter: :attributes, serializer: InlineAssociationTestPostSerializer).as_json
145
145
  expected = {
146
- :comments => [
146
+ :comments => [
147
147
  { :id => 1, :contents => 'first comment' },
148
148
  { :id => 2, :contents => 'last comment' }
149
149
  ],
150
- :last_comments => [
150
+ :last_comments => [
151
151
  { :id => 2, :contents => 'last comment' }
152
152
  ]
153
153
  }
@@ -14,14 +14,14 @@ module ActiveModel
14
14
  end
15
15
 
16
16
  def test_json_serializable_hash
17
- adapter = ActiveModel::Serializer::Adapter::Json.new(@blog_serializer)
17
+ adapter = ActiveModelSerializers::Adapter::Json.new(@blog_serializer)
18
18
  assert_equal({ blog: { id: 1, title: 'AMS Hints' } }, adapter.serializable_hash)
19
19
  end
20
20
 
21
21
  def test_attribute_inheritance_with_key
22
22
  inherited_klass = Class.new(AlternateBlogSerializer)
23
23
  blog_serializer = inherited_klass.new(@blog)
24
- adapter = ActiveModel::Serializer::Adapter::Attributes.new(blog_serializer)
24
+ adapter = ActiveModelSerializers::Adapter::Attributes.new(blog_serializer)
25
25
  assert_equal({ :id => 1, :title => 'AMS Hints' }, adapter.serializable_hash)
26
26
  end
27
27
 
@@ -39,7 +39,7 @@ module ActiveModel
39
39
  attribute :name, key: :id
40
40
  end
41
41
 
42
- adapter = ActiveModel::Serializer::Adapter::Json.new(serializer.new(@blog))
42
+ adapter = ActiveModelSerializers::Adapter::Json.new(serializer.new(@blog))
43
43
  assert_equal({ blog: { id: 'AMS Hints' } }, adapter.serializable_hash)
44
44
  end
45
45
 
@@ -48,7 +48,7 @@ module ActiveModel
48
48
  attribute :name, key: :object
49
49
  end
50
50
 
51
- adapter = ActiveModel::Serializer::Adapter::Json.new(serializer.new(@blog))
51
+ adapter = ActiveModelSerializers::Adapter::Json.new(serializer.new(@blog))
52
52
  assert_equal({ blog: { object: 'AMS Hints' } }, adapter.serializable_hash)
53
53
  end
54
54
 
@@ -60,10 +60,10 @@ module ActiveModel
60
60
  attributes :type
61
61
  end
62
62
 
63
- adapter = ActiveModel::Serializer::Adapter::Json.new(attribute_serializer.new(@blog))
63
+ adapter = ActiveModelSerializers::Adapter::Json.new(attribute_serializer.new(@blog))
64
64
  assert_equal({ blog: { type: 1 } }, adapter.serializable_hash)
65
65
 
66
- adapter = ActiveModel::Serializer::Adapter::Json.new(attributes_serializer.new(@blog))
66
+ adapter = ActiveModelSerializers::Adapter::Json.new(attributes_serializer.new(@blog))
67
67
  assert_equal({ blog: { type: 'stuff' } }, adapter.serializable_hash)
68
68
  end
69
69
 
@@ -76,7 +76,7 @@ module ActiveModel
76
76
  attribute :id
77
77
  end
78
78
 
79
- hash = ActiveModel::SerializableResource.new(@blog, adapter: :json, serializer: serializer).serializable_hash
79
+ hash = ActiveModelSerializers::SerializableResource.new(@blog, adapter: :json, serializer: serializer).serializable_hash
80
80
 
81
81
  assert_equal('custom', hash[:blog][:id])
82
82
  end
@@ -0,0 +1,170 @@
1
+ # Execute this test in isolation
2
+ require 'support/isolated_unit'
3
+
4
+ class CachingConfigurationTest < ActiveSupport::TestCase
5
+ include ActiveSupport::Testing::Isolation
6
+
7
+ setup do
8
+ require 'rails'
9
+ # AMS needs to be required before Rails.application is initialized for
10
+ # Railtie's to fire in Rails.application.initialize!
11
+ # (and make_basic_app initializes the app)
12
+ require 'active_model_serializers'
13
+ # Create serializers before Rails.application.initialize!
14
+ # To ensure we're testing that the cache settings depend on
15
+ # the Railtie firing, not on the ActionController being loaded.
16
+ create_serializers
17
+ end
18
+
19
+ def create_serializers
20
+ @cached_serializer = Class.new(ActiveModel::Serializer) do
21
+ cache skip_digest: true
22
+ attributes :id, :name, :title
23
+ end
24
+ @fragment_cached_serializer = Class.new(ActiveModel::Serializer) do
25
+ cache only: :id
26
+ attributes :id, :name, :title
27
+ end
28
+ @non_cached_serializer = Class.new(ActiveModel::Serializer) do
29
+ attributes :id, :name, :title
30
+ end
31
+ end
32
+
33
+ class PerformCachingTrue < CachingConfigurationTest
34
+ setup do
35
+ # Let's make that Rails app and initialize it!
36
+ make_basic_app do |app|
37
+ app.config.action_controller.perform_caching = true
38
+ app.config.action_controller.cache_store = ActiveSupport::Cache.lookup_store(:memory_store)
39
+ end
40
+ controller_cache_store # Force ActiveSupport.on_load(:action_controller) to run
41
+ end
42
+
43
+ test 'it sets perform_caching to true on AMS.config and serializers' do
44
+ assert Rails.configuration.action_controller.perform_caching
45
+ assert ActiveModelSerializers.config.perform_caching
46
+ assert ActiveModel::Serializer.perform_caching?
47
+ assert @cached_serializer.perform_caching?
48
+ assert @non_cached_serializer.perform_caching?
49
+ assert @fragment_cached_serializer.perform_caching?
50
+ end
51
+
52
+ test 'it sets the AMS.config.cache_store to the controller cache_store' do
53
+ assert_equal controller_cache_store, ActiveSupport::Cache::MemoryStore
54
+ assert_equal controller_cache_store, ActiveModelSerializers.config.cache_store.class
55
+ end
56
+
57
+ test 'it sets the cached serializer cache_store to the ActionController::Base.cache_store' do
58
+ assert_equal ActiveSupport::Cache::NullStore, @cached_serializer._cache.class
59
+ assert_equal controller_cache_store, @cached_serializer.cache_store.class
60
+ assert_equal ActiveSupport::Cache::MemoryStore, @cached_serializer._cache.class
61
+ end
62
+
63
+ test 'the cached serializer has cache_enabled?' do
64
+ assert @cached_serializer.cache_enabled?
65
+ end
66
+
67
+ test 'the cached serializer does not have fragment_cache_enabled?' do
68
+ refute @cached_serializer.fragment_cache_enabled?
69
+ end
70
+
71
+ test 'the non-cached serializer cache_store is nil' do
72
+ assert_equal nil, @non_cached_serializer._cache
73
+ assert_equal nil, @non_cached_serializer.cache_store
74
+ assert_equal nil, @non_cached_serializer._cache
75
+ end
76
+
77
+ test 'the non-cached serializer does not have cache_enabled?' do
78
+ refute @non_cached_serializer.cache_enabled?
79
+ end
80
+
81
+ test 'the non-cached serializer does not have fragment_cache_enabled?' do
82
+ refute @non_cached_serializer.fragment_cache_enabled?
83
+ end
84
+
85
+ test 'it sets the fragment cached serializer cache_store to the ActionController::Base.cache_store' do
86
+ assert_equal ActiveSupport::Cache::NullStore, @fragment_cached_serializer._cache.class
87
+ assert_equal controller_cache_store, @fragment_cached_serializer.cache_store.class
88
+ assert_equal ActiveSupport::Cache::MemoryStore, @fragment_cached_serializer._cache.class
89
+ end
90
+
91
+ test 'the fragment cached serializer does not have cache_enabled?' do
92
+ refute @fragment_cached_serializer.cache_enabled?
93
+ end
94
+
95
+ test 'the fragment cached serializer has fragment_cache_enabled?' do
96
+ assert @fragment_cached_serializer.fragment_cache_enabled?
97
+ end
98
+ end
99
+
100
+ class PerformCachingFalse < CachingConfigurationTest
101
+ setup do
102
+ # Let's make that Rails app and initialize it!
103
+ make_basic_app do |app|
104
+ app.config.action_controller.perform_caching = false
105
+ app.config.action_controller.cache_store = ActiveSupport::Cache.lookup_store(:memory_store)
106
+ end
107
+ controller_cache_store # Force ActiveSupport.on_load(:action_controller) to run
108
+ end
109
+
110
+ test 'it sets perform_caching to false on AMS.config and serializers' do
111
+ refute Rails.configuration.action_controller.perform_caching
112
+ refute ActiveModelSerializers.config.perform_caching
113
+ refute ActiveModel::Serializer.perform_caching?
114
+ refute @cached_serializer.perform_caching?
115
+ refute @non_cached_serializer.perform_caching?
116
+ refute @fragment_cached_serializer.perform_caching?
117
+ end
118
+
119
+ test 'it sets the AMS.config.cache_store to the controller cache_store' do
120
+ assert_equal controller_cache_store, ActiveSupport::Cache::MemoryStore
121
+ assert_equal controller_cache_store, ActiveModelSerializers.config.cache_store.class
122
+ end
123
+
124
+ test 'it sets the cached serializer cache_store to the ActionController::Base.cache_store' do
125
+ assert_equal ActiveSupport::Cache::NullStore, @cached_serializer._cache.class
126
+ assert_equal controller_cache_store, @cached_serializer.cache_store.class
127
+ assert_equal ActiveSupport::Cache::MemoryStore, @cached_serializer._cache.class
128
+ end
129
+
130
+ test 'the cached serializer does not have cache_enabled?' do
131
+ refute @cached_serializer.cache_enabled?
132
+ end
133
+
134
+ test 'the cached serializer does not have fragment_cache_enabled?' do
135
+ refute @cached_serializer.fragment_cache_enabled?
136
+ end
137
+
138
+ test 'the non-cached serializer cache_store is nil' do
139
+ assert_equal nil, @non_cached_serializer._cache
140
+ assert_equal nil, @non_cached_serializer.cache_store
141
+ assert_equal nil, @non_cached_serializer._cache
142
+ end
143
+
144
+ test 'the non-cached serializer does not have cache_enabled?' do
145
+ refute @non_cached_serializer.cache_enabled?
146
+ end
147
+
148
+ test 'the non-cached serializer does not have fragment_cache_enabled?' do
149
+ refute @non_cached_serializer.fragment_cache_enabled?
150
+ end
151
+
152
+ test 'it sets the fragment cached serializer cache_store to the ActionController::Base.cache_store' do
153
+ assert_equal ActiveSupport::Cache::NullStore, @fragment_cached_serializer._cache.class
154
+ assert_equal controller_cache_store, @fragment_cached_serializer.cache_store.class
155
+ assert_equal ActiveSupport::Cache::MemoryStore, @fragment_cached_serializer._cache.class
156
+ end
157
+
158
+ test 'the fragment cached serializer does not have cache_enabled?' do
159
+ refute @fragment_cached_serializer.cache_enabled?
160
+ end
161
+
162
+ test 'the fragment cached serializer does not have fragment_cache_enabled?' do
163
+ refute @fragment_cached_serializer.fragment_cache_enabled?
164
+ end
165
+ end
166
+
167
+ def controller_cache_store
168
+ ActionController::Base.cache_store.class
169
+ end
170
+ end