active_model_serializers 0.10.0 → 0.10.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (109) hide show
  1. checksums.yaml +4 -4
  2. data/.rubocop.yml +2 -4
  3. data/.travis.yml +1 -0
  4. data/CHANGELOG.md +39 -2
  5. data/Gemfile +1 -1
  6. data/README.md +21 -24
  7. data/Rakefile +3 -3
  8. data/active_model_serializers.gemspec +19 -23
  9. data/docs/general/adapters.md +4 -2
  10. data/docs/general/configuration_options.md +6 -1
  11. data/docs/general/deserialization.md +1 -1
  12. data/docs/general/fields.md +31 -0
  13. data/docs/general/rendering.md +7 -2
  14. data/docs/general/serializers.md +62 -3
  15. data/docs/howto/add_pagination_links.md +2 -3
  16. data/docs/integrations/ember-and-json-api.md +25 -10
  17. data/docs/jsonapi/schema.md +1 -1
  18. data/lib/action_controller/serialization.rb +4 -3
  19. data/lib/active_model/serializer/adapter/base.rb +2 -0
  20. data/lib/active_model/serializer/array_serializer.rb +8 -5
  21. data/lib/active_model/serializer/associations.rb +6 -7
  22. data/lib/active_model/serializer/belongs_to_reflection.rb +0 -3
  23. data/lib/active_model/serializer/caching.rb +67 -112
  24. data/lib/active_model/serializer/collection_serializer.rb +30 -10
  25. data/lib/active_model/serializer/configuration.rb +1 -0
  26. data/lib/active_model/serializer/error_serializer.rb +11 -7
  27. data/lib/active_model/serializer/errors_serializer.rb +25 -20
  28. data/lib/active_model/serializer/has_many_reflection.rb +0 -3
  29. data/lib/active_model/serializer/has_one_reflection.rb +0 -3
  30. data/lib/active_model/serializer/lint.rb +134 -130
  31. data/lib/active_model/serializer/reflection.rb +3 -3
  32. data/lib/active_model/serializer/version.rb +1 -1
  33. data/lib/active_model/serializer.rb +57 -15
  34. data/lib/active_model_serializers/adapter/attributes.rb +2 -67
  35. data/lib/active_model_serializers/adapter/base.rb +38 -38
  36. data/lib/active_model_serializers/adapter/json_api/link.rb +1 -1
  37. data/lib/active_model_serializers/adapter/json_api/pagination_links.rb +8 -1
  38. data/lib/active_model_serializers/adapter/json_api.rb +36 -28
  39. data/lib/active_model_serializers/adapter.rb +6 -0
  40. data/lib/active_model_serializers/deprecate.rb +1 -2
  41. data/lib/active_model_serializers/deserialization.rb +2 -0
  42. data/lib/active_model_serializers/model.rb +3 -1
  43. data/lib/active_model_serializers/railtie.rb +3 -1
  44. data/lib/active_model_serializers/register_jsonapi_renderer.rb +44 -31
  45. data/lib/active_model_serializers/serialization_context.rb +10 -3
  46. data/lib/active_model_serializers.rb +6 -0
  47. data/lib/generators/rails/serializer_generator.rb +3 -3
  48. data/lib/grape/active_model_serializers.rb +7 -5
  49. data/lib/grape/formatters/active_model_serializers.rb +19 -2
  50. data/lib/grape/helpers/active_model_serializers.rb +1 -0
  51. data/test/action_controller/adapter_selector_test.rb +4 -4
  52. data/test/action_controller/explicit_serializer_test.rb +5 -4
  53. data/test/action_controller/json/include_test.rb +106 -27
  54. data/test/action_controller/json_api/errors_test.rb +6 -7
  55. data/test/action_controller/json_api/linked_test.rb +29 -24
  56. data/test/action_controller/json_api/pagination_test.rb +19 -19
  57. data/test/action_controller/serialization_test.rb +10 -7
  58. data/test/active_model_serializers/json_pointer_test.rb +15 -13
  59. data/test/active_model_serializers/key_transform_test.rb +254 -252
  60. data/test/active_model_serializers/model_test.rb +6 -4
  61. data/test/active_model_serializers/register_jsonapi_renderer_test_isolated.rb +143 -0
  62. data/test/active_model_serializers/serialization_context_test_isolated.rb +23 -10
  63. data/test/adapter/json/collection_test.rb +14 -0
  64. data/test/adapter/json/transform_test.rb +14 -14
  65. data/test/adapter/json_api/collection_test.rb +4 -3
  66. data/test/adapter/json_api/errors_test.rb +17 -19
  67. data/test/adapter/json_api/has_many_test.rb +18 -18
  68. data/test/adapter/json_api/json_api_test.rb +5 -7
  69. data/test/adapter/json_api/linked_test.rb +9 -6
  70. data/test/adapter/json_api/links_test.rb +3 -1
  71. data/test/adapter/json_api/pagination_links_test.rb +19 -7
  72. data/test/adapter/json_api/relationships_test.rb +9 -4
  73. data/test/adapter/json_api/resource_identifier_test.rb +7 -2
  74. data/test/adapter/json_api/resource_meta_test.rb +3 -3
  75. data/test/adapter/json_api/transform_test.rb +251 -250
  76. data/test/adapter/json_api/type_test.rb +1 -1
  77. data/test/adapter/json_test.rb +8 -7
  78. data/test/adapter/null_test.rb +1 -2
  79. data/test/adapter/polymorphic_test.rb +5 -5
  80. data/test/adapter_test.rb +1 -1
  81. data/test/benchmark/app.rb +1 -1
  82. data/test/benchmark/bm_caching.rb +15 -15
  83. data/test/benchmark/bm_transform.rb +16 -5
  84. data/test/benchmark/controllers.rb +16 -17
  85. data/test/benchmark/fixtures.rb +72 -72
  86. data/test/cache_test.rb +101 -45
  87. data/test/collection_serializer_test.rb +2 -2
  88. data/test/fixtures/poro.rb +8 -7
  89. data/test/grape_test.rb +152 -56
  90. data/test/lint_test.rb +1 -1
  91. data/test/logger_test.rb +13 -11
  92. data/test/serializable_resource_test.rb +18 -22
  93. data/test/serializers/associations_test.rb +10 -10
  94. data/test/serializers/attribute_test.rb +1 -1
  95. data/test/serializers/attributes_test.rb +1 -1
  96. data/test/serializers/fieldset_test.rb +1 -1
  97. data/test/serializers/meta_test.rb +12 -6
  98. data/test/serializers/root_test.rb +1 -1
  99. data/test/serializers/serializer_for_test.rb +3 -1
  100. data/test/support/isolated_unit.rb +5 -2
  101. data/test/support/rails5_shims.rb +8 -2
  102. data/test/support/rails_app.rb +0 -9
  103. data/test/support/serialization_testing.rb +7 -5
  104. metadata +54 -24
  105. data/.rubocop_todo.yml +0 -167
  106. data/lib/active_model/serializer/include_tree.rb +0 -111
  107. data/test/include_tree/from_include_args_test.rb +0 -26
  108. data/test/include_tree/from_string_test.rb +0 -94
  109. data/test/include_tree/include_args_to_hash_test.rb +0 -64
@@ -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
@@ -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
@@ -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
@@ -112,14 +112,14 @@ module ActiveModelSerializers
112
112
  adapter = ActiveModelSerializers::Adapter::JsonApi.new(serializer)
113
113
 
114
114
  assert_equal({
115
- data: {
116
- id: '1',
117
- type: 'posts',
118
- relationships: {
119
- tags: { data: [@tag.as_json] }
120
- }
121
- }
122
- }, adapter.serializable_hash)
115
+ data: {
116
+ id: '1',
117
+ type: 'posts',
118
+ relationships: {
119
+ tags: { data: [@tag.as_json] }
120
+ }
121
+ }
122
+ }, adapter.serializable_hash)
123
123
  end
124
124
 
125
125
  def test_has_many_with_virtual_value
@@ -127,16 +127,16 @@ module ActiveModelSerializers
127
127
  adapter = ActiveModelSerializers::Adapter::JsonApi.new(serializer)
128
128
 
129
129
  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)
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)
140
140
  end
141
141
  end
142
142
  end
@@ -22,13 +22,11 @@ module ActiveModelSerializers
22
22
  adapter = ActiveModelSerializers::Adapter::JsonApi.new(serializer)
23
23
 
24
24
  assert_equal({
25
- reviews: { data: [
26
- { type: 'comments', id: '1' },
27
- { type: 'comments', id: '2' }
28
- ] },
29
- writer: { data: { type: 'authors', id: '1' } },
30
- site: { data: { type: 'blogs', id: '1' } }
31
- }, adapter.serializable_hash[:data][:relationships])
25
+ reviews: { data: [{ type: 'comments', id: '1' },
26
+ { type: 'comments', id: '2' }] },
27
+ writer: { data: { type: 'authors', id: '1' } },
28
+ site: { data: { type: 'blogs', id: '1' } }
29
+ }, adapter.serializable_hash[:data][:relationships])
32
30
  end
33
31
  end
34
32
  end
@@ -17,7 +17,7 @@ module ActiveModelSerializers
17
17
  @first_post = Post.new(id: 10, title: 'Hello!!', body: 'Hello, world!!')
18
18
  @second_post = Post.new(id: 20, title: 'New Post', body: 'Body')
19
19
  @third_post = Post.new(id: 30, title: 'Yet Another Post', body: 'Body')
20
- @blog = Blog.new({ name: 'AMS Blog' })
20
+ @blog = Blog.new(name: 'AMS Blog')
21
21
  @first_comment = Comment.new(id: 1, body: 'ZOMG A COMMENT')
22
22
  @second_comment = Comment.new(id: 2, body: 'ZOMG ANOTHER COMMENT')
23
23
  @first_post.blog = @blog
@@ -341,9 +341,10 @@ module ActiveModelSerializers
341
341
 
342
342
  def test_no_duplicates_collection
343
343
  hash = ActiveModelSerializers::SerializableResource.new(
344
- [@post1, @post2], adapter: :json_api,
345
- include: '*.*')
346
- .serializable_hash
344
+ [@post1, @post2],
345
+ adapter: :json_api,
346
+ include: '*.*'
347
+ ).serializable_hash
347
348
  expected = [
348
349
  {
349
350
  type: 'authors', id: '1',
@@ -364,7 +365,8 @@ module ActiveModelSerializers
364
365
  hash = ActiveModelSerializers::SerializableResource.new(
365
366
  @nestedpost1,
366
367
  adapter: :json_api,
367
- include: '*').serializable_hash
368
+ include: '*'
369
+ ).serializable_hash
368
370
  expected = [
369
371
  type: 'nested-posts', id: '2',
370
372
  relationships: {
@@ -383,7 +385,8 @@ module ActiveModelSerializers
383
385
  hash = ActiveModelSerializers::SerializableResource.new(
384
386
  [@nestedpost1, @nestedpost2],
385
387
  adapter: :json_api,
386
- include: '*').serializable_hash
388
+ include: '*'
389
+ ).serializable_hash
387
390
  assert_nil(hash[:included])
388
391
  end
389
392
  end
@@ -17,6 +17,7 @@ module ActiveModelSerializers
17
17
  link :yet_another do
18
18
  "http://example.com/resource/#{object.id}"
19
19
  end
20
+ link(:nil) { nil }
20
21
  end
21
22
 
22
23
  def setup
@@ -40,7 +41,8 @@ module ActiveModelSerializers
40
41
  stuff: 'value'
41
42
  }
42
43
  }
43
- }).serializable_hash
44
+ }
45
+ ).serializable_hash
44
46
  expected = {
45
47
  self: {
46
48
  href: 'http://example.com/posts',
@@ -13,11 +13,11 @@ module ActiveModelSerializers
13
13
  def setup
14
14
  ActionController::Base.cache_store.clear
15
15
  @array = [
16
- Profile.new({ id: 1, name: 'Name 1', description: 'Description 1', comments: 'Comments 1' }),
17
- Profile.new({ id: 2, name: 'Name 2', description: 'Description 2', comments: 'Comments 2' }),
18
- Profile.new({ id: 3, name: 'Name 3', description: 'Description 3', comments: 'Comments 3' }),
19
- Profile.new({ id: 4, name: 'Name 4', description: 'Description 4', comments: 'Comments 4' }),
20
- Profile.new({ id: 5, name: 'Name 5', description: 'Description 5', comments: 'Comments 5' })
16
+ Profile.new(id: 1, name: 'Name 1', description: 'Description 1', comments: 'Comments 1'),
17
+ Profile.new(id: 2, name: 'Name 2', description: 'Description 2', comments: 'Comments 2'),
18
+ Profile.new(id: 3, name: 'Name 3', description: 'Description 3', comments: 'Comments 3'),
19
+ Profile.new(id: 4, name: 'Name 4', description: 'Description 4', comments: 'Comments 4'),
20
+ Profile.new(id: 5, name: 'Name 5', description: 'Description 5', comments: 'Comments 5')
21
21
  ]
22
22
  end
23
23
 
@@ -43,7 +43,8 @@ module ActiveModelSerializers
43
43
  end
44
44
 
45
45
  def data
46
- { data: [
46
+ {
47
+ data: [
47
48
  { id: '1', type: 'profiles', attributes: { name: 'Name 1', description: 'Description 1' } },
48
49
  { id: '2', type: 'profiles', attributes: { name: 'Name 2', description: 'Description 2' } },
49
50
  { id: '3', type: 'profiles', attributes: { name: 'Name 3', description: 'Description 3' } },
@@ -121,7 +122,7 @@ module ActiveModelSerializers
121
122
  end
122
123
 
123
124
  def test_pagination_links_with_additional_params
124
- adapter = load_adapter(using_will_paginate, mock_request({ test: 'test' }))
125
+ adapter = load_adapter(using_will_paginate, mock_request(test: 'test'))
125
126
 
126
127
  assert_equal expected_response_with_pagination_links_and_additional_params,
127
128
  adapter.serializable_hash
@@ -160,6 +161,17 @@ module ActiveModelSerializers
160
161
 
161
162
  assert_equal expected_response_without_pagination_links, adapter.serializable_hash
162
163
  end
164
+
165
+ def test_raises_descriptive_error_when_serialization_context_unset
166
+ render_options = { adapter: :json_api }
167
+ adapter = serializable(using_kaminari, render_options)
168
+ exception = assert_raises do
169
+ adapter.as_json
170
+ end
171
+ exception_class = ActiveModelSerializers::Adapter::JsonApi::PaginationLinks::MissingSerializationContextError
172
+ assert_equal exception_class, exception.class
173
+ assert_match(/CollectionSerializer#paginated\?/, exception.message)
174
+ end
163
175
  end
164
176
  end
165
177
  end
@@ -5,7 +5,8 @@ module ActiveModel
5
5
  module Adapter
6
6
  class JsonApi
7
7
  class RelationshipTest < ActiveSupport::TestCase
8
- RelationshipAuthor = Class.new(::Model)
8
+ class RelationshipAuthor < ::Model; end
9
+
9
10
  class RelationshipAuthorSerializer < ActiveModel::Serializer
10
11
  has_one :bio do
11
12
  link :self, '//example.com/link_author/relationships/bio'
@@ -71,7 +72,6 @@ module ActiveModel
71
72
 
72
73
  def setup
73
74
  @post = Post.new(id: 1337, comments: [], author: nil)
74
- @blog = Blog.new(id: 1337, name: 'extra')
75
75
  @bio = Bio.new(id: 1337)
76
76
  @like = Like.new(id: 1337)
77
77
  @role = Role.new(id: 'from-record')
@@ -82,7 +82,6 @@ module ActiveModel
82
82
  @author = RelationshipAuthor.new(
83
83
  id: 1337,
84
84
  posts: [@post],
85
- blog: @blog,
86
85
  reviewer: @reviewer,
87
86
  bio: @bio,
88
87
  likes: [@like],
@@ -158,10 +157,16 @@ module ActiveModel
158
157
  end
159
158
 
160
159
  def test_relationship_not_including_data
160
+ @author.define_singleton_method(:read_attribute_for_serialization) do |attr|
161
+ fail 'should not be called' if attr == :blog
162
+ super(attr)
163
+ end
161
164
  expected = {
162
165
  links: { self: '//example.com/link_author/relationships/blog' }
163
166
  }
164
- assert_relationship(:blog, expected)
167
+ assert_nothing_raised do
168
+ assert_relationship(:blog, expected)
169
+ end
165
170
  end
166
171
 
167
172
  def test_relationship_including_data_explicit
@@ -14,7 +14,13 @@ module ActiveModelSerializers
14
14
  end
15
15
  end
16
16
 
17
- class FragmentedSerializer < ActiveModel::Serializer; end
17
+ class FragmentedSerializer < ActiveModel::Serializer
18
+ cache only: :id
19
+
20
+ def id
21
+ 'special_id'
22
+ end
23
+ end
18
24
 
19
25
  setup do
20
26
  @model = Author.new(id: 1, name: 'Steve K.')
@@ -42,7 +48,6 @@ module ActiveModelSerializers
42
48
  end
43
49
 
44
50
  def test_id_defined_on_fragmented
45
- FragmentedSerializer.fragmented(WithDefinedIdSerializer.new(@model))
46
51
  test_id(FragmentedSerializer, 'special_id')
47
52
  end
48
53
 
@@ -68,9 +68,9 @@ module ActiveModel
68
68
  adapter: :json_api
69
69
  ).serializable_hash
70
70
  expected = {
71
- :data => [
72
- { :id => '1337', :type => 'posts', :meta => { :"comments-count" => 0 } },
73
- { :id => '1339', :type => 'posts', :meta => { :"comments-count" => 1 } }
71
+ data: [
72
+ { id: '1337', type: 'posts', meta: { :"comments-count" => 0 } },
73
+ { id: '1339', type: 'posts', meta: { :"comments-count" => 1 } }
74
74
  ]
75
75
  }
76
76
  assert_equal(expected, hash)