active_model_serializers_custom 0.10.90

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 +7 -0
  2. data/.github/ISSUE_TEMPLATE.md +29 -0
  3. data/.github/PULL_REQUEST_TEMPLATE.md +15 -0
  4. data/.gitignore +35 -0
  5. data/.rubocop.yml +109 -0
  6. data/.simplecov +110 -0
  7. data/.travis.yml +63 -0
  8. data/CHANGELOG.md +727 -0
  9. data/CODE_OF_CONDUCT.md +74 -0
  10. data/CONTRIBUTING.md +105 -0
  11. data/Gemfile +74 -0
  12. data/MIT-LICENSE +22 -0
  13. data/README.md +305 -0
  14. data/Rakefile +76 -0
  15. data/active_model_serializers.gemspec +64 -0
  16. data/appveyor.yml +28 -0
  17. data/bin/bench +171 -0
  18. data/bin/bench_regression +316 -0
  19. data/bin/rubocop +38 -0
  20. data/bin/serve_benchmark +39 -0
  21. data/docs/README.md +41 -0
  22. data/docs/STYLE.md +58 -0
  23. data/docs/general/adapters.md +269 -0
  24. data/docs/general/caching.md +58 -0
  25. data/docs/general/configuration_options.md +185 -0
  26. data/docs/general/deserialization.md +100 -0
  27. data/docs/general/fields.md +31 -0
  28. data/docs/general/getting_started.md +133 -0
  29. data/docs/general/instrumentation.md +40 -0
  30. data/docs/general/key_transforms.md +40 -0
  31. data/docs/general/logging.md +21 -0
  32. data/docs/general/rendering.md +293 -0
  33. data/docs/general/serializers.md +495 -0
  34. data/docs/how-open-source-maintained.jpg +0 -0
  35. data/docs/howto/add_pagination_links.md +138 -0
  36. data/docs/howto/add_relationship_links.md +140 -0
  37. data/docs/howto/add_root_key.md +62 -0
  38. data/docs/howto/grape_integration.md +42 -0
  39. data/docs/howto/outside_controller_use.md +66 -0
  40. data/docs/howto/passing_arbitrary_options.md +27 -0
  41. data/docs/howto/serialize_poro.md +73 -0
  42. data/docs/howto/test.md +154 -0
  43. data/docs/howto/upgrade_from_0_8_to_0_10.md +265 -0
  44. data/docs/integrations/ember-and-json-api.md +147 -0
  45. data/docs/integrations/grape.md +19 -0
  46. data/docs/jsonapi/errors.md +56 -0
  47. data/docs/jsonapi/schema.md +151 -0
  48. data/docs/jsonapi/schema/schema.json +366 -0
  49. data/docs/rfcs/0000-namespace.md +106 -0
  50. data/docs/rfcs/template.md +15 -0
  51. data/lib/action_controller/serialization.rb +76 -0
  52. data/lib/active_model/serializable_resource.rb +13 -0
  53. data/lib/active_model/serializer.rb +418 -0
  54. data/lib/active_model/serializer/adapter.rb +26 -0
  55. data/lib/active_model/serializer/adapter/attributes.rb +17 -0
  56. data/lib/active_model/serializer/adapter/base.rb +20 -0
  57. data/lib/active_model/serializer/adapter/json.rb +17 -0
  58. data/lib/active_model/serializer/adapter/json_api.rb +17 -0
  59. data/lib/active_model/serializer/adapter/null.rb +17 -0
  60. data/lib/active_model/serializer/array_serializer.rb +14 -0
  61. data/lib/active_model/serializer/association.rb +91 -0
  62. data/lib/active_model/serializer/attribute.rb +27 -0
  63. data/lib/active_model/serializer/belongs_to_reflection.rb +13 -0
  64. data/lib/active_model/serializer/collection_serializer.rb +90 -0
  65. data/lib/active_model/serializer/concerns/caching.rb +304 -0
  66. data/lib/active_model/serializer/error_serializer.rb +16 -0
  67. data/lib/active_model/serializer/errors_serializer.rb +34 -0
  68. data/lib/active_model/serializer/field.rb +92 -0
  69. data/lib/active_model/serializer/fieldset.rb +33 -0
  70. data/lib/active_model/serializer/has_many_reflection.rb +12 -0
  71. data/lib/active_model/serializer/has_one_reflection.rb +9 -0
  72. data/lib/active_model/serializer/lazy_association.rb +99 -0
  73. data/lib/active_model/serializer/link.rb +23 -0
  74. data/lib/active_model/serializer/lint.rb +152 -0
  75. data/lib/active_model/serializer/null.rb +19 -0
  76. data/lib/active_model/serializer/reflection.rb +212 -0
  77. data/lib/active_model/serializer/version.rb +7 -0
  78. data/lib/active_model_serializers.rb +63 -0
  79. data/lib/active_model_serializers/adapter.rb +100 -0
  80. data/lib/active_model_serializers/adapter/attributes.rb +15 -0
  81. data/lib/active_model_serializers/adapter/base.rb +85 -0
  82. data/lib/active_model_serializers/adapter/json.rb +23 -0
  83. data/lib/active_model_serializers/adapter/json_api.rb +535 -0
  84. data/lib/active_model_serializers/adapter/json_api/deserialization.rb +215 -0
  85. data/lib/active_model_serializers/adapter/json_api/error.rb +98 -0
  86. data/lib/active_model_serializers/adapter/json_api/jsonapi.rb +51 -0
  87. data/lib/active_model_serializers/adapter/json_api/link.rb +85 -0
  88. data/lib/active_model_serializers/adapter/json_api/meta.rb +39 -0
  89. data/lib/active_model_serializers/adapter/json_api/pagination_links.rb +90 -0
  90. data/lib/active_model_serializers/adapter/json_api/relationship.rb +106 -0
  91. data/lib/active_model_serializers/adapter/json_api/resource_identifier.rb +68 -0
  92. data/lib/active_model_serializers/adapter/null.rb +11 -0
  93. data/lib/active_model_serializers/callbacks.rb +57 -0
  94. data/lib/active_model_serializers/deprecate.rb +56 -0
  95. data/lib/active_model_serializers/deserialization.rb +17 -0
  96. data/lib/active_model_serializers/json_pointer.rb +16 -0
  97. data/lib/active_model_serializers/logging.rb +124 -0
  98. data/lib/active_model_serializers/lookup_chain.rb +82 -0
  99. data/lib/active_model_serializers/model.rb +132 -0
  100. data/lib/active_model_serializers/railtie.rb +52 -0
  101. data/lib/active_model_serializers/register_jsonapi_renderer.rb +80 -0
  102. data/lib/active_model_serializers/serializable_resource.rb +84 -0
  103. data/lib/active_model_serializers/serialization_context.rb +41 -0
  104. data/lib/active_model_serializers/test.rb +9 -0
  105. data/lib/active_model_serializers/test/schema.rb +140 -0
  106. data/lib/active_model_serializers/test/serializer.rb +127 -0
  107. data/lib/generators/rails/USAGE +6 -0
  108. data/lib/generators/rails/resource_override.rb +12 -0
  109. data/lib/generators/rails/serializer_generator.rb +38 -0
  110. data/lib/generators/rails/templates/serializer.rb.erb +8 -0
  111. data/lib/grape/active_model_serializers.rb +18 -0
  112. data/lib/grape/formatters/active_model_serializers.rb +34 -0
  113. data/lib/grape/helpers/active_model_serializers.rb +19 -0
  114. data/lib/tasks/rubocop.rake +55 -0
  115. data/test/action_controller/adapter_selector_test.rb +64 -0
  116. data/test/action_controller/explicit_serializer_test.rb +137 -0
  117. data/test/action_controller/json/include_test.rb +248 -0
  118. data/test/action_controller/json_api/deserialization_test.rb +114 -0
  119. data/test/action_controller/json_api/errors_test.rb +42 -0
  120. data/test/action_controller/json_api/fields_test.rb +68 -0
  121. data/test/action_controller/json_api/linked_test.rb +204 -0
  122. data/test/action_controller/json_api/pagination_test.rb +126 -0
  123. data/test/action_controller/json_api/transform_test.rb +191 -0
  124. data/test/action_controller/lookup_proc_test.rb +51 -0
  125. data/test/action_controller/namespace_lookup_test.rb +239 -0
  126. data/test/action_controller/serialization_scope_name_test.rb +237 -0
  127. data/test/action_controller/serialization_test.rb +480 -0
  128. data/test/active_model_serializers/adapter_for_test.rb +210 -0
  129. data/test/active_model_serializers/json_pointer_test.rb +24 -0
  130. data/test/active_model_serializers/logging_test.rb +79 -0
  131. data/test/active_model_serializers/model_test.rb +144 -0
  132. data/test/active_model_serializers/railtie_test_isolated.rb +70 -0
  133. data/test/active_model_serializers/register_jsonapi_renderer_test_isolated.rb +163 -0
  134. data/test/active_model_serializers/serialization_context_test_isolated.rb +73 -0
  135. data/test/active_model_serializers/test/schema_test.rb +133 -0
  136. data/test/active_model_serializers/test/serializer_test.rb +64 -0
  137. data/test/active_record_test.rb +11 -0
  138. data/test/adapter/attributes_test.rb +42 -0
  139. data/test/adapter/deprecation_test.rb +102 -0
  140. data/test/adapter/json/belongs_to_test.rb +47 -0
  141. data/test/adapter/json/collection_test.rb +106 -0
  142. data/test/adapter/json/has_many_test.rb +55 -0
  143. data/test/adapter/json/transform_test.rb +95 -0
  144. data/test/adapter/json_api/belongs_to_test.rb +157 -0
  145. data/test/adapter/json_api/collection_test.rb +98 -0
  146. data/test/adapter/json_api/errors_test.rb +78 -0
  147. data/test/adapter/json_api/fields_test.rb +98 -0
  148. data/test/adapter/json_api/has_many_explicit_serializer_test.rb +98 -0
  149. data/test/adapter/json_api/has_many_test.rb +175 -0
  150. data/test/adapter/json_api/has_one_test.rb +82 -0
  151. data/test/adapter/json_api/include_data_if_sideloaded_test.rb +215 -0
  152. data/test/adapter/json_api/json_api_test.rb +35 -0
  153. data/test/adapter/json_api/linked_test.rb +415 -0
  154. data/test/adapter/json_api/links_test.rb +112 -0
  155. data/test/adapter/json_api/pagination_links_test.rb +208 -0
  156. data/test/adapter/json_api/parse_test.rb +139 -0
  157. data/test/adapter/json_api/relationship_test.rb +399 -0
  158. data/test/adapter/json_api/resource_meta_test.rb +102 -0
  159. data/test/adapter/json_api/toplevel_jsonapi_test.rb +84 -0
  160. data/test/adapter/json_api/transform_test.rb +514 -0
  161. data/test/adapter/json_api/type_test.rb +195 -0
  162. data/test/adapter/json_test.rb +48 -0
  163. data/test/adapter/null_test.rb +24 -0
  164. data/test/adapter/polymorphic_test.rb +220 -0
  165. data/test/adapter_test.rb +69 -0
  166. data/test/array_serializer_test.rb +24 -0
  167. data/test/benchmark/app.rb +67 -0
  168. data/test/benchmark/benchmarking_support.rb +69 -0
  169. data/test/benchmark/bm_active_record.rb +83 -0
  170. data/test/benchmark/bm_adapter.rb +40 -0
  171. data/test/benchmark/bm_caching.rb +121 -0
  172. data/test/benchmark/bm_lookup_chain.rb +85 -0
  173. data/test/benchmark/bm_transform.rb +47 -0
  174. data/test/benchmark/config.ru +3 -0
  175. data/test/benchmark/controllers.rb +85 -0
  176. data/test/benchmark/fixtures.rb +221 -0
  177. data/test/cache_test.rb +717 -0
  178. data/test/collection_serializer_test.rb +129 -0
  179. data/test/fixtures/active_record.rb +115 -0
  180. data/test/fixtures/poro.rb +227 -0
  181. data/test/generators/scaffold_controller_generator_test.rb +26 -0
  182. data/test/generators/serializer_generator_test.rb +77 -0
  183. data/test/grape_test.rb +198 -0
  184. data/test/lint_test.rb +51 -0
  185. data/test/logger_test.rb +22 -0
  186. data/test/poro_test.rb +11 -0
  187. data/test/serializable_resource_test.rb +81 -0
  188. data/test/serializers/association_macros_test.rb +39 -0
  189. data/test/serializers/associations_test.rb +520 -0
  190. data/test/serializers/attribute_test.rb +155 -0
  191. data/test/serializers/attributes_test.rb +54 -0
  192. data/test/serializers/caching_configuration_test_isolated.rb +172 -0
  193. data/test/serializers/configuration_test.rb +34 -0
  194. data/test/serializers/fieldset_test.rb +16 -0
  195. data/test/serializers/meta_test.rb +204 -0
  196. data/test/serializers/options_test.rb +34 -0
  197. data/test/serializers/read_attribute_for_serialization_test.rb +81 -0
  198. data/test/serializers/reflection_test.rb +481 -0
  199. data/test/serializers/root_test.rb +23 -0
  200. data/test/serializers/serialization_test.rb +57 -0
  201. data/test/serializers/serializer_for_test.rb +138 -0
  202. data/test/serializers/serializer_for_with_namespace_test.rb +90 -0
  203. data/test/support/custom_schemas/active_model_serializers/test/schema_test/my/index.json +6 -0
  204. data/test/support/isolated_unit.rb +86 -0
  205. data/test/support/rails5_shims.rb +55 -0
  206. data/test/support/rails_app.rb +40 -0
  207. data/test/support/schemas/active_model_serializers/test/schema_test/my/index.json +6 -0
  208. data/test/support/schemas/active_model_serializers/test/schema_test/my/show.json +6 -0
  209. data/test/support/schemas/custom/show.json +7 -0
  210. data/test/support/schemas/hyper_schema.json +93 -0
  211. data/test/support/schemas/render_using_json_api.json +43 -0
  212. data/test/support/schemas/simple_json_pointers.json +10 -0
  213. data/test/support/serialization_testing.rb +81 -0
  214. data/test/test_helper.rb +72 -0
  215. metadata +622 -0
@@ -0,0 +1,155 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'test_helper'
4
+
5
+ module ActiveModel
6
+ class Serializer
7
+ class AttributeTest < ActiveSupport::TestCase
8
+ def setup
9
+ @blog = Blog.new(id: 1, name: 'AMS Hints', type: 'stuff')
10
+ @blog_serializer = AlternateBlogSerializer.new(@blog)
11
+ end
12
+
13
+ def test_attributes_definition
14
+ assert_equal([:id, :title],
15
+ @blog_serializer.class._attributes)
16
+ end
17
+
18
+ def test_json_serializable_hash
19
+ adapter = ActiveModelSerializers::Adapter::Json.new(@blog_serializer)
20
+ assert_equal({ blog: { id: 1, title: 'AMS Hints' } }, adapter.serializable_hash)
21
+ end
22
+
23
+ def test_attribute_inheritance_with_key
24
+ inherited_klass = Class.new(AlternateBlogSerializer)
25
+ blog_serializer = inherited_klass.new(@blog)
26
+ adapter = ActiveModelSerializers::Adapter::Attributes.new(blog_serializer)
27
+ assert_equal({ id: 1, title: 'AMS Hints' }, adapter.serializable_hash)
28
+ end
29
+
30
+ def test_multiple_calls_with_the_same_attribute
31
+ serializer_class = Class.new(ActiveModel::Serializer) do
32
+ attribute :title
33
+ attribute :title
34
+ end
35
+
36
+ assert_equal([:title], serializer_class._attributes)
37
+ end
38
+
39
+ def test_id_attribute_override
40
+ serializer = Class.new(ActiveModel::Serializer) do
41
+ attribute :name, key: :id
42
+ end
43
+
44
+ adapter = ActiveModelSerializers::Adapter::Json.new(serializer.new(@blog))
45
+ assert_equal({ blog: { id: 'AMS Hints' } }, adapter.serializable_hash)
46
+ end
47
+
48
+ def test_object_attribute_override
49
+ serializer = Class.new(ActiveModel::Serializer) do
50
+ attribute :name, key: :object
51
+ end
52
+
53
+ adapter = ActiveModelSerializers::Adapter::Json.new(serializer.new(@blog))
54
+ assert_equal({ blog: { object: 'AMS Hints' } }, adapter.serializable_hash)
55
+ end
56
+
57
+ def test_type_attribute
58
+ attribute_serializer = Class.new(ActiveModel::Serializer) do
59
+ attribute :id, key: :type
60
+ end
61
+ attributes_serializer = Class.new(ActiveModel::Serializer) do
62
+ attributes :type
63
+ end
64
+
65
+ adapter = ActiveModelSerializers::Adapter::Json.new(attribute_serializer.new(@blog))
66
+ assert_equal({ blog: { type: 1 } }, adapter.serializable_hash)
67
+
68
+ adapter = ActiveModelSerializers::Adapter::Json.new(attributes_serializer.new(@blog))
69
+ assert_equal({ blog: { type: 'stuff' } }, adapter.serializable_hash)
70
+ end
71
+
72
+ def test_id_attribute_override_before
73
+ serializer = Class.new(ActiveModel::Serializer) do
74
+ def id
75
+ 'custom'
76
+ end
77
+
78
+ attribute :id
79
+ end
80
+
81
+ hash = ActiveModelSerializers::SerializableResource.new(@blog, adapter: :json, serializer: serializer).serializable_hash
82
+
83
+ assert_equal('custom', hash[:blog][:id])
84
+ end
85
+
86
+ class PostWithVirtualAttribute < ::Model; attributes :first_name, :last_name end
87
+ class PostWithVirtualAttributeSerializer < ActiveModel::Serializer
88
+ attribute :name do
89
+ "#{object.first_name} #{object.last_name}"
90
+ end
91
+ end
92
+
93
+ def test_virtual_attribute_block
94
+ post = PostWithVirtualAttribute.new(first_name: 'Lucas', last_name: 'Hosseini')
95
+ hash = serializable(post).serializable_hash
96
+ expected = { name: 'Lucas Hosseini' }
97
+
98
+ assert_equal(expected, hash)
99
+ end
100
+
101
+ # rubocop:disable Metrics/AbcSize
102
+ def test_conditional_associations
103
+ model = Class.new(::Model) do
104
+ attributes :true, :false, :attribute
105
+ end.new(true: true, false: false)
106
+
107
+ scenarios = [
108
+ { options: { if: :true }, included: true },
109
+ { options: { if: :false }, included: false },
110
+ { options: { unless: :false }, included: true },
111
+ { options: { unless: :true }, included: false },
112
+ { options: { if: 'object.true' }, included: true },
113
+ { options: { if: 'object.false' }, included: false },
114
+ { options: { unless: 'object.false' }, included: true },
115
+ { options: { unless: 'object.true' }, included: false },
116
+ { options: { if: -> { object.true } }, included: true },
117
+ { options: { if: -> { object.false } }, included: false },
118
+ { options: { unless: -> { object.false } }, included: true },
119
+ { options: { unless: -> { object.true } }, included: false },
120
+ { options: { if: -> (s) { s.object.true } }, included: true },
121
+ { options: { if: -> (s) { s.object.false } }, included: false },
122
+ { options: { unless: -> (s) { s.object.false } }, included: true },
123
+ { options: { unless: -> (s) { s.object.true } }, included: false }
124
+ ]
125
+
126
+ scenarios.each do |s|
127
+ serializer = Class.new(ActiveModel::Serializer) do
128
+ attribute :attribute, s[:options]
129
+
130
+ def true
131
+ true
132
+ end
133
+
134
+ def false
135
+ false
136
+ end
137
+ end
138
+
139
+ hash = serializable(model, serializer: serializer).serializable_hash
140
+ assert_equal(s[:included], hash.key?(:attribute), "Error with #{s[:options]}")
141
+ end
142
+ end
143
+
144
+ def test_illegal_conditional_attributes
145
+ exception = assert_raises(TypeError) do
146
+ Class.new(ActiveModel::Serializer) do
147
+ attribute :x, if: nil
148
+ end
149
+ end
150
+
151
+ assert_match(/:if should be a Symbol, String or Proc/, exception.message)
152
+ end
153
+ end
154
+ end
155
+ end
@@ -0,0 +1,54 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'test_helper'
4
+
5
+ module ActiveModel
6
+ class Serializer
7
+ class AttributesTest < ActiveSupport::TestCase
8
+ def setup
9
+ @profile = Profile.new(name: 'Name 1', description: 'Description 1', comments: 'Comments 1')
10
+ @profile_serializer = ProfileSerializer.new(@profile)
11
+ @comment = Comment.new(id: 1, body: 'ZOMG!!', date: '2015')
12
+ @serializer_klass = Class.new(CommentSerializer)
13
+ @serializer_klass_with_new_attributes = Class.new(CommentSerializer) do
14
+ attributes :date, :likes
15
+ end
16
+ end
17
+
18
+ def test_attributes_definition
19
+ assert_equal([:name, :description],
20
+ @profile_serializer.class._attributes)
21
+ end
22
+
23
+ def test_attributes_inheritance_definition
24
+ assert_equal([:id, :body], @serializer_klass._attributes)
25
+ end
26
+
27
+ def test_attributes_inheritance
28
+ serializer = @serializer_klass.new(@comment)
29
+ assert_equal({ id: 1, body: 'ZOMG!!' },
30
+ serializer.attributes)
31
+ end
32
+
33
+ def test_attribute_inheritance_with_new_attribute_definition
34
+ assert_equal([:id, :body, :date, :likes], @serializer_klass_with_new_attributes._attributes)
35
+ assert_equal([:id, :body], CommentSerializer._attributes)
36
+ end
37
+
38
+ def test_attribute_inheritance_with_new_attribute
39
+ serializer = @serializer_klass_with_new_attributes.new(@comment)
40
+ assert_equal({ id: 1, body: 'ZOMG!!', date: '2015', likes: nil },
41
+ serializer.attributes)
42
+ end
43
+
44
+ def test_multiple_calls_with_the_same_attribute
45
+ serializer_class = Class.new(ActiveModel::Serializer) do
46
+ attributes :id, :title
47
+ attributes :id, :title, :title, :body
48
+ end
49
+
50
+ assert_equal([:id, :title, :body], serializer_class._attributes)
51
+ end
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,172 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Execute this test in isolation
4
+ require 'support/isolated_unit'
5
+
6
+ class CachingConfigurationTest < ActiveSupport::TestCase
7
+ include ActiveSupport::Testing::Isolation
8
+
9
+ setup do
10
+ require 'rails'
11
+ # AMS needs to be required before Rails.application is initialized for
12
+ # Railtie's to fire in Rails.application.initialize!
13
+ # (and make_basic_app initializes the app)
14
+ require 'active_model_serializers'
15
+ # Create serializers before Rails.application.initialize!
16
+ # To ensure we're testing that the cache settings depend on
17
+ # the Railtie firing, not on the ActionController being loaded.
18
+ create_serializers
19
+ end
20
+
21
+ def create_serializers
22
+ @cached_serializer = Class.new(ActiveModel::Serializer) do
23
+ cache skip_digest: true
24
+ attributes :id, :name, :title
25
+ end
26
+ @fragment_cached_serializer = Class.new(ActiveModel::Serializer) do
27
+ cache only: :id
28
+ attributes :id, :name, :title
29
+ end
30
+ @non_cached_serializer = Class.new(ActiveModel::Serializer) do
31
+ attributes :id, :name, :title
32
+ end
33
+ end
34
+
35
+ class PerformCachingTrue < CachingConfigurationTest
36
+ setup do
37
+ # Let's make that Rails app and initialize it!
38
+ make_basic_app do |app|
39
+ app.config.action_controller.perform_caching = true
40
+ app.config.action_controller.cache_store = ActiveSupport::Cache.lookup_store(:memory_store)
41
+ end
42
+ controller_cache_store # Force ActiveSupport.on_load(:action_controller) to run
43
+ end
44
+
45
+ test 'it sets perform_caching to true on AMS.config and serializers' do
46
+ assert Rails.configuration.action_controller.perform_caching
47
+ assert ActiveModelSerializers.config.perform_caching
48
+ assert ActiveModel::Serializer.perform_caching?
49
+ assert @cached_serializer.perform_caching?
50
+ assert @non_cached_serializer.perform_caching?
51
+ assert @fragment_cached_serializer.perform_caching?
52
+ end
53
+
54
+ test 'it sets the AMS.config.cache_store to the controller cache_store' do
55
+ assert_equal controller_cache_store, ActiveSupport::Cache::MemoryStore
56
+ assert_equal controller_cache_store, ActiveModelSerializers.config.cache_store.class
57
+ end
58
+
59
+ test 'it sets the cached serializer cache_store to the ActionController::Base.cache_store' do
60
+ assert_equal ActiveSupport::Cache::NullStore, @cached_serializer._cache.class
61
+ assert_equal controller_cache_store, @cached_serializer.cache_store.class
62
+ assert_equal ActiveSupport::Cache::MemoryStore, @cached_serializer._cache.class
63
+ end
64
+
65
+ test 'the cached serializer has cache_enabled?' do
66
+ assert @cached_serializer.cache_enabled?
67
+ end
68
+
69
+ test 'the cached serializer does not have fragment_cache_enabled?' do
70
+ refute @cached_serializer.fragment_cache_enabled?
71
+ end
72
+
73
+ test 'the non-cached serializer cache_store is nil' do
74
+ assert_nil @non_cached_serializer._cache
75
+ assert_nil @non_cached_serializer.cache_store
76
+ assert_nil @non_cached_serializer._cache
77
+ end
78
+
79
+ test 'the non-cached serializer does not have cache_enabled?' do
80
+ refute @non_cached_serializer.cache_enabled?
81
+ end
82
+
83
+ test 'the non-cached serializer does not have fragment_cache_enabled?' do
84
+ refute @non_cached_serializer.fragment_cache_enabled?
85
+ end
86
+
87
+ test 'it sets the fragment cached serializer cache_store to the ActionController::Base.cache_store' do
88
+ assert_equal ActiveSupport::Cache::NullStore, @fragment_cached_serializer._cache.class
89
+ assert_equal controller_cache_store, @fragment_cached_serializer.cache_store.class
90
+ assert_equal ActiveSupport::Cache::MemoryStore, @fragment_cached_serializer._cache.class
91
+ end
92
+
93
+ test 'the fragment cached serializer does not have cache_enabled?' do
94
+ refute @fragment_cached_serializer.cache_enabled?
95
+ end
96
+
97
+ test 'the fragment cached serializer has fragment_cache_enabled?' do
98
+ assert @fragment_cached_serializer.fragment_cache_enabled?
99
+ end
100
+ end
101
+
102
+ class PerformCachingFalse < CachingConfigurationTest
103
+ setup do
104
+ # Let's make that Rails app and initialize it!
105
+ make_basic_app do |app|
106
+ app.config.action_controller.perform_caching = false
107
+ app.config.action_controller.cache_store = ActiveSupport::Cache.lookup_store(:memory_store)
108
+ end
109
+ controller_cache_store # Force ActiveSupport.on_load(:action_controller) to run
110
+ end
111
+
112
+ test 'it sets perform_caching to false on AMS.config and serializers' do
113
+ refute Rails.configuration.action_controller.perform_caching
114
+ refute ActiveModelSerializers.config.perform_caching
115
+ refute ActiveModel::Serializer.perform_caching?
116
+ refute @cached_serializer.perform_caching?
117
+ refute @non_cached_serializer.perform_caching?
118
+ refute @fragment_cached_serializer.perform_caching?
119
+ end
120
+
121
+ test 'it sets the AMS.config.cache_store to the controller cache_store' do
122
+ assert_equal controller_cache_store, ActiveSupport::Cache::MemoryStore
123
+ assert_equal controller_cache_store, ActiveModelSerializers.config.cache_store.class
124
+ end
125
+
126
+ test 'it sets the cached serializer cache_store to the ActionController::Base.cache_store' do
127
+ assert_equal ActiveSupport::Cache::NullStore, @cached_serializer._cache.class
128
+ assert_equal controller_cache_store, @cached_serializer.cache_store.class
129
+ assert_equal ActiveSupport::Cache::MemoryStore, @cached_serializer._cache.class
130
+ end
131
+
132
+ test 'the cached serializer does not have cache_enabled?' do
133
+ refute @cached_serializer.cache_enabled?
134
+ end
135
+
136
+ test 'the cached serializer does not have fragment_cache_enabled?' do
137
+ refute @cached_serializer.fragment_cache_enabled?
138
+ end
139
+
140
+ test 'the non-cached serializer cache_store is nil' do
141
+ assert_nil @non_cached_serializer._cache
142
+ assert_nil @non_cached_serializer.cache_store
143
+ assert_nil @non_cached_serializer._cache
144
+ end
145
+
146
+ test 'the non-cached serializer does not have cache_enabled?' do
147
+ refute @non_cached_serializer.cache_enabled?
148
+ end
149
+
150
+ test 'the non-cached serializer does not have fragment_cache_enabled?' do
151
+ refute @non_cached_serializer.fragment_cache_enabled?
152
+ end
153
+
154
+ test 'it sets the fragment cached serializer cache_store to the ActionController::Base.cache_store' do
155
+ assert_equal ActiveSupport::Cache::NullStore, @fragment_cached_serializer._cache.class
156
+ assert_equal controller_cache_store, @fragment_cached_serializer.cache_store.class
157
+ assert_equal ActiveSupport::Cache::MemoryStore, @fragment_cached_serializer._cache.class
158
+ end
159
+
160
+ test 'the fragment cached serializer does not have cache_enabled?' do
161
+ refute @fragment_cached_serializer.cache_enabled?
162
+ end
163
+
164
+ test 'the fragment cached serializer does not have fragment_cache_enabled?' do
165
+ refute @fragment_cached_serializer.fragment_cache_enabled?
166
+ end
167
+ end
168
+
169
+ def controller_cache_store
170
+ ActionController::Base.cache_store.class
171
+ end
172
+ end
@@ -0,0 +1,34 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'test_helper'
4
+
5
+ module ActiveModel
6
+ class Serializer
7
+ class ConfigurationTest < ActiveSupport::TestCase
8
+ def test_collection_serializer
9
+ assert_equal ActiveModel::Serializer::CollectionSerializer, ActiveModelSerializers.config.collection_serializer
10
+ end
11
+
12
+ def test_array_serializer
13
+ assert_equal ActiveModel::Serializer::CollectionSerializer, ActiveModelSerializers.config.array_serializer
14
+ end
15
+
16
+ def test_setting_array_serializer_sets_collection_serializer
17
+ config = ActiveModelSerializers.config
18
+ old_config = config.dup
19
+ begin
20
+ assert_equal ActiveModel::Serializer::CollectionSerializer, config.collection_serializer
21
+ config.array_serializer = :foo
22
+ assert_equal config.array_serializer, :foo
23
+ assert_equal config.collection_serializer, :foo
24
+ ensure
25
+ ActiveModelSerializers.config.replace(old_config)
26
+ end
27
+ end
28
+
29
+ def test_default_adapter
30
+ assert_equal :attributes, ActiveModelSerializers.config.adapter
31
+ end
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'test_helper'
4
+
5
+ module ActiveModel
6
+ class Serializer
7
+ class FieldsetTest < ActiveSupport::TestCase
8
+ def test_fieldset_with_hash
9
+ fieldset = ActiveModel::Serializer::Fieldset.new('post' => %w(id title), 'comment' => ['body'])
10
+ expected = { post: [:id, :title], comment: [:body] }
11
+
12
+ assert_equal(expected, fieldset.fields)
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,204 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'test_helper'
4
+
5
+ module ActiveModel
6
+ class Serializer
7
+ class MetaTest < ActiveSupport::TestCase
8
+ def setup
9
+ @blog = Blog.new(id: 1,
10
+ name: 'AMS Hints',
11
+ writer: Author.new(id: 2, name: 'Steve'),
12
+ articles: [Post.new(id: 3, title: 'AMS')])
13
+ end
14
+
15
+ def test_meta_is_present_with_root
16
+ actual = ActiveModelSerializers::SerializableResource.new(
17
+ @blog,
18
+ adapter: :json,
19
+ serializer: AlternateBlogSerializer,
20
+ meta: { total: 10 }
21
+ ).as_json
22
+ expected = {
23
+ blog: {
24
+ id: 1,
25
+ title: 'AMS Hints'
26
+ },
27
+ 'meta' => {
28
+ total: 10
29
+ }
30
+ }
31
+ assert_equal(expected, actual)
32
+ end
33
+
34
+ def test_meta_is_not_included_when_blank
35
+ actual = ActiveModelSerializers::SerializableResource.new(
36
+ @blog,
37
+ adapter: :json,
38
+ serializer: AlternateBlogSerializer,
39
+ meta: {}
40
+ ).as_json
41
+ expected = {
42
+ blog: {
43
+ id: 1,
44
+ title: 'AMS Hints'
45
+ }
46
+ }
47
+ assert_equal(expected, actual)
48
+ end
49
+
50
+ def test_meta_is_not_included_when_empty_string
51
+ actual = ActiveModelSerializers::SerializableResource.new(
52
+ @blog,
53
+ adapter: :json,
54
+ serializer: AlternateBlogSerializer,
55
+ meta: ''
56
+ ).as_json
57
+ expected = {
58
+ blog: {
59
+ id: 1,
60
+ title: 'AMS Hints'
61
+ }
62
+ }
63
+ assert_equal(expected, actual)
64
+ end
65
+
66
+ def test_meta_is_not_included_when_root_is_missing
67
+ actual = ActiveModelSerializers::SerializableResource.new(
68
+ @blog,
69
+ adapter: :attributes,
70
+ serializer: AlternateBlogSerializer,
71
+ meta: { total: 10 }
72
+ ).as_json
73
+ expected = {
74
+ id: 1,
75
+ title: 'AMS Hints'
76
+ }
77
+ assert_equal(expected, actual)
78
+ end
79
+
80
+ def test_meta_key_is_used
81
+ actual = ActiveModelSerializers::SerializableResource.new(
82
+ @blog,
83
+ adapter: :json,
84
+ serializer: AlternateBlogSerializer,
85
+ meta: { total: 10 },
86
+ meta_key: 'haha_meta'
87
+ ).as_json
88
+ expected = {
89
+ blog: {
90
+ id: 1,
91
+ title: 'AMS Hints'
92
+ },
93
+ 'haha_meta' => {
94
+ total: 10
95
+ }
96
+ }
97
+ assert_equal(expected, actual)
98
+ end
99
+
100
+ def test_meta_key_is_not_used_with_json_api
101
+ actual = ActiveModelSerializers::SerializableResource.new(
102
+ @blog,
103
+ adapter: :json_api,
104
+ serializer: AlternateBlogSerializer,
105
+ meta: { total: 10 },
106
+ meta_key: 'haha_meta'
107
+ ).as_json
108
+ expected = {
109
+ data: {
110
+ id: '1',
111
+ type: 'blogs',
112
+ attributes: { title: 'AMS Hints' }
113
+ },
114
+ meta: { total: 10 }
115
+ }
116
+ assert_equal(expected, actual)
117
+ end
118
+
119
+ def test_meta_key_is_not_present_when_empty_hash_with_json_api
120
+ actual = ActiveModelSerializers::SerializableResource.new(
121
+ @blog,
122
+ adapter: :json_api,
123
+ serializer: AlternateBlogSerializer,
124
+ meta: {}
125
+ ).as_json
126
+ expected = {
127
+ data: {
128
+ id: '1',
129
+ type: 'blogs',
130
+ attributes: { title: 'AMS Hints' }
131
+ }
132
+ }
133
+ assert_equal(expected, actual)
134
+ end
135
+
136
+ def test_meta_key_is_not_present_when_empty_string_with_json_api
137
+ actual = ActiveModelSerializers::SerializableResource.new(
138
+ @blog,
139
+ adapter: :json_api,
140
+ serializer: AlternateBlogSerializer,
141
+ meta: ''
142
+ ).as_json
143
+ expected = {
144
+ data: {
145
+ id: '1',
146
+ type: 'blogs',
147
+ attributes: { title: 'AMS Hints' }
148
+ }
149
+ }
150
+ assert_equal(expected, actual)
151
+ end
152
+
153
+ def test_meta_is_not_present_on_arrays_without_root
154
+ actual = ActiveModelSerializers::SerializableResource.new(
155
+ [@blog],
156
+ adapter: :attributes,
157
+ meta: { total: 10 }
158
+ ).as_json
159
+ expected = [{
160
+ id: 1,
161
+ name: 'AMS Hints',
162
+ writer: {
163
+ id: 2,
164
+ name: 'Steve'
165
+ },
166
+ articles: [{
167
+ id: 3,
168
+ title: 'AMS',
169
+ body: nil
170
+ }]
171
+ }]
172
+ assert_equal(expected, actual)
173
+ end
174
+
175
+ def test_meta_is_present_on_arrays_with_root
176
+ actual = ActiveModelSerializers::SerializableResource.new(
177
+ [@blog],
178
+ adapter: :json,
179
+ meta: { total: 10 },
180
+ meta_key: 'haha_meta'
181
+ ).as_json
182
+ expected = {
183
+ blogs: [{
184
+ id: 1,
185
+ name: 'AMS Hints',
186
+ writer: {
187
+ id: 2,
188
+ name: 'Steve'
189
+ },
190
+ articles: [{
191
+ id: 3,
192
+ title: 'AMS',
193
+ body: nil
194
+ }]
195
+ }],
196
+ 'haha_meta' => {
197
+ total: 10
198
+ }
199
+ }
200
+ assert_equal(expected, actual)
201
+ end
202
+ end
203
+ end
204
+ end