active_model_serializers 0.10.0 → 0.10.12

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 +233 -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,94 +0,0 @@
1
- require 'test_helper'
2
-
3
- module ActiveModel
4
- class Serializer
5
- class IncludeTree
6
- class FromStringTest < ActiveSupport::TestCase
7
- def test_single_string
8
- input = 'author'
9
- actual = ActiveModel::Serializer::IncludeTree.from_string(input)
10
- assert(actual.key?(:author))
11
- end
12
-
13
- def test_multiple_strings
14
- input = 'author,comments'
15
- actual = ActiveModel::Serializer::IncludeTree.from_string(input)
16
- assert(actual.key?(:author))
17
- assert(actual.key?(:comments))
18
- end
19
-
20
- def test_multiple_strings_with_space
21
- input = 'author, comments'
22
- actual = ActiveModel::Serializer::IncludeTree.from_string(input)
23
- assert(actual.key?(:author))
24
- assert(actual.key?(:comments))
25
- end
26
-
27
- def test_nested_string
28
- input = 'posts.author'
29
- actual = ActiveModel::Serializer::IncludeTree.from_string(input)
30
- assert(actual.key?(:posts))
31
- assert(actual[:posts].key?(:author))
32
- end
33
-
34
- def test_multiple_nested_string
35
- input = 'posts.author,posts.comments.author,comments'
36
- actual = ActiveModel::Serializer::IncludeTree.from_string(input)
37
- assert(actual.key?(:posts))
38
- assert(actual[:posts].key?(:author))
39
- assert(actual[:posts].key?(:comments))
40
- assert(actual[:posts][:comments].key?(:author))
41
- assert(actual.key?(:comments))
42
- end
43
-
44
- def test_toplevel_star_string
45
- input = '*'
46
- actual = ActiveModel::Serializer::IncludeTree.from_string(input)
47
- assert(actual.key?(:comments))
48
- end
49
-
50
- def test_nested_star_string
51
- input = 'posts.*'
52
- actual = ActiveModel::Serializer::IncludeTree.from_string(input)
53
- assert(actual.key?(:posts))
54
- assert(actual[:posts].key?(:comments))
55
- end
56
-
57
- def test_nested_star_middle_string
58
- input = 'posts.*.author'
59
- actual = ActiveModel::Serializer::IncludeTree.from_string(input)
60
- assert(actual.key?(:posts))
61
- assert(actual[:posts].key?(:comments))
62
- assert(actual[:posts][:comments].key?(:author))
63
- refute(actual[:posts][:comments].key?(:unspecified))
64
- end
65
-
66
- def test_nested_star_lower_precedence_string
67
- input = 'posts.comments.author,posts.*'
68
- actual = ActiveModel::Serializer::IncludeTree.from_string(input)
69
- assert(actual.key?(:posts))
70
- assert(actual[:posts].key?(:comments))
71
- assert(actual[:posts][:comments].key?(:author))
72
- end
73
-
74
- def test_toplevel_double_star_string
75
- input = '**'
76
- actual = ActiveModel::Serializer::IncludeTree.from_string(input)
77
- assert(actual.key?(:posts))
78
- assert(actual[:posts].key?(:comments))
79
- assert(actual[:posts][:comments].key?(:posts))
80
- end
81
-
82
- def test_nested_double_star_string
83
- input = 'comments, posts.**'
84
- actual = ActiveModel::Serializer::IncludeTree.from_string(input)
85
- assert(actual.key?(:comments))
86
- refute(actual[:comments].key?(:author))
87
- assert(actual.key?(:posts))
88
- assert(actual[:posts].key?(:comments))
89
- assert(actual[:posts][:comments].key?(:posts))
90
- end
91
- end
92
- end
93
- end
94
- end
@@ -1,64 +0,0 @@
1
- require 'test_helper'
2
-
3
- module ActiveModel
4
- class Serializer
5
- class IncludeTree
6
- module Parsing
7
- class IncludeArgsToHashTest < MiniTest::Test
8
- def test_include_args_to_hash_from_symbol
9
- expected = { author: {} }
10
- input = :author
11
- actual = Parsing.include_args_to_hash(input)
12
-
13
- assert_equal(expected, actual)
14
- end
15
-
16
- def test_include_args_to_hash_from_array
17
- expected = { author: {}, comments: {} }
18
- input = [:author, :comments]
19
- actual = Parsing.include_args_to_hash(input)
20
-
21
- assert_equal(expected, actual)
22
- end
23
-
24
- def test_include_args_to_hash_from_nested_array
25
- expected = { author: {}, comments: { author: {} } }
26
- input = [:author, comments: [:author]]
27
- actual = Parsing.include_args_to_hash(input)
28
-
29
- assert_equal(expected, actual)
30
- end
31
-
32
- def test_include_args_to_hash_from_array_of_hashes
33
- expected = {
34
- author: {},
35
- blogs: { posts: { contributors: {} } },
36
- comments: { author: { blogs: { posts: {} } } }
37
- }
38
- input = [
39
- :author,
40
- blogs: [posts: :contributors],
41
- comments: { author: { blogs: :posts } }
42
- ]
43
- actual = Parsing.include_args_to_hash(input)
44
-
45
- assert_equal(expected, actual)
46
- end
47
-
48
- def test_array_of_string
49
- expected = {
50
- comments: { author: {}, attachment: {} }
51
- }
52
- input = [
53
- 'comments.author',
54
- 'comments.attachment'
55
- ]
56
- actual = Parsing.include_args_to_hash(input)
57
-
58
- assert_equal(expected, actual)
59
- end
60
- end
61
- end
62
- end
63
- end
64
- end
data/test/lint_test.rb DELETED
@@ -1,49 +0,0 @@
1
- require 'test_helper'
2
-
3
- module ActiveModel
4
- class Serializer
5
- class LintTest < ActiveSupport::TestCase
6
- include ActiveModel::Serializer::Lint::Tests
7
-
8
- class CompliantResource
9
- def serializable_hash(options = nil)
10
- end
11
-
12
- def read_attribute_for_serialization(name)
13
- end
14
-
15
- def as_json(options = nil)
16
- end
17
-
18
- def to_json(options = nil)
19
- end
20
-
21
- def cache_key
22
- end
23
-
24
- def id
25
- end
26
-
27
- def updated_at
28
- end
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
-
39
- def self.model_name
40
- @_model_name ||= ActiveModel::Name.new(self)
41
- end
42
- end
43
-
44
- def setup
45
- @resource = CompliantResource.new
46
- end
47
- end
48
- end
49
- end
data/test/logger_test.rb DELETED
@@ -1,18 +0,0 @@
1
- require 'test_helper'
2
-
3
- class ActiveModelSerializers::LoggerTest < ActiveSupport::TestCase
4
- def test_logger_is_set_to_action_controller_logger_when_initializer_runs
5
- assert_equal $action_controller_logger, ActionController::Base.logger # rubocop:disable Style/GlobalVars
6
- end
7
-
8
- def test_logger_can_be_set
9
- original_logger = ActiveModelSerializers.logger
10
- logger = Logger.new(STDOUT)
11
-
12
- ActiveModelSerializers.logger = logger
13
-
14
- assert_equal ActiveModelSerializers.logger, logger
15
- ensure
16
- ActiveModelSerializers.logger = original_logger
17
- end
18
- end
data/test/poro_test.rb DELETED
@@ -1,9 +0,0 @@
1
- require 'test_helper'
2
-
3
- class PoroTest < ActiveSupport::TestCase
4
- include ActiveModel::Serializer::Lint::Tests
5
-
6
- def setup
7
- @resource = Model.new
8
- end
9
- end
@@ -1,83 +0,0 @@
1
- require 'test_helper'
2
-
3
- module ActiveModelSerializers
4
- class SerializableResourceTest < ActiveSupport::TestCase
5
- def setup
6
- @resource = Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' })
7
- @serializer = ProfileSerializer.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
17
- end
18
-
19
- def test_serializable_resource_delegates_serializable_hash_to_the_adapter
20
- options = nil
21
- assert_equal @adapter.serializable_hash(options), @serializable_resource.serializable_hash(options)
22
- end
23
-
24
- def test_serializable_resource_delegates_to_json_to_the_adapter
25
- options = nil
26
- assert_equal @adapter.to_json(options), @serializable_resource.to_json(options)
27
- end
28
-
29
- def test_serializable_resource_delegates_as_json_to_the_adapter
30
- options = nil
31
- assert_equal @adapter.as_json(options), @serializable_resource.as_json(options)
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
82
- end
83
- end
@@ -1,36 +0,0 @@
1
- require 'test_helper'
2
-
3
- module ActiveModel
4
- class Serializer
5
- class AssociationMacrosTest < ActiveSupport::TestCase
6
- AuthorSummarySerializer = Class.new
7
- class AssociationsTestSerializer < Serializer
8
- belongs_to :author, serializer: AuthorSummarySerializer
9
- has_many :comments
10
- has_one :category
11
- end
12
-
13
- def before_setup
14
- @reflections = AssociationsTestSerializer._reflections
15
- end
16
-
17
- def test_has_one_defines_reflection
18
- has_one_reflection = HasOneReflection.new(:category, {})
19
-
20
- assert_includes(@reflections, has_one_reflection)
21
- end
22
-
23
- def test_has_many_defines_reflection
24
- has_many_reflection = HasManyReflection.new(:comments, {})
25
-
26
- assert_includes(@reflections, has_many_reflection)
27
- end
28
-
29
- def test_belongs_to_defines_reflection
30
- belongs_to_reflection = BelongsToReflection.new(:author, serializer: AuthorSummarySerializer)
31
-
32
- assert_includes(@reflections, belongs_to_reflection)
33
- end
34
- end
35
- end
36
- end
@@ -1,295 +0,0 @@
1
- require 'test_helper'
2
-
3
- module ActiveModel
4
- class Serializer
5
- class AssociationsTest < ActiveSupport::TestCase
6
- def setup
7
- @author = Author.new(name: 'Steve K.')
8
- @author.bio = nil
9
- @author.roles = []
10
- @blog = Blog.new({ name: 'AMS Blog' })
11
- @post = Post.new({ title: 'New Post', body: 'Body' })
12
- @tag = Tag.new({ name: '#hashtagged' })
13
- @comment = Comment.new({ id: 1, body: 'ZOMG A COMMENT' })
14
- @post.comments = [@comment]
15
- @post.tags = [@tag]
16
- @post.blog = @blog
17
- @comment.post = @post
18
- @comment.author = nil
19
- @post.author = @author
20
- @author.posts = [@post]
21
-
22
- @post_serializer = PostSerializer.new(@post, { custom_options: true })
23
- @author_serializer = AuthorSerializer.new(@author)
24
- @comment_serializer = CommentSerializer.new(@comment)
25
- end
26
-
27
- def test_has_many_and_has_one
28
- @author_serializer.associations.each do |association|
29
- key = association.key
30
- serializer = association.serializer
31
- options = association.options
32
-
33
- case key
34
- when :posts
35
- assert_equal({ include_data: true }, options)
36
- assert_kind_of(ActiveModelSerializers.config.collection_serializer, serializer)
37
- when :bio
38
- assert_equal({ include_data: true }, options)
39
- assert_nil serializer
40
- when :roles
41
- assert_equal({ include_data: true }, options)
42
- assert_kind_of(ActiveModelSerializers.config.collection_serializer, serializer)
43
- else
44
- flunk "Unknown association: #{key}"
45
- end
46
- end
47
- end
48
-
49
- def test_has_many_with_no_serializer
50
- PostWithTagsSerializer.new(@post).associations.each do |association|
51
- key = association.key
52
- serializer = association.serializer
53
- options = association.options
54
-
55
- assert_equal :tags, key
56
- assert_nil serializer
57
- assert_equal [{ name: '#hashtagged' }].to_json, options[:virtual_value].to_json
58
- end
59
- end
60
-
61
- def test_serializer_options_are_passed_into_associations_serializers
62
- association = @post_serializer
63
- .associations
64
- .detect { |assoc| assoc.key == :comments }
65
-
66
- assert association.serializer.first.custom_options[:custom_options]
67
- end
68
-
69
- def test_belongs_to
70
- @comment_serializer.associations.each do |association|
71
- key = association.key
72
- serializer = association.serializer
73
-
74
- case key
75
- when :post
76
- assert_kind_of(PostSerializer, serializer)
77
- when :author
78
- assert_nil serializer
79
- else
80
- flunk "Unknown association: #{key}"
81
- end
82
-
83
- assert_equal({ include_data: true }, association.options)
84
- end
85
- end
86
-
87
- def test_belongs_to_with_custom_method
88
- assert(
89
- @post_serializer.associations.any? do |association|
90
- association.key == :blog
91
- end
92
- )
93
- end
94
-
95
- def test_associations_inheritance
96
- inherited_klass = Class.new(PostSerializer)
97
-
98
- assert_equal(PostSerializer._reflections, inherited_klass._reflections)
99
- end
100
-
101
- def test_associations_inheritance_with_new_association
102
- inherited_klass = Class.new(PostSerializer) do
103
- has_many :top_comments, serializer: CommentSerializer
104
- end
105
-
106
- assert(
107
- PostSerializer._reflections.all? do |reflection|
108
- inherited_klass._reflections.include?(reflection)
109
- end
110
- )
111
-
112
- assert(
113
- inherited_klass._reflections.any? do |reflection|
114
- reflection.name == :top_comments
115
- end
116
- )
117
- end
118
-
119
- def test_associations_custom_keys
120
- serializer = PostWithCustomKeysSerializer.new(@post)
121
-
122
- expected_association_keys = serializer.associations.map(&:key)
123
-
124
- assert expected_association_keys.include? :reviews
125
- assert expected_association_keys.include? :writer
126
- assert expected_association_keys.include? :site
127
- end
128
-
129
- class InlineAssociationTestPostSerializer < ActiveModel::Serializer
130
- has_many :comments
131
- has_many :comments, key: :last_comments do
132
- object.comments.last(1)
133
- end
134
- end
135
-
136
- def test_virtual_attribute_block
137
- comment1 = ::ARModels::Comment.create!(contents: 'first comment')
138
- comment2 = ::ARModels::Comment.create!(contents: 'last comment')
139
- post = ::ARModels::Post.create!(
140
- title: 'inline association test',
141
- body: 'etc',
142
- comments: [comment1, comment2]
143
- )
144
- actual = serializable(post, adapter: :attributes, serializer: InlineAssociationTestPostSerializer).as_json
145
- expected = {
146
- :comments => [
147
- { :id => 1, :contents => 'first comment' },
148
- { :id => 2, :contents => 'last comment' }
149
- ],
150
- :last_comments => [
151
- { :id => 2, :contents => 'last comment' }
152
- ]
153
- }
154
-
155
- assert_equal expected, actual
156
- ensure
157
- ::ARModels::Post.delete_all
158
- ::ARModels::Comment.delete_all
159
- end
160
-
161
- class NamespacedResourcesTest < ActiveSupport::TestCase
162
- class ResourceNamespace
163
- Post = Class.new(::Model)
164
- Comment = Class.new(::Model)
165
- Author = Class.new(::Model)
166
- Description = Class.new(::Model)
167
- class PostSerializer < ActiveModel::Serializer
168
- has_many :comments
169
- belongs_to :author
170
- has_one :description
171
- end
172
- CommentSerializer = Class.new(ActiveModel::Serializer)
173
- AuthorSerializer = Class.new(ActiveModel::Serializer)
174
- DescriptionSerializer = Class.new(ActiveModel::Serializer)
175
- end
176
-
177
- def setup
178
- @comment = ResourceNamespace::Comment.new
179
- @author = ResourceNamespace::Author.new
180
- @description = ResourceNamespace::Description.new
181
- @post = ResourceNamespace::Post.new(comments: [@comment],
182
- author: @author,
183
- description: @description)
184
- @post_serializer = ResourceNamespace::PostSerializer.new(@post)
185
- end
186
-
187
- def test_associations_namespaced_resources
188
- @post_serializer.associations.each do |association|
189
- case association.key
190
- when :comments
191
- assert_instance_of(ResourceNamespace::CommentSerializer, association.serializer.first)
192
- when :author
193
- assert_instance_of(ResourceNamespace::AuthorSerializer, association.serializer)
194
- when :description
195
- assert_instance_of(ResourceNamespace::DescriptionSerializer, association.serializer)
196
- else
197
- flunk "Unknown association: #{key}"
198
- end
199
- end
200
- end
201
- end
202
-
203
- class NestedSerializersTest < ActiveSupport::TestCase
204
- Post = Class.new(::Model)
205
- Comment = Class.new(::Model)
206
- Author = Class.new(::Model)
207
- Description = Class.new(::Model)
208
- class PostSerializer < ActiveModel::Serializer
209
- has_many :comments
210
- CommentSerializer = Class.new(ActiveModel::Serializer)
211
- belongs_to :author
212
- AuthorSerializer = Class.new(ActiveModel::Serializer)
213
- has_one :description
214
- DescriptionSerializer = Class.new(ActiveModel::Serializer)
215
- end
216
-
217
- def setup
218
- @comment = Comment.new
219
- @author = Author.new
220
- @description = Description.new
221
- @post = Post.new(comments: [@comment],
222
- author: @author,
223
- description: @description)
224
- @post_serializer = PostSerializer.new(@post)
225
- end
226
-
227
- def test_associations_namespaced_resources
228
- @post_serializer.associations.each do |association|
229
- case association.key
230
- when :comments
231
- assert_instance_of(PostSerializer::CommentSerializer, association.serializer.first)
232
- when :author
233
- assert_instance_of(PostSerializer::AuthorSerializer, association.serializer)
234
- when :description
235
- assert_instance_of(PostSerializer::DescriptionSerializer, association.serializer)
236
- else
237
- flunk "Unknown association: #{key}"
238
- end
239
- end
240
- end
241
-
242
- # rubocop:disable Metrics/AbcSize
243
- def test_conditional_associations
244
- model = ::Model.new(true: true, false: false)
245
-
246
- scenarios = [
247
- { options: { if: :true }, included: true },
248
- { options: { if: :false }, included: false },
249
- { options: { unless: :false }, included: true },
250
- { options: { unless: :true }, included: false },
251
- { options: { if: 'object.true' }, included: true },
252
- { options: { if: 'object.false' }, included: false },
253
- { options: { unless: 'object.false' }, included: true },
254
- { options: { unless: 'object.true' }, included: false },
255
- { options: { if: -> { object.true } }, included: true },
256
- { options: { if: -> { object.false } }, included: false },
257
- { options: { unless: -> { object.false } }, included: true },
258
- { options: { unless: -> { object.true } }, included: false },
259
- { options: { if: -> (s) { s.object.true } }, included: true },
260
- { options: { if: -> (s) { s.object.false } }, included: false },
261
- { options: { unless: -> (s) { s.object.false } }, included: true },
262
- { options: { unless: -> (s) { s.object.true } }, included: false }
263
- ]
264
-
265
- scenarios.each do |s|
266
- serializer = Class.new(ActiveModel::Serializer) do
267
- belongs_to :association, s[:options]
268
-
269
- def true
270
- true
271
- end
272
-
273
- def false
274
- false
275
- end
276
- end
277
-
278
- hash = serializable(model, serializer: serializer).serializable_hash
279
- assert_equal(s[:included], hash.key?(:association), "Error with #{s[:options]}")
280
- end
281
- end
282
-
283
- def test_illegal_conditional_associations
284
- exception = assert_raises(TypeError) do
285
- Class.new(ActiveModel::Serializer) do
286
- belongs_to :x, if: nil
287
- end
288
- end
289
-
290
- assert_match(/:if should be a Symbol, String or Proc/, exception.message)
291
- end
292
- end
293
- end
294
- end
295
- end