active_model_serializers 0.10.0 → 0.10.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (146) hide show
  1. checksums.yaml +4 -4
  2. data/.rubocop.yml +2 -4
  3. data/.travis.yml +9 -1
  4. data/CHANGELOG.md +81 -2
  5. data/CODE_OF_CONDUCT.md +74 -0
  6. data/Gemfile +5 -2
  7. data/README.md +24 -24
  8. data/Rakefile +3 -3
  9. data/active_model_serializers.gemspec +20 -24
  10. data/docs/ARCHITECTURE.md +6 -7
  11. data/docs/README.md +2 -0
  12. data/docs/general/adapters.md +4 -2
  13. data/docs/general/caching.md +7 -1
  14. data/docs/general/configuration_options.md +70 -1
  15. data/docs/general/deserialization.md +1 -1
  16. data/docs/general/fields.md +31 -0
  17. data/docs/general/rendering.md +42 -3
  18. data/docs/general/serializers.md +97 -8
  19. data/docs/howto/add_pagination_links.md +4 -5
  20. data/docs/howto/add_relationship_links.md +137 -0
  21. data/docs/howto/add_root_key.md +4 -0
  22. data/docs/howto/grape_integration.md +42 -0
  23. data/docs/howto/outside_controller_use.md +9 -2
  24. data/docs/howto/passing_arbitrary_options.md +2 -2
  25. data/docs/howto/test.md +2 -0
  26. data/docs/howto/upgrade_from_0_8_to_0_10.md +265 -0
  27. data/docs/integrations/ember-and-json-api.md +64 -32
  28. data/docs/jsonapi/schema.md +1 -1
  29. data/lib/action_controller/serialization.rb +13 -3
  30. data/lib/active_model/serializer/adapter/base.rb +2 -0
  31. data/lib/active_model/serializer/array_serializer.rb +8 -5
  32. data/lib/active_model/serializer/association.rb +19 -4
  33. data/lib/active_model/serializer/belongs_to_reflection.rb +0 -3
  34. data/lib/active_model/serializer/collection_serializer.rb +35 -12
  35. data/lib/active_model/serializer/{associations.rb → concerns/associations.rb} +13 -11
  36. data/lib/active_model/serializer/{attributes.rb → concerns/attributes.rb} +0 -0
  37. data/lib/active_model/serializer/{caching.rb → concerns/caching.rb} +72 -113
  38. data/lib/active_model/serializer/{configuration.rb → concerns/configuration.rb} +25 -1
  39. data/lib/active_model/serializer/{links.rb → concerns/links.rb} +0 -0
  40. data/lib/active_model/serializer/{meta.rb → concerns/meta.rb} +0 -0
  41. data/lib/active_model/serializer/{type.rb → concerns/type.rb} +0 -0
  42. data/lib/active_model/serializer/error_serializer.rb +11 -7
  43. data/lib/active_model/serializer/errors_serializer.rb +25 -20
  44. data/lib/active_model/serializer/has_many_reflection.rb +0 -3
  45. data/lib/active_model/serializer/has_one_reflection.rb +0 -3
  46. data/lib/active_model/serializer/lint.rb +134 -130
  47. data/lib/active_model/serializer/reflection.rb +37 -21
  48. data/lib/active_model/serializer/version.rb +1 -1
  49. data/lib/active_model/serializer.rb +76 -37
  50. data/lib/active_model_serializers/adapter/attributes.rb +3 -66
  51. data/lib/active_model_serializers/adapter/base.rb +38 -38
  52. data/lib/active_model_serializers/adapter/json_api/link.rb +1 -1
  53. data/lib/active_model_serializers/adapter/json_api/pagination_links.rb +8 -1
  54. data/lib/active_model_serializers/adapter/json_api/relationship.rb +30 -19
  55. data/lib/active_model_serializers/adapter/json_api/resource_identifier.rb +23 -9
  56. data/lib/active_model_serializers/adapter/json_api.rb +44 -43
  57. data/lib/active_model_serializers/adapter.rb +6 -0
  58. data/lib/active_model_serializers/deprecate.rb +1 -2
  59. data/lib/active_model_serializers/deserialization.rb +2 -0
  60. data/lib/active_model_serializers/key_transform.rb +4 -0
  61. data/lib/active_model_serializers/lookup_chain.rb +80 -0
  62. data/lib/active_model_serializers/model.rb +4 -2
  63. data/lib/active_model_serializers/railtie.rb +3 -1
  64. data/lib/active_model_serializers/register_jsonapi_renderer.rb +44 -31
  65. data/lib/active_model_serializers/serializable_resource.rb +6 -5
  66. data/lib/active_model_serializers/serialization_context.rb +10 -3
  67. data/lib/active_model_serializers.rb +7 -0
  68. data/lib/generators/rails/serializer_generator.rb +4 -4
  69. data/lib/grape/active_model_serializers.rb +7 -5
  70. data/lib/grape/formatters/active_model_serializers.rb +19 -2
  71. data/lib/grape/helpers/active_model_serializers.rb +1 -0
  72. data/test/action_controller/adapter_selector_test.rb +4 -4
  73. data/test/action_controller/explicit_serializer_test.rb +5 -4
  74. data/test/action_controller/json/include_test.rb +106 -27
  75. data/test/action_controller/json_api/errors_test.rb +6 -7
  76. data/test/action_controller/json_api/fields_test.rb +57 -0
  77. data/test/action_controller/json_api/linked_test.rb +29 -24
  78. data/test/action_controller/json_api/pagination_test.rb +19 -19
  79. data/test/action_controller/json_api/transform_test.rb +3 -3
  80. data/test/action_controller/lookup_proc_test.rb +49 -0
  81. data/test/action_controller/namespace_lookup_test.rb +226 -0
  82. data/test/action_controller/serialization_test.rb +10 -7
  83. data/test/active_model_serializers/json_pointer_test.rb +15 -13
  84. data/test/active_model_serializers/key_transform_test.rb +286 -252
  85. data/test/active_model_serializers/model_test.rb +17 -4
  86. data/test/active_model_serializers/register_jsonapi_renderer_test_isolated.rb +143 -0
  87. data/test/active_model_serializers/serialization_context_test_isolated.rb +23 -10
  88. data/test/adapter/attributes_test.rb +43 -0
  89. data/test/adapter/json/collection_test.rb +14 -0
  90. data/test/adapter/json/transform_test.rb +15 -15
  91. data/test/adapter/json_api/collection_test.rb +4 -3
  92. data/test/adapter/json_api/errors_test.rb +17 -19
  93. data/test/adapter/json_api/fields_test.rb +4 -3
  94. data/test/adapter/json_api/has_many_test.rb +39 -18
  95. data/test/adapter/json_api/include_data_if_sideloaded_test.rb +166 -0
  96. data/test/adapter/json_api/json_api_test.rb +5 -7
  97. data/test/adapter/json_api/linked_test.rb +33 -12
  98. data/test/adapter/json_api/links_test.rb +4 -2
  99. data/test/adapter/json_api/pagination_links_test.rb +35 -8
  100. data/test/adapter/json_api/relationship_test.rb +309 -73
  101. data/test/adapter/json_api/resource_identifier_test.rb +27 -2
  102. data/test/adapter/json_api/resource_meta_test.rb +3 -3
  103. data/test/adapter/json_api/transform_test.rb +255 -253
  104. data/test/adapter/json_api/type_test.rb +1 -1
  105. data/test/adapter/json_test.rb +8 -7
  106. data/test/adapter/null_test.rb +1 -2
  107. data/test/adapter/polymorphic_test.rb +5 -5
  108. data/test/adapter_test.rb +1 -1
  109. data/test/benchmark/app.rb +1 -1
  110. data/test/benchmark/benchmarking_support.rb +1 -1
  111. data/test/benchmark/bm_active_record.rb +81 -0
  112. data/test/benchmark/bm_adapter.rb +38 -0
  113. data/test/benchmark/bm_caching.rb +16 -16
  114. data/test/benchmark/bm_lookup_chain.rb +83 -0
  115. data/test/benchmark/bm_transform.rb +16 -5
  116. data/test/benchmark/controllers.rb +16 -17
  117. data/test/benchmark/fixtures.rb +72 -72
  118. data/test/cache_test.rb +143 -49
  119. data/test/collection_serializer_test.rb +3 -3
  120. data/test/fixtures/poro.rb +52 -48
  121. data/test/generators/serializer_generator_test.rb +22 -5
  122. data/test/grape_test.rb +152 -56
  123. data/test/lint_test.rb +1 -1
  124. data/test/logger_test.rb +13 -11
  125. data/test/serializable_resource_test.rb +18 -22
  126. data/test/serializers/association_macros_test.rb +3 -2
  127. data/test/serializers/associations_test.rb +107 -32
  128. data/test/serializers/attribute_test.rb +2 -2
  129. data/test/serializers/attributes_test.rb +1 -1
  130. data/test/serializers/fieldset_test.rb +1 -1
  131. data/test/serializers/meta_test.rb +12 -6
  132. data/test/serializers/root_test.rb +1 -1
  133. data/test/serializers/serializer_for_test.rb +6 -4
  134. data/test/serializers/serializer_for_with_namespace_test.rb +87 -0
  135. data/test/support/isolated_unit.rb +5 -2
  136. data/test/support/rails5_shims.rb +8 -2
  137. data/test/support/rails_app.rb +0 -9
  138. data/test/support/serialization_testing.rb +23 -5
  139. data/test/test_helper.rb +1 -0
  140. metadata +85 -34
  141. data/.rubocop_todo.yml +0 -167
  142. data/lib/active_model/serializer/include_tree.rb +0 -111
  143. data/test/adapter/json_api/relationships_test.rb +0 -199
  144. data/test/include_tree/from_include_args_test.rb +0 -26
  145. data/test/include_tree/from_string_test.rb +0 -94
  146. data/test/include_tree/include_args_to_hash_test.rb +0 -64
@@ -0,0 +1,143 @@
1
+ require 'support/isolated_unit'
2
+ require 'minitest/mock'
3
+ require 'action_dispatch'
4
+ require 'action_controller'
5
+
6
+ class JsonApiRendererTest < ActionDispatch::IntegrationTest
7
+ include ActiveSupport::Testing::Isolation
8
+
9
+ class TestController < ActionController::Base
10
+ class << self
11
+ attr_accessor :last_request_parameters
12
+ end
13
+
14
+ def render_with_jsonapi_renderer
15
+ author = Author.new(params[:data][:attributes])
16
+ render jsonapi: author
17
+ end
18
+
19
+ def parse
20
+ self.class.last_request_parameters = request.request_parameters
21
+ head :ok
22
+ end
23
+ end
24
+
25
+ def teardown
26
+ TestController.last_request_parameters = nil
27
+ end
28
+
29
+ def assert_parses(expected, actual, headers = {})
30
+ post '/parse', params: actual, headers: headers
31
+ assert_response :ok
32
+ assert_equal(expected, TestController.last_request_parameters)
33
+ end
34
+
35
+ class WithoutRenderer < JsonApiRendererTest
36
+ setup do
37
+ require 'rails'
38
+ require 'active_record'
39
+ require 'support/rails5_shims'
40
+ require 'active_model_serializers'
41
+ require 'fixtures/poro'
42
+
43
+ make_basic_app
44
+
45
+ Rails.application.routes.draw do
46
+ ActiveSupport::Deprecation.silence do
47
+ match ':action', to: TestController, via: [:get, :post]
48
+ end
49
+ end
50
+ end
51
+
52
+ def test_jsonapi_parser_not_registered
53
+ parsers = if Rails::VERSION::MAJOR >= 5
54
+ ActionDispatch::Request.parameter_parsers
55
+ else
56
+ ActionDispatch::ParamsParser::DEFAULT_PARSERS
57
+ end
58
+ assert_nil parsers[Mime[:jsonapi]]
59
+ end
60
+
61
+ def test_jsonapi_renderer_not_registered
62
+ expected = {
63
+ 'data' => {
64
+ 'attributes' => {
65
+ 'name' => 'Johnny Rico'
66
+ },
67
+ 'type' => 'users'
68
+ }
69
+ }
70
+ payload = '{"data": {"attributes": {"name": "Johnny Rico"}, "type": "authors"}}'
71
+ headers = { 'CONTENT_TYPE' => 'application/vnd.api+json' }
72
+ post '/render_with_jsonapi_renderer', params: payload, headers: headers
73
+ assert expected, response.body
74
+ end
75
+
76
+ def test_jsonapi_parser
77
+ assert_parses(
78
+ {},
79
+ '',
80
+ 'CONTENT_TYPE' => 'application/vnd.api+json'
81
+ )
82
+ end
83
+ end
84
+
85
+ class WithRenderer < JsonApiRendererTest
86
+ setup do
87
+ require 'rails'
88
+ require 'active_record'
89
+ require 'support/rails5_shims'
90
+ require 'active_model_serializers'
91
+ require 'fixtures/poro'
92
+ require 'active_model_serializers/register_jsonapi_renderer'
93
+
94
+ make_basic_app
95
+
96
+ Rails.application.routes.draw do
97
+ ActiveSupport::Deprecation.silence do
98
+ match ':action', to: TestController, via: [:get, :post]
99
+ end
100
+ end
101
+ end
102
+
103
+ def test_jsonapi_parser_registered
104
+ if Rails::VERSION::MAJOR >= 5
105
+ parsers = ActionDispatch::Request.parameter_parsers
106
+ assert_equal Proc, parsers[:jsonapi].class
107
+ else
108
+ parsers = ActionDispatch::ParamsParser::DEFAULT_PARSERS
109
+ assert_equal Proc, parsers[Mime[:jsonapi]].class
110
+ end
111
+ end
112
+
113
+ def test_jsonapi_renderer_registered
114
+ expected = {
115
+ 'data' => {
116
+ 'attributes' => {
117
+ 'name' => 'Johnny Rico'
118
+ },
119
+ 'type' => 'users'
120
+ }
121
+ }
122
+ payload = '{"data": {"attributes": {"name": "Johnny Rico"}, "type": "authors"}}'
123
+ headers = { 'CONTENT_TYPE' => 'application/vnd.api+json' }
124
+ post '/render_with_jsonapi_renderer', params: payload, headers: headers
125
+ assert expected, response.body
126
+ end
127
+
128
+ def test_jsonapi_parser
129
+ assert_parses(
130
+ {
131
+ 'data' => {
132
+ 'attributes' => {
133
+ 'name' => 'John Doe'
134
+ },
135
+ 'type' => 'users'
136
+ }
137
+ },
138
+ '{"data": {"attributes": {"name": "John Doe"}, "type": "users"}}',
139
+ 'CONTENT_TYPE' => 'application/vnd.api+json'
140
+ )
141
+ end
142
+ end
143
+ end
@@ -5,13 +5,19 @@ require 'minitest/mock'
5
5
  class SerializationContextTest < ActiveSupport::TestCase
6
6
  include ActiveSupport::Testing::Isolation
7
7
 
8
- def create_request
9
- request = Minitest::Mock.new
10
- request.expect(:original_url, 'original_url')
11
- request.expect(:query_parameters, 'query_parameters')
12
- end
13
-
14
8
  class WithRails < SerializationContextTest
9
+ def create_request
10
+ request = ActionDispatch::Request.new({})
11
+ def request.original_url
12
+ 'http://example.com/articles?page=2'
13
+ end
14
+
15
+ def request.query_parameters
16
+ { 'page' => 2 }
17
+ end
18
+ request
19
+ end
20
+
15
21
  setup do
16
22
  require 'rails'
17
23
  require 'active_model_serializers'
@@ -20,8 +26,8 @@ class SerializationContextTest < ActiveSupport::TestCase
20
26
  end
21
27
 
22
28
  test 'create context with request url and query parameters' do
23
- assert_equal @context.request_url, 'original_url'
24
- assert_equal @context.query_parameters, 'query_parameters'
29
+ assert_equal @context.request_url, 'http://example.com/articles'
30
+ assert_equal @context.query_parameters, 'page' => 2
25
31
  end
26
32
 
27
33
  test 'url_helpers is set up for Rails url_helpers' do
@@ -36,14 +42,21 @@ class SerializationContextTest < ActiveSupport::TestCase
36
42
  end
37
43
 
38
44
  class WithoutRails < SerializationContextTest
45
+ def create_request
46
+ {
47
+ request_url: 'http://example.com/articles',
48
+ query_parameters: { 'page' => 2 }
49
+ }
50
+ end
51
+
39
52
  setup do
40
53
  require 'active_model_serializers/serialization_context'
41
54
  @context = ActiveModelSerializers::SerializationContext.new(create_request)
42
55
  end
43
56
 
44
57
  test 'create context with request url and query parameters' do
45
- assert_equal @context.request_url, 'original_url'
46
- assert_equal @context.query_parameters, 'query_parameters'
58
+ assert_equal @context.request_url, 'http://example.com/articles'
59
+ assert_equal @context.query_parameters, 'page' => 2
47
60
  end
48
61
 
49
62
  test 'url_helpers is a module when Rails is not present' do
@@ -0,0 +1,43 @@
1
+ require 'test_helper'
2
+
3
+ module ActiveModelSerializers
4
+ module Adapter
5
+ class AttributesTest < ActiveSupport::TestCase
6
+ class Person
7
+ include ActiveModel::Model
8
+ include ActiveModel::Serialization
9
+
10
+ attr_accessor :first_name, :last_name
11
+ end
12
+
13
+ class PersonSerializer < ActiveModel::Serializer
14
+ attributes :first_name, :last_name
15
+ end
16
+
17
+ def setup
18
+ ActionController::Base.cache_store.clear
19
+ end
20
+
21
+ def test_serializable_hash
22
+ person = Person.new(first_name: 'Arthur', last_name: 'Dent')
23
+ serializer = PersonSerializer.new(person)
24
+ adapter = ActiveModelSerializers::Adapter::Attributes.new(serializer)
25
+
26
+ assert_equal({ first_name: 'Arthur', last_name: 'Dent' },
27
+ adapter.serializable_hash)
28
+ end
29
+
30
+ def test_serializable_hash_with_transform_key_casing
31
+ person = Person.new(first_name: 'Arthur', last_name: 'Dent')
32
+ serializer = PersonSerializer.new(person)
33
+ adapter = ActiveModelSerializers::Adapter::Attributes.new(
34
+ serializer,
35
+ key_transform: :camel_lower
36
+ )
37
+
38
+ assert_equal({ firstName: 'Arthur', lastName: 'Dent' },
39
+ adapter.serializable_hash)
40
+ end
41
+ end
42
+ end
43
+ end
@@ -84,6 +84,20 @@ module ActiveModelSerializers
84
84
 
85
85
  assert_equal(expected, actual)
86
86
  end
87
+
88
+ def test_fields_with_no_associations_include_option
89
+ actual = ActiveModelSerializers::SerializableResource.new(
90
+ [@first_post, @second_post], adapter: :json, fields: [:id], include: []
91
+ ).as_json
92
+
93
+ expected = { posts: [{
94
+ id: 1
95
+ }, {
96
+ id: 2
97
+ }] }
98
+
99
+ assert_equal(expected, actual)
100
+ end
87
101
  end
88
102
  end
89
103
  end
@@ -15,7 +15,7 @@ module ActiveModelSerializers
15
15
  @adapter = ActiveModelSerializers::Adapter::Json.new(serializer, options)
16
16
  end
17
17
 
18
- Post = Class.new(::Model)
18
+ class Post < ::Model; end
19
19
  class PostSerializer < ActiveModel::Serializer
20
20
  attributes :id, :title, :body, :publish_at
21
21
  end
@@ -28,8 +28,8 @@ module ActiveModelSerializers
28
28
  def test_transform_default
29
29
  mock_request
30
30
  assert_equal({
31
- blog: { id: 1, special_attribute: 'neat', articles: nil }
32
- }, @adapter.serializable_hash)
31
+ blog: { id: 1, special_attribute: 'neat', articles: nil }
32
+ }, @adapter.serializable_hash)
33
33
  end
34
34
 
35
35
  def test_transform_global_config
@@ -38,8 +38,8 @@ module ActiveModelSerializers
38
38
  @adapter.serializable_hash
39
39
  end
40
40
  assert_equal({
41
- blog: { id: 1, specialAttribute: 'neat', articles: nil }
42
- }, result)
41
+ blog: { id: 1, specialAttribute: 'neat', articles: nil }
42
+ }, result)
43
43
  end
44
44
 
45
45
  def test_transform_serialization_ctx_overrides_global_config
@@ -48,8 +48,8 @@ module ActiveModelSerializers
48
48
  @adapter.serializable_hash
49
49
  end
50
50
  assert_equal({
51
- Blog: { Id: 1, SpecialAttribute: 'neat', Articles: nil }
52
- }, result)
51
+ Blog: { Id: 1, SpecialAttribute: 'neat', Articles: nil }
52
+ }, result)
53
53
  end
54
54
 
55
55
  def test_transform_undefined
@@ -63,29 +63,29 @@ module ActiveModelSerializers
63
63
  def test_transform_dash
64
64
  mock_request(:dash)
65
65
  assert_equal({
66
- blog: { id: 1, :"special-attribute" => 'neat', articles: nil }
67
- }, @adapter.serializable_hash)
66
+ blog: { id: 1, :"special-attribute" => 'neat', articles: nil }
67
+ }, @adapter.serializable_hash)
68
68
  end
69
69
 
70
70
  def test_transform_unaltered
71
71
  mock_request(:unaltered)
72
72
  assert_equal({
73
- blog: { id: 1, special_attribute: 'neat', articles: nil }
74
- }, @adapter.serializable_hash)
73
+ blog: { id: 1, special_attribute: 'neat', articles: nil }
74
+ }, @adapter.serializable_hash)
75
75
  end
76
76
 
77
77
  def test_transform_camel
78
78
  mock_request(:camel)
79
79
  assert_equal({
80
- Blog: { Id: 1, SpecialAttribute: 'neat', Articles: nil }
81
- }, @adapter.serializable_hash)
80
+ Blog: { Id: 1, SpecialAttribute: 'neat', Articles: nil }
81
+ }, @adapter.serializable_hash)
82
82
  end
83
83
 
84
84
  def test_transform_camel_lower
85
85
  mock_request(:camel_lower)
86
86
  assert_equal({
87
- blog: { id: 1, specialAttribute: 'neat', articles: nil }
88
- }, @adapter.serializable_hash)
87
+ blog: { id: 1, specialAttribute: 'neat', articles: nil }
88
+ }, @adapter.serializable_hash)
89
89
  end
90
90
  end
91
91
  end
@@ -58,9 +58,10 @@ module ActiveModelSerializers
58
58
 
59
59
  def test_limiting_fields
60
60
  actual = ActiveModelSerializers::SerializableResource.new(
61
- [@first_post, @second_post], adapter: :json_api,
62
- fields: { posts: %w(title comments blog author) })
63
- .serializable_hash
61
+ [@first_post, @second_post],
62
+ adapter: :json_api,
63
+ fields: { posts: %w(title comments blog author) }
64
+ ).serializable_hash
64
65
  expected = [
65
66
  {
66
67
  id: '1',
@@ -12,8 +12,8 @@ module ActiveModelSerializers
12
12
 
13
13
  def test_active_model_with_error
14
14
  options = {
15
- serializer: ActiveModel::Serializer::ErrorSerializer,
16
- adapter: :json_api
15
+ serializer: ActiveModel::Serializer::ErrorSerializer,
16
+ adapter: :json_api
17
17
  }
18
18
 
19
19
  @resource.errors.add(:name, 'cannot be nil')
@@ -22,22 +22,21 @@ module ActiveModelSerializers
22
22
  assert_equal serializable_resource.serializer_instance.attributes, {}
23
23
  assert_equal serializable_resource.serializer_instance.object, @resource
24
24
 
25
- expected_errors_object =
26
- { :errors =>
27
- [
28
- {
29
- source: { pointer: '/data/attributes/name' },
30
- detail: 'cannot be nil'
31
- }
32
- ]
25
+ expected_errors_object = {
26
+ errors: [
27
+ {
28
+ source: { pointer: '/data/attributes/name' },
29
+ detail: 'cannot be nil'
30
+ }
31
+ ]
33
32
  }
34
33
  assert_equal serializable_resource.as_json, expected_errors_object
35
34
  end
36
35
 
37
36
  def test_active_model_with_multiple_errors
38
37
  options = {
39
- serializer: ActiveModel::Serializer::ErrorSerializer,
40
- adapter: :json_api
38
+ serializer: ActiveModel::Serializer::ErrorSerializer,
39
+ adapter: :json_api
41
40
  }
42
41
 
43
42
  @resource.errors.add(:name, 'cannot be nil')
@@ -48,13 +47,12 @@ module ActiveModelSerializers
48
47
  assert_equal serializable_resource.serializer_instance.attributes, {}
49
48
  assert_equal serializable_resource.serializer_instance.object, @resource
50
49
 
51
- expected_errors_object =
52
- { :errors =>
53
- [
54
- { :source => { :pointer => '/data/attributes/name' }, :detail => 'cannot be nil' },
55
- { :source => { :pointer => '/data/attributes/name' }, :detail => 'must be longer' },
56
- { :source => { :pointer => '/data/attributes/id' }, :detail => 'must be a uuid' }
57
- ]
50
+ expected_errors_object = {
51
+ errors: [
52
+ { source: { pointer: '/data/attributes/name' }, detail: 'cannot be nil' },
53
+ { source: { pointer: '/data/attributes/name' }, detail: 'must be longer' },
54
+ { source: { pointer: '/data/attributes/id' }, detail: 'must be a uuid' }
55
+ ]
58
56
  }
59
57
  assert_equal serializable_resource.as_json, expected_errors_object
60
58
  end
@@ -4,7 +4,10 @@ module ActiveModelSerializers
4
4
  module Adapter
5
5
  class JsonApi
6
6
  class FieldsTest < ActiveSupport::TestCase
7
- Post = Class.new(::Model)
7
+ class Post < ::Model; end
8
+ class Author < ::Model; end
9
+ class Comment < ::Model; end
10
+
8
11
  class PostSerializer < ActiveModel::Serializer
9
12
  type 'posts'
10
13
  attributes :title, :body
@@ -12,13 +15,11 @@ module ActiveModelSerializers
12
15
  has_many :comments
13
16
  end
14
17
 
15
- Author = Class.new(::Model)
16
18
  class AuthorSerializer < ActiveModel::Serializer
17
19
  type 'authors'
18
20
  attributes :name, :birthday
19
21
  end
20
22
 
21
- Comment = Class.new(::Model)
22
23
  class CommentSerializer < ActiveModel::Serializer
23
24
  type 'comments'
24
25
  attributes :body
@@ -40,6 +40,27 @@ module ActiveModelSerializers
40
40
  assert_equal(expected, @adapter.serializable_hash[:data][:relationships][:comments])
41
41
  end
42
42
 
43
+ test 'relationships can be whitelisted via fields' do
44
+ @adapter = ActiveModelSerializers::Adapter::JsonApi.new(@serializer, fields: { posts: [:author] })
45
+ result = @adapter.serializable_hash
46
+ expected = {
47
+ data: {
48
+ id: '1',
49
+ type: 'posts',
50
+ relationships: {
51
+ author: {
52
+ data: {
53
+ id: '1',
54
+ type: 'authors'
55
+ }
56
+ }
57
+ }
58
+ }
59
+ }
60
+
61
+ assert_equal expected, result
62
+ end
63
+
43
64
  def test_includes_linked_comments
44
65
  @adapter = ActiveModelSerializers::Adapter::JsonApi.new(@serializer, include: [:comments])
45
66
  expected = [{
@@ -112,14 +133,14 @@ module ActiveModelSerializers
112
133
  adapter = ActiveModelSerializers::Adapter::JsonApi.new(serializer)
113
134
 
114
135
  assert_equal({
115
- data: {
116
- id: '1',
117
- type: 'posts',
118
- relationships: {
119
- tags: { data: [@tag.as_json] }
120
- }
121
- }
122
- }, adapter.serializable_hash)
136
+ data: {
137
+ id: '1',
138
+ type: 'posts',
139
+ relationships: {
140
+ tags: { data: [@tag.as_json] }
141
+ }
142
+ }
143
+ }, adapter.serializable_hash)
123
144
  end
124
145
 
125
146
  def test_has_many_with_virtual_value
@@ -127,16 +148,16 @@ module ActiveModelSerializers
127
148
  adapter = ActiveModelSerializers::Adapter::JsonApi.new(serializer)
128
149
 
129
150
  assert_equal({
130
- data: {
131
- id: '1',
132
- type: 'virtual-values',
133
- relationships: {
134
- maker: { data: { type: 'makers', id: '1' } },
135
- reviews: { data: [{ type: 'reviews', id: '1' },
136
- { type: 'reviews', id: '2' }] }
137
- }
138
- }
139
- }, adapter.serializable_hash)
151
+ data: {
152
+ id: '1',
153
+ type: 'virtual-values',
154
+ relationships: {
155
+ maker: { data: { type: 'makers', id: '1' } },
156
+ reviews: { data: [{ type: 'reviews', id: '1' },
157
+ { type: 'reviews', id: '2' }] }
158
+ }
159
+ }
160
+ }, adapter.serializable_hash)
140
161
  end
141
162
  end
142
163
  end