jsonapi-resources 0.7.1.beta1 → 0.7.1.beta2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (93) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +248 -74
  3. data/lib/jsonapi-resources.rb +5 -3
  4. data/lib/jsonapi/acts_as_resource_controller.rb +77 -14
  5. data/lib/jsonapi/configuration.rb +77 -16
  6. data/lib/jsonapi/error.rb +12 -0
  7. data/lib/jsonapi/error_codes.rb +2 -0
  8. data/lib/jsonapi/exceptions.rb +29 -9
  9. data/lib/jsonapi/formatter.rb +29 -4
  10. data/lib/jsonapi/link_builder.rb +18 -18
  11. data/lib/jsonapi/mime_types.rb +25 -6
  12. data/lib/jsonapi/naive_cache.rb +30 -0
  13. data/lib/jsonapi/operation.rb +10 -342
  14. data/lib/jsonapi/operation_dispatcher.rb +87 -0
  15. data/lib/jsonapi/operation_result.rb +2 -1
  16. data/lib/jsonapi/paginator.rb +6 -2
  17. data/lib/jsonapi/processor.rb +283 -0
  18. data/lib/jsonapi/relationship.rb +6 -4
  19. data/lib/jsonapi/{request.rb → request_parser.rb} +46 -35
  20. data/lib/jsonapi/resource.rb +88 -13
  21. data/lib/jsonapi/resource_controller.rb +2 -14
  22. data/lib/jsonapi/resource_controller_metal.rb +17 -0
  23. data/lib/jsonapi/resource_serializer.rb +62 -47
  24. data/lib/jsonapi/resources/version.rb +1 -1
  25. data/lib/jsonapi/response_document.rb +13 -2
  26. data/lib/jsonapi/routing_ext.rb +49 -11
  27. metadata +37 -129
  28. data/.gitignore +0 -22
  29. data/.travis.yml +0 -9
  30. data/Gemfile +0 -23
  31. data/Rakefile +0 -20
  32. data/jsonapi-resources.gemspec +0 -29
  33. data/lib/jsonapi/active_record_operations_processor.rb +0 -35
  34. data/lib/jsonapi/operations_processor.rb +0 -120
  35. data/locales/en.yml +0 -80
  36. data/test/config/database.yml +0 -5
  37. data/test/controllers/controller_test.rb +0 -3312
  38. data/test/fixtures/active_record.rb +0 -1486
  39. data/test/fixtures/author_details.yml +0 -9
  40. data/test/fixtures/book_authors.yml +0 -3
  41. data/test/fixtures/book_comments.yml +0 -12
  42. data/test/fixtures/books.yml +0 -7
  43. data/test/fixtures/categories.yml +0 -35
  44. data/test/fixtures/comments.yml +0 -21
  45. data/test/fixtures/comments_tags.yml +0 -20
  46. data/test/fixtures/companies.yml +0 -4
  47. data/test/fixtures/craters.yml +0 -9
  48. data/test/fixtures/customers.yml +0 -11
  49. data/test/fixtures/documents.yml +0 -3
  50. data/test/fixtures/expense_entries.yml +0 -13
  51. data/test/fixtures/facts.yml +0 -11
  52. data/test/fixtures/hair_cuts.yml +0 -3
  53. data/test/fixtures/iso_currencies.yml +0 -17
  54. data/test/fixtures/line_items.yml +0 -37
  55. data/test/fixtures/makes.yml +0 -2
  56. data/test/fixtures/moons.yml +0 -6
  57. data/test/fixtures/numeros_telefone.yml +0 -3
  58. data/test/fixtures/order_flags.yml +0 -7
  59. data/test/fixtures/people.yml +0 -31
  60. data/test/fixtures/pictures.yml +0 -15
  61. data/test/fixtures/planet_types.yml +0 -19
  62. data/test/fixtures/planets.yml +0 -47
  63. data/test/fixtures/posts.yml +0 -102
  64. data/test/fixtures/posts_tags.yml +0 -59
  65. data/test/fixtures/preferences.yml +0 -14
  66. data/test/fixtures/products.yml +0 -3
  67. data/test/fixtures/purchase_orders.yml +0 -23
  68. data/test/fixtures/sections.yml +0 -8
  69. data/test/fixtures/tags.yml +0 -39
  70. data/test/fixtures/vehicles.yml +0 -17
  71. data/test/fixtures/web_pages.yml +0 -3
  72. data/test/helpers/assertions.rb +0 -13
  73. data/test/helpers/functional_helpers.rb +0 -59
  74. data/test/helpers/value_matchers.rb +0 -60
  75. data/test/helpers/value_matchers_test.rb +0 -40
  76. data/test/integration/requests/namespaced_model_test.rb +0 -13
  77. data/test/integration/requests/request_test.rb +0 -932
  78. data/test/integration/routes/routes_test.rb +0 -218
  79. data/test/integration/sti_fields_test.rb +0 -18
  80. data/test/lib/generators/jsonapi/controller_generator_test.rb +0 -25
  81. data/test/lib/generators/jsonapi/resource_generator_test.rb +0 -30
  82. data/test/test_helper.rb +0 -342
  83. data/test/unit/formatters/dasherized_key_formatter_test.rb +0 -8
  84. data/test/unit/jsonapi_request/jsonapi_request_test.rb +0 -199
  85. data/test/unit/operation/operations_processor_test.rb +0 -528
  86. data/test/unit/pagination/offset_paginator_test.rb +0 -245
  87. data/test/unit/pagination/paged_paginator_test.rb +0 -242
  88. data/test/unit/resource/resource_test.rb +0 -560
  89. data/test/unit/serializer/include_directives_test.rb +0 -113
  90. data/test/unit/serializer/link_builder_test.rb +0 -244
  91. data/test/unit/serializer/polymorphic_serializer_test.rb +0 -383
  92. data/test/unit/serializer/response_document_test.rb +0 -61
  93. data/test/unit/serializer/serializer_test.rb +0 -1939
@@ -1,113 +0,0 @@
1
- require File.expand_path('../../../test_helper', __FILE__)
2
- require 'jsonapi-resources'
3
-
4
- class IncludeDirectivesTest < ActiveSupport::TestCase
5
-
6
- def test_one_level_one_include
7
- directives = JSONAPI::IncludeDirectives.new(['posts']).include_directives
8
-
9
- assert_hash_equals(
10
- {
11
- include_related: {
12
- posts: {
13
- include: true,
14
- include_related:{}
15
- }
16
- }
17
- },
18
- directives)
19
- end
20
-
21
- def test_one_level_multiple_includes
22
- directives = JSONAPI::IncludeDirectives.new(['posts', 'comments', 'tags']).include_directives
23
-
24
- assert_hash_equals(
25
- {
26
- include_related: {
27
- posts: {
28
- include: true,
29
- include_related:{}
30
- },
31
- comments: {
32
- include: true,
33
- include_related:{}
34
- },
35
- tags: {
36
- include: true,
37
- include_related:{}
38
- }
39
- }
40
- },
41
- directives)
42
- end
43
-
44
- def test_two_levels_include_full_path
45
- directives = JSONAPI::IncludeDirectives.new(['posts.comments']).include_directives
46
-
47
- assert_hash_equals(
48
- {
49
- include_related: {
50
- posts: {
51
- include: true,
52
- include_related:{
53
- comments: {
54
- include: true,
55
- include_related:{}
56
- }
57
- }
58
- }
59
- }
60
- },
61
- directives)
62
- end
63
-
64
- def test_two_levels_include_full_path_redundant
65
- directives = JSONAPI::IncludeDirectives.new(['posts','posts.comments']).include_directives
66
-
67
- assert_hash_equals(
68
- {
69
- include_related: {
70
- posts: {
71
- include: true,
72
- include_related:{
73
- comments: {
74
- include: true,
75
- include_related:{}
76
- }
77
- }
78
- }
79
- }
80
- },
81
- directives)
82
- end
83
-
84
- def test_three_levels_include_full
85
- directives = JSONAPI::IncludeDirectives.new(['posts.comments.tags']).include_directives
86
-
87
- assert_hash_equals(
88
- {
89
- include_related: {
90
- posts: {
91
- include: true,
92
- include_related:{
93
- comments: {
94
- include: true,
95
- include_related:{
96
- tags: {
97
- include: true,
98
- include_related:{}
99
- }
100
- }
101
- }
102
- }
103
- }
104
- }
105
- },
106
- directives)
107
- end
108
-
109
- def test_three_levels_include_full_model_includes
110
- directives = JSONAPI::IncludeDirectives.new(['posts.comments.tags'])
111
- assert_array_equals([{:posts=>[{:comments=>[:tags]}]}], directives.model_includes)
112
- end
113
- end
@@ -1,244 +0,0 @@
1
- require File.expand_path('../../../test_helper', __FILE__)
2
- require 'jsonapi-resources'
3
- require 'json'
4
-
5
- class LinkBuilderTest < ActionDispatch::IntegrationTest
6
- def setup
7
- @base_url = "http://example.com"
8
- @route_formatter = JSONAPI.configuration.route_formatter
9
- @steve = Person.create(name: "Steve Rogers", date_joined: "1941-03-01")
10
- end
11
-
12
- def test_engine_boolean
13
- assert JSONAPI::LinkBuilder.new(
14
- primary_resource_klass: MyEngine::Api::V1::PersonResource
15
- ).engine?, "MyEngine should be considered an Engine"
16
-
17
- refute JSONAPI::LinkBuilder.new(
18
- primary_resource_klass: Api::V1::PersonResource
19
- ).engine?, "Api shouldn't be considered an Engine"
20
- end
21
-
22
- def test_engine_name
23
- assert_equal MyEngine::Engine,
24
- JSONAPI::LinkBuilder.new(
25
- primary_resource_klass: MyEngine::Api::V1::PersonResource
26
- ).engine_name
27
-
28
- assert_equal nil,
29
- JSONAPI::LinkBuilder.new(
30
- primary_resource_klass: Api::V1::PersonResource
31
- ).engine_name
32
- end
33
-
34
- def test_self_link_regular_app
35
- primary_resource_klass = Api::V1::PersonResource
36
-
37
- config = {
38
- base_url: @base_url,
39
- route_formatter: @route_formatter,
40
- primary_resource_klass: primary_resource_klass,
41
- }
42
-
43
- builder = JSONAPI::LinkBuilder.new(config)
44
- source = primary_resource_klass.new(@steve, nil)
45
- expected_link = "#{ @base_url }/api/v1/people/#{ source.id }"
46
-
47
- assert_equal expected_link, builder.self_link(source)
48
- end
49
-
50
- def test_self_link_with_engine_app
51
- primary_resource_klass = MyEngine::Api::V1::PersonResource
52
-
53
- config = {
54
- base_url: @base_url,
55
- route_formatter: @route_formatter,
56
- primary_resource_klass: primary_resource_klass,
57
- }
58
-
59
- builder = JSONAPI::LinkBuilder.new(config)
60
- source = primary_resource_klass.new(@steve, nil)
61
- expected_link = "#{ @base_url }/boomshaka/api/v1/people/#{ source.id }"
62
-
63
- assert_equal expected_link, builder.self_link(source)
64
- end
65
-
66
- def test_self_link_with_engine_app_and_camel_case_scope
67
- primary_resource_klass = MyEngine::AdminApi::V1::PersonResource
68
-
69
- config = {
70
- base_url: @base_url,
71
- route_formatter: @route_formatter,
72
- primary_resource_klass: primary_resource_klass,
73
- }
74
-
75
- builder = JSONAPI::LinkBuilder.new(config)
76
- source = primary_resource_klass.new(@steve, nil)
77
- expected_link = "#{ @base_url }/boomshaka/admin_api/v1/people/#{ source.id }"
78
-
79
- assert_equal expected_link, builder.self_link(source)
80
- end
81
-
82
- def test_primary_resources_url_for_regular_app
83
- config = {
84
- base_url: @base_url,
85
- route_formatter: @route_formatter,
86
- primary_resource_klass: Api::V1::PersonResource,
87
- }
88
-
89
- builder = JSONAPI::LinkBuilder.new(config)
90
- expected_link = "#{ @base_url }/api/v1/people"
91
-
92
- assert_equal expected_link, builder.primary_resources_url
93
- end
94
-
95
- def test_primary_resources_url_for_engine
96
- config = {
97
- base_url: @base_url,
98
- route_formatter: @route_formatter,
99
- primary_resource_klass: MyEngine::Api::V1::PersonResource
100
- }
101
-
102
- builder = JSONAPI::LinkBuilder.new(config)
103
- expected_link = "#{ @base_url }/boomshaka/api/v1/people"
104
-
105
- assert_equal expected_link, builder.primary_resources_url
106
- end
107
-
108
- def test_relationships_self_link_for_regular_app
109
- config = {
110
- base_url: @base_url,
111
- route_formatter: @route_formatter,
112
- primary_resource_klass: Api::V1::PersonResource
113
- }
114
-
115
- builder = JSONAPI::LinkBuilder.new(config)
116
- source = Api::V1::PersonResource.new(@steve, nil)
117
- relationship = JSONAPI::Relationship::ToMany.new("posts", {})
118
- expected_link = "#{ @base_url }/api/v1/people/#{ @steve.id }/relationships/posts"
119
-
120
- assert_equal expected_link,
121
- builder.relationships_self_link(source, relationship)
122
- end
123
-
124
- def test_relationships_self_link_for_engine
125
- config = {
126
- base_url: @base_url,
127
- route_formatter: @route_formatter,
128
- primary_resource_klass: MyEngine::Api::V1::PersonResource
129
- }
130
-
131
- builder = JSONAPI::LinkBuilder.new(config)
132
- source = MyEngine::Api::V1::PersonResource.new(@steve, nil)
133
- relationship = JSONAPI::Relationship::ToMany.new("posts", {})
134
- expected_link = "#{ @base_url }/boomshaka/api/v1/people/#{ @steve.id }/relationships/posts"
135
-
136
- assert_equal expected_link,
137
- builder.relationships_self_link(source, relationship)
138
- end
139
-
140
- def test_relationships_related_link_for_regular_app
141
- config = {
142
- base_url: @base_url,
143
- route_formatter: @route_formatter,
144
- primary_resource_klass: Api::V1::PersonResource
145
- }
146
-
147
- builder = JSONAPI::LinkBuilder.new(config)
148
- source = Api::V1::PersonResource.new(@steve, nil)
149
- relationship = JSONAPI::Relationship::ToMany.new("posts", {})
150
- expected_link = "#{ @base_url }/api/v1/people/#{ @steve.id }/posts"
151
-
152
- assert_equal expected_link,
153
- builder.relationships_related_link(source, relationship)
154
- end
155
-
156
- def test_relationships_related_link_for_engine
157
- config = {
158
- base_url: @base_url,
159
- route_formatter: @route_formatter,
160
- primary_resource_klass: MyEngine::Api::V1::PersonResource
161
- }
162
-
163
- builder = JSONAPI::LinkBuilder.new(config)
164
- source = MyEngine::Api::V1::PersonResource.new(@steve, nil)
165
- relationship = JSONAPI::Relationship::ToMany.new("posts", {})
166
- expected_link = "#{ @base_url }/boomshaka/api/v1/people/#{ @steve.id }/posts"
167
-
168
- assert_equal expected_link,
169
- builder.relationships_related_link(source, relationship)
170
- end
171
-
172
- def test_relationships_related_link_with_query_params
173
- config = {
174
- base_url: @base_url,
175
- route_formatter: @route_formatter,
176
- primary_resource_klass: Api::V1::PersonResource
177
- }
178
-
179
- builder = JSONAPI::LinkBuilder.new(config)
180
- source = Api::V1::PersonResource.new(@steve, nil)
181
- relationship = JSONAPI::Relationship::ToMany.new("posts", {})
182
- expected_link = "#{ @base_url }/api/v1/people/#{ @steve.id }/posts?page%5Blimit%5D=12&page%5Boffset%5D=0"
183
- query = { page: { offset: 0, limit: 12 } }
184
-
185
- assert_equal expected_link,
186
- builder.relationships_related_link(source, relationship, query)
187
- end
188
-
189
- def test_query_link_for_regular_app
190
- config = {
191
- base_url: @base_url,
192
- route_formatter: @route_formatter,
193
- primary_resource_klass: Api::V1::PersonResource
194
- }
195
-
196
- query = { page: { offset: 0, limit: 12 } }
197
- builder = JSONAPI::LinkBuilder.new(config)
198
- expected_link = "#{ @base_url }/api/v1/people?page%5Blimit%5D=12&page%5Boffset%5D=0"
199
-
200
- assert_equal expected_link, builder.query_link(query)
201
- end
202
-
203
- def test_query_link_for_regular_app_with_camel_case_scope
204
- config = {
205
- base_url: @base_url,
206
- route_formatter: @route_formatter,
207
- primary_resource_klass: AdminApi::V1::PersonResource
208
- }
209
-
210
- query = { page: { offset: 0, limit: 12 } }
211
- builder = JSONAPI::LinkBuilder.new(config)
212
- expected_link = "#{ @base_url }/admin_api/v1/people?page%5Blimit%5D=12&page%5Boffset%5D=0"
213
-
214
- assert_equal expected_link, builder.query_link(query)
215
- end
216
-
217
- def test_query_link_for_engine
218
- config = {
219
- base_url: @base_url,
220
- route_formatter: @route_formatter,
221
- primary_resource_klass: MyEngine::Api::V1::PersonResource
222
- }
223
-
224
- query = { page: { offset: 0, limit: 12 } }
225
- builder = JSONAPI::LinkBuilder.new(config)
226
- expected_link = "#{ @base_url }/boomshaka/api/v1/people?page%5Blimit%5D=12&page%5Boffset%5D=0"
227
-
228
- assert_equal expected_link, builder.query_link(query)
229
- end
230
-
231
- def test_query_link_for_engine_with_camel_case_scope
232
- config = {
233
- base_url: @base_url,
234
- route_formatter: @route_formatter,
235
- primary_resource_klass: MyEngine::AdminApi::V1::PersonResource
236
- }
237
-
238
- query = { page: { offset: 0, limit: 12 } }
239
- builder = JSONAPI::LinkBuilder.new(config)
240
- expected_link = "#{ @base_url }/boomshaka/admin_api/v1/people?page%5Blimit%5D=12&page%5Boffset%5D=0"
241
-
242
- assert_equal expected_link, builder.query_link(query)
243
- end
244
- end
@@ -1,383 +0,0 @@
1
- require File.expand_path('../../../test_helper', __FILE__)
2
- require 'jsonapi-resources'
3
- require 'json'
4
-
5
- class PolymorphismTest < ActionDispatch::IntegrationTest
6
- def setup
7
- @pictures = Picture.all
8
- @person = Person.find(1)
9
-
10
- JSONAPI.configuration.json_key_format = :camelized_key
11
- JSONAPI.configuration.route_format = :camelized_route
12
- end
13
-
14
- def after_teardown
15
- JSONAPI.configuration.json_key_format = :underscored_key
16
- end
17
-
18
- def test_polymorphic_relationship
19
- relationships = PictureResource._relationships
20
- imageable = relationships[:imageable]
21
-
22
- assert_equal relationships.size, 1
23
- assert imageable.polymorphic?
24
- end
25
-
26
- def test_sti_polymorphic_to_many_serialization
27
- serialized_data = JSONAPI::ResourceSerializer.new(
28
- PersonResource,
29
- include: %w(vehicles)
30
- ).serialize_to_hash(PersonResource.new(@person, nil))
31
-
32
- assert_hash_equals(
33
- {
34
- data: {
35
- id: '1',
36
- type: 'people',
37
- links: {
38
- self: '/people/1'
39
- },
40
- attributes: {
41
- name: 'Joe Author',
42
- email: 'joe@xyz.fake',
43
- dateJoined: '2013-08-07 16:25:00 -0400'
44
- },
45
- relationships: {
46
- comments: {
47
- links: {
48
- self: '/people/1/relationships/comments',
49
- related: '/people/1/comments'
50
- }
51
- },
52
- posts: {
53
- links: {
54
- self: '/people/1/relationships/posts',
55
- related: '/people/1/posts'
56
- }
57
- },
58
- vehicles: {
59
- links: {
60
- self: '/people/1/relationships/vehicles',
61
- related: '/people/1/vehicles'
62
- },
63
- :data => [
64
- { type: 'cars', id: '1' },
65
- { type: 'boats', id: '2' }
66
- ]
67
- },
68
- preferences: {
69
- links: {
70
- self: '/people/1/relationships/preferences',
71
- related: '/people/1/preferences'
72
- }
73
- },
74
- hairCut: {
75
- links: {
76
- self: '/people/1/relationships/hairCut',
77
- related: '/people/1/hairCut'
78
- }
79
- }
80
- }
81
- },
82
- included: [
83
- {
84
- id: '1',
85
- type: 'cars',
86
- links: {
87
- self: '/cars/1'
88
- },
89
- attributes: {
90
- make: 'Mazda',
91
- model: 'Miata MX5',
92
- driveLayout: 'Front Engine RWD',
93
- serialNumber: '32432adfsfdysua'
94
- },
95
- relationships: {
96
- person: {
97
- links: {
98
- self: '/cars/1/relationships/person',
99
- related: '/cars/1/person'
100
- }
101
- }
102
- }
103
- },
104
- {
105
- id: '2',
106
- type: 'boats',
107
- links: {
108
- self: '/boats/2'
109
- },
110
- attributes: {
111
- make: 'Chris-Craft',
112
- model: 'Launch 20',
113
- lengthAtWaterLine: '15.5ft',
114
- serialNumber: '434253JJJSD'
115
- },
116
- relationships: {
117
- person: {
118
- links: {
119
- self: '/boats/2/relationships/person',
120
- related: '/boats/2/person'
121
- }
122
- }
123
- }
124
- }
125
- ]
126
- },
127
- serialized_data
128
- )
129
- end
130
-
131
- def test_polymorphic_to_one_serialization
132
- serialized_data = JSONAPI::ResourceSerializer.new(
133
- PictureResource,
134
- include: %w(imageable)
135
- ).serialize_to_hash(@pictures.map { |p| PictureResource.new p, nil })
136
-
137
- assert_hash_equals(
138
- {
139
- data: [
140
- {
141
- id: '1',
142
- type: 'pictures',
143
- links: {
144
- self: '/pictures/1'
145
- },
146
- attributes: {
147
- name: 'enterprise_gizmo.jpg'
148
- },
149
- relationships: {
150
- imageable: {
151
- links: {
152
- self: '/pictures/1/relationships/imageable',
153
- related: '/pictures/1/imageable'
154
- },
155
- data: {
156
- type: 'products',
157
- id: '1'
158
- }
159
- }
160
- }
161
- },
162
- {
163
- id: '2',
164
- type: 'pictures',
165
- links: {
166
- self: '/pictures/2'
167
- },
168
- attributes: {
169
- name: 'company_brochure.jpg'
170
- },
171
- relationships: {
172
- imageable: {
173
- links: {
174
- self: '/pictures/2/relationships/imageable',
175
- related: '/pictures/2/imageable'
176
- },
177
- data: {
178
- type: 'documents',
179
- id: '1'
180
- }
181
- }
182
- }
183
- },
184
- {
185
- id: '3',
186
- type: 'pictures',
187
- links: {
188
- self: '/pictures/3'
189
- },
190
- attributes: {
191
- name: 'group_photo.jpg'
192
- },
193
- relationships: {
194
- imageable: {
195
- links: {
196
- self: '/pictures/3/relationships/imageable',
197
- related: '/pictures/3/imageable'
198
- },
199
- data: nil
200
- }
201
- }
202
- }
203
-
204
- ],
205
- :included => [
206
- {
207
- id: '1',
208
- type: 'products',
209
- links: {
210
- self: '/products/1'
211
- },
212
- attributes: {
213
- name: 'Enterprise Gizmo'
214
- },
215
- relationships: {
216
- picture: {
217
- links: {
218
- self: '/products/1/relationships/picture',
219
- related: '/products/1/picture',
220
- },
221
- data: {
222
- type: 'pictures',
223
- id: '1'
224
- }
225
- }
226
- }
227
- },
228
- {
229
- id: '1',
230
- type: 'documents',
231
- links: {
232
- self: '/documents/1'
233
- },
234
- attributes: {
235
- name: 'Company Brochure'
236
- },
237
- relationships: {
238
- pictures: {
239
- links: {
240
- self: '/documents/1/relationships/pictures',
241
- related: '/documents/1/pictures'
242
- }
243
- }
244
- }
245
- }
246
- ]
247
- },
248
- serialized_data
249
- )
250
- end
251
-
252
- def test_polymorphic_get_related_resource
253
- get '/pictures/1/imageable'
254
- serialized_data = JSON.parse(response.body)
255
- assert_hash_equals(
256
- {
257
- data: {
258
- id: '1',
259
- type: 'products',
260
- links: {
261
- self: 'http://www.example.com/products/1'
262
- },
263
- attributes: {
264
- name: 'Enterprise Gizmo'
265
- },
266
- relationships: {
267
- picture: {
268
- links: {
269
- self: 'http://www.example.com/products/1/relationships/picture',
270
- related: 'http://www.example.com/products/1/picture'
271
- },
272
- data: {
273
- type: 'pictures',
274
- id: '1'
275
- }
276
- }
277
- }
278
- }
279
- },
280
- serialized_data
281
- )
282
- end
283
-
284
- def test_create_resource_with_polymorphic_relationship
285
- document = Document.find(1)
286
- post "/pictures/",
287
- {
288
- data: {
289
- type: "pictures",
290
- attributes: {
291
- name: "hello.jpg"
292
- },
293
- relationships: {
294
- imageable: {
295
- data: {
296
- type: "documents",
297
- id: document.id.to_s
298
- }
299
- }
300
- }
301
- }
302
- }.to_json,
303
- {
304
- 'Content-Type' => JSONAPI::MEDIA_TYPE
305
- }
306
- assert_equal response.status, 201
307
- picture = Picture.find(json_response["data"]["id"])
308
- assert_not_nil picture.imageable, "imageable should be present"
309
- ensure
310
- picture.destroy
311
- end
312
-
313
- def test_polymorphic_create_relationship
314
- picture = Picture.find(3)
315
- original_imageable = picture.imageable
316
- assert_nil original_imageable
317
-
318
- patch "/pictures/#{picture.id}/relationships/imageable",
319
- {
320
- relationship: 'imageable',
321
- data: {
322
- type: 'documents',
323
- id: '1'
324
- }
325
- }.to_json,
326
- {
327
- 'Content-Type' => JSONAPI::MEDIA_TYPE
328
- }
329
- assert_response :no_content
330
- picture = Picture.find(3)
331
- assert_equal 'Document', picture.imageable.class.to_s
332
-
333
- # restore data
334
- picture.imageable = original_imageable
335
- picture.save
336
- end
337
-
338
- def test_polymorphic_update_relationship
339
- picture = Picture.find(1)
340
- original_imageable = picture.imageable
341
- assert_not_equal 'Document', picture.imageable.class.to_s
342
-
343
- patch "/pictures/#{picture.id}/relationships/imageable",
344
- {
345
- relationship: 'imageable',
346
- data: {
347
- type: 'documents',
348
- id: '1'
349
- }
350
- }.to_json,
351
- {
352
- 'Content-Type' => JSONAPI::MEDIA_TYPE
353
- }
354
- assert_response :no_content
355
- picture = Picture.find(1)
356
- assert_equal 'Document', picture.imageable.class.to_s
357
-
358
- # restore data
359
- picture.imageable = original_imageable
360
- picture.save
361
- end
362
-
363
- def test_polymorphic_delete_relationship
364
- picture = Picture.find(1)
365
- original_imageable = picture.imageable
366
- assert original_imageable
367
-
368
- delete "/pictures/#{picture.id}/relationships/imageable",
369
- {
370
- relationship: 'imageable'
371
- }.to_json,
372
- {
373
- 'Content-Type' => JSONAPI::MEDIA_TYPE
374
- }
375
- assert_response :no_content
376
- picture = Picture.find(1)
377
- assert_nil picture.imageable
378
-
379
- # restore data
380
- picture.imageable = original_imageable
381
- picture.save
382
- end
383
- end