jsonapi-resources 0.5.9 → 0.6.0

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.
@@ -25,27 +25,33 @@ class CatResource < JSONAPI::Resource
25
25
  end
26
26
 
27
27
  class PersonWithCustomRecordsForResource < PersonResource
28
- def records_for(relationship_name, context)
28
+ def records_for(relationship_name)
29
29
  :records_for
30
30
  end
31
31
  end
32
32
 
33
33
  class PersonWithCustomRecordsForRelationshipsResource < PersonResource
34
- def records_for_posts(options = {})
34
+ def records_for_posts
35
35
  :records_for_posts
36
36
  end
37
- def record_for_preferences(options = {})
37
+
38
+ def record_for_preferences
38
39
  :record_for_preferences
39
40
  end
40
41
  end
41
42
 
42
43
  class PersonWithCustomRecordsForErrorResource < PersonResource
43
44
  class AuthorizationError < StandardError; end
44
- def records_for(relationship_name, context)
45
+ def records_for(relationship_name)
45
46
  raise AuthorizationError
46
47
  end
47
48
  end
48
49
 
50
+ module MyModule
51
+ class MyNamespacedResource < JSONAPI::Resource
52
+ end
53
+ end
54
+
49
55
  class ResourceTest < ActiveSupport::TestCase
50
56
  def setup
51
57
  @post = Post.first
@@ -59,6 +65,10 @@ class ResourceTest < ActiveSupport::TestCase
59
65
  assert_equal(PostResource._model_class, Post)
60
66
  end
61
67
 
68
+ def test_module_path
69
+ assert_equal(MyModule::MyNamespacedResource.module_path, 'my_module/')
70
+ end
71
+
62
72
  def test_base_resource_abstract
63
73
  assert BaseResource._abstract
64
74
  end
@@ -69,8 +79,11 @@ class ResourceTest < ActiveSupport::TestCase
69
79
  end
70
80
 
71
81
  def test_nil_model_class
72
- assert_output nil, "[MODEL NOT FOUND] Model could not be found for NoMatchResource. If this a base Resource declare it as abstract.\n" do
73
- assert_nil NoMatchResource._model_class
82
+ # ToDo:Figure out why this test does not work on Rails 4.0
83
+ if Rails::VERSION::MAJOR >= 4 && Rails::VERSION::MINOR >= 1
84
+ assert_output nil, "[MODEL NOT FOUND] Model could not be found for NoMatchResource. If this a base Resource declare it as abstract.\n" do
85
+ assert_nil NoMatchResource._model_class
86
+ end
74
87
  end
75
88
  end
76
89
 
@@ -109,13 +122,13 @@ class ResourceTest < ActiveSupport::TestCase
109
122
  preferences = Preferences.first
110
123
  refute(preferences == nil)
111
124
  author.update! preferences: preferences
112
- author_resource = PersonResource.new(author)
125
+ author_resource = PersonResource.new(author, nil)
113
126
  assert_equal(author_resource.preferences.model, preferences)
114
127
 
115
- author_resource = PersonWithCustomRecordsForResource.new(author)
128
+ author_resource = PersonWithCustomRecordsForResource.new(author, nil)
116
129
  assert_equal(author_resource.preferences.model, :records_for)
117
130
 
118
- author_resource = PersonWithCustomRecordsForErrorResource.new(author)
131
+ author_resource = PersonWithCustomRecordsForErrorResource.new(author, nil)
119
132
  assert_raises PersonWithCustomRecordsForErrorResource::AuthorizationError do
120
133
  author_resource.posts
121
134
  end
@@ -124,28 +137,28 @@ class ResourceTest < ActiveSupport::TestCase
124
137
  def test_records_for_meta_method_for_to_one
125
138
  author = Person.find(1)
126
139
  author.update! preferences: Preferences.first
127
- author_resource = PersonWithCustomRecordsForRelationshipsResource.new(author)
140
+ author_resource = PersonWithCustomRecordsForRelationshipsResource.new(author, nil)
128
141
  assert_equal(author_resource.record_for_preferences, :record_for_preferences)
129
142
  end
130
143
 
131
144
  def test_records_for_meta_method_for_to_one_calling_records_for
132
145
  author = Person.find(1)
133
146
  author.update! preferences: Preferences.first
134
- author_resource = PersonWithCustomRecordsForResource.new(author)
147
+ author_resource = PersonWithCustomRecordsForResource.new(author, nil)
135
148
  assert_equal(author_resource.record_for_preferences, :records_for)
136
149
  end
137
150
 
138
151
  def test_associated_records_meta_method_for_to_many
139
152
  author = Person.find(1)
140
153
  author.posts << Post.find(1)
141
- author_resource = PersonWithCustomRecordsForRelationshipsResource.new(author)
154
+ author_resource = PersonWithCustomRecordsForRelationshipsResource.new(author, nil)
142
155
  assert_equal(author_resource.records_for_posts, :records_for_posts)
143
156
  end
144
157
 
145
158
  def test_associated_records_meta_method_for_to_many_calling_records_for
146
159
  author = Person.find(1)
147
160
  author.posts << Post.find(1)
148
- author_resource = PersonWithCustomRecordsForResource.new(author)
161
+ author_resource = PersonWithCustomRecordsForResource.new(author, nil)
149
162
  assert_equal(author_resource.records_for_posts, :records_for)
150
163
  end
151
164
 
@@ -179,7 +192,7 @@ class ResourceTest < ActiveSupport::TestCase
179
192
  end
180
193
 
181
194
  def test_to_many_relationship_filters
182
- post_resource = PostResource.new(Post.find(1))
195
+ post_resource = PostResource.new(Post.find(1), nil)
183
196
  comments = post_resource.comments
184
197
  assert_equal(2, comments.size)
185
198
 
@@ -207,7 +220,7 @@ class ResourceTest < ActiveSupport::TestCase
207
220
  end
208
221
 
209
222
  def test_to_many_relationship_sorts
210
- post_resource = PostResource.new(Post.find(1))
223
+ post_resource = PostResource.new(Post.find(1), nil)
211
224
  comment_ids = post_resource.comments.map{|c| c.model.id }
212
225
  assert_equal [1,2], comment_ids
213
226
 
@@ -220,7 +233,7 @@ class ResourceTest < ActiveSupport::TestCase
220
233
  end
221
234
  end
222
235
 
223
- sorted_comment_ids = post_resource.comments(sort_criteria: [{ field: 'id', direction: 'desc'}]).map{|c| c.model.id }
236
+ sorted_comment_ids = post_resource.comments(sort_criteria: [{ field: 'id', direction: :desc}]).map{|c| c.model.id }
224
237
  assert_equal [2,1], sorted_comment_ids
225
238
 
226
239
  ensure
@@ -235,7 +248,7 @@ class ResourceTest < ActiveSupport::TestCase
235
248
  end
236
249
 
237
250
  def test_to_many_relationship_pagination
238
- post_resource = PostResource.new(Post.find(1))
251
+ post_resource = PostResource.new(Post.find(1), nil)
239
252
  comments = post_resource.comments
240
253
  assert_equal 2, comments.size
241
254
 
@@ -41,7 +41,7 @@ class LinkBuilderTest < ActionDispatch::IntegrationTest
41
41
  }
42
42
 
43
43
  builder = JSONAPI::LinkBuilder.new(config)
44
- source = primary_resource_klass.new(@steve)
44
+ source = primary_resource_klass.new(@steve, nil)
45
45
  expected_link = "#{ @base_url }/api/v1/people/#{ source.id }"
46
46
 
47
47
  assert_equal expected_link, builder.self_link(source)
@@ -57,7 +57,7 @@ class LinkBuilderTest < ActionDispatch::IntegrationTest
57
57
  }
58
58
 
59
59
  builder = JSONAPI::LinkBuilder.new(config)
60
- source = primary_resource_klass.new(@steve)
60
+ source = primary_resource_klass.new(@steve, nil)
61
61
  expected_link = "#{ @base_url }/boomshaka/api/v1/people/#{ source.id }"
62
62
 
63
63
  assert_equal expected_link, builder.self_link(source)
@@ -73,7 +73,7 @@ class LinkBuilderTest < ActionDispatch::IntegrationTest
73
73
  }
74
74
 
75
75
  builder = JSONAPI::LinkBuilder.new(config)
76
- source = primary_resource_klass.new(@steve)
76
+ source = primary_resource_klass.new(@steve, nil)
77
77
  expected_link = "#{ @base_url }/boomshaka/admin_api/v1/people/#{ source.id }"
78
78
 
79
79
  assert_equal expected_link, builder.self_link(source)
@@ -113,7 +113,7 @@ class LinkBuilderTest < ActionDispatch::IntegrationTest
113
113
  }
114
114
 
115
115
  builder = JSONAPI::LinkBuilder.new(config)
116
- source = Api::V1::PersonResource.new(@steve)
116
+ source = Api::V1::PersonResource.new(@steve, nil)
117
117
  relationship = JSONAPI::Relationship::ToMany.new("posts", {})
118
118
  expected_link = "#{ @base_url }/api/v1/people/#{ @steve.id }/relationships/posts"
119
119
 
@@ -129,7 +129,7 @@ class LinkBuilderTest < ActionDispatch::IntegrationTest
129
129
  }
130
130
 
131
131
  builder = JSONAPI::LinkBuilder.new(config)
132
- source = MyEngine::Api::V1::PersonResource.new(@steve)
132
+ source = MyEngine::Api::V1::PersonResource.new(@steve, nil)
133
133
  relationship = JSONAPI::Relationship::ToMany.new("posts", {})
134
134
  expected_link = "#{ @base_url }/boomshaka/api/v1/people/#{ @steve.id }/relationships/posts"
135
135
 
@@ -145,7 +145,7 @@ class LinkBuilderTest < ActionDispatch::IntegrationTest
145
145
  }
146
146
 
147
147
  builder = JSONAPI::LinkBuilder.new(config)
148
- source = Api::V1::PersonResource.new(@steve)
148
+ source = Api::V1::PersonResource.new(@steve, nil)
149
149
  relationship = JSONAPI::Relationship::ToMany.new("posts", {})
150
150
  expected_link = "#{ @base_url }/api/v1/people/#{ @steve.id }/posts"
151
151
 
@@ -161,7 +161,7 @@ class LinkBuilderTest < ActionDispatch::IntegrationTest
161
161
  }
162
162
 
163
163
  builder = JSONAPI::LinkBuilder.new(config)
164
- source = MyEngine::Api::V1::PersonResource.new(@steve)
164
+ source = MyEngine::Api::V1::PersonResource.new(@steve, nil)
165
165
  relationship = JSONAPI::Relationship::ToMany.new("posts", {})
166
166
  expected_link = "#{ @base_url }/boomshaka/api/v1/people/#{ @steve.id }/posts"
167
167
 
@@ -177,7 +177,7 @@ class LinkBuilderTest < ActionDispatch::IntegrationTest
177
177
  }
178
178
 
179
179
  builder = JSONAPI::LinkBuilder.new(config)
180
- source = Api::V1::PersonResource.new(@steve)
180
+ source = Api::V1::PersonResource.new(@steve, nil)
181
181
  relationship = JSONAPI::Relationship::ToMany.new("posts", {})
182
182
  expected_link = "#{ @base_url }/api/v1/people/#{ @steve.id }/posts?page%5Blimit%5D=12&page%5Boffset%5D=0"
183
183
  query = { page: { offset: 0, limit: 12 } }
@@ -27,7 +27,7 @@ class PolymorphismTest < ActionDispatch::IntegrationTest
27
27
  serialized_data = JSONAPI::ResourceSerializer.new(
28
28
  PersonResource,
29
29
  include: %w(vehicles)
30
- ).serialize_to_hash(PersonResource.new(@person))
30
+ ).serialize_to_hash(PersonResource.new(@person, nil))
31
31
 
32
32
  assert_hash_equals(
33
33
  {
@@ -132,7 +132,7 @@ class PolymorphismTest < ActionDispatch::IntegrationTest
132
132
  serialized_data = JSONAPI::ResourceSerializer.new(
133
133
  PictureResource,
134
134
  include: %w(imageable)
135
- ).serialize_to_hash(@pictures.map { |p| PictureResource.new p })
135
+ ).serialize_to_hash(@pictures.map { |p| PictureResource.new p, nil })
136
136
 
137
137
  assert_hash_equals(
138
138
  {
@@ -23,7 +23,7 @@ class SerializerTest < ActionDispatch::IntegrationTest
23
23
 
24
24
  serialized = JSONAPI::ResourceSerializer.new(
25
25
  PostResource,
26
- base_url: 'http://example.com').serialize_to_hash(PostResource.new(@post)
26
+ base_url: 'http://example.com').serialize_to_hash(PostResource.new(@post, nil)
27
27
  )
28
28
 
29
29
  assert_hash_equals(
@@ -118,7 +118,7 @@ class SerializerTest < ActionDispatch::IntegrationTest
118
118
  },
119
119
  JSONAPI::ResourceSerializer.new(Api::V1::PostResource,
120
120
  base_url: 'http://example.com').serialize_to_hash(
121
- Api::V1::PostResource.new(@post))
121
+ Api::V1::PostResource.new(@post, nil))
122
122
  )
123
123
  end
124
124
 
@@ -146,7 +146,7 @@ class SerializerTest < ActionDispatch::IntegrationTest
146
146
  }
147
147
  },
148
148
  JSONAPI::ResourceSerializer.new(PostResource,
149
- fields: {posts: [:id, :title, :author]}).serialize_to_hash(PostResource.new(@post))
149
+ fields: {posts: [:id, :title, :author]}).serialize_to_hash(PostResource.new(@post, nil))
150
150
  )
151
151
  end
152
152
 
@@ -154,7 +154,7 @@ class SerializerTest < ActionDispatch::IntegrationTest
154
154
  serialized = JSONAPI::ResourceSerializer.new(
155
155
  PostResource,
156
156
  include: ['author']
157
- ).serialize_to_hash(PostResource.new(@post))
157
+ ).serialize_to_hash(PostResource.new(@post, nil))
158
158
 
159
159
  assert_hash_equals(
160
160
  {
@@ -256,7 +256,7 @@ class SerializerTest < ActionDispatch::IntegrationTest
256
256
  PostResource,
257
257
  include: ['author'],
258
258
  key_formatter: UnderscoredKeyFormatter
259
- ).serialize_to_hash(PostResource.new(@post))
259
+ ).serialize_to_hash(PostResource.new(@post, nil))
260
260
 
261
261
  assert_hash_equals(
262
262
  {
@@ -525,7 +525,7 @@ class SerializerTest < ActionDispatch::IntegrationTest
525
525
  ]
526
526
  },
527
527
  JSONAPI::ResourceSerializer.new(PostResource,
528
- include: ['comments', 'comments.tags']).serialize_to_hash(PostResource.new(@post))
528
+ include: ['comments', 'comments.tags']).serialize_to_hash(PostResource.new(@post, nil))
529
529
  )
530
530
  end
531
531
 
@@ -533,7 +533,7 @@ class SerializerTest < ActionDispatch::IntegrationTest
533
533
  serialized = JSONAPI::ResourceSerializer.new(
534
534
  PersonResource,
535
535
  include: ['comments']
536
- ).serialize_to_hash(PersonResource.new(@fred))
536
+ ).serialize_to_hash(PersonResource.new(@fred, nil))
537
537
 
538
538
  assert_hash_equals(
539
539
  {
@@ -656,7 +656,7 @@ class SerializerTest < ActionDispatch::IntegrationTest
656
656
 
657
657
  posts = []
658
658
  Post.find(1, 2).each do |post|
659
- posts.push PostResource.new(post)
659
+ posts.push PostResource.new(post, nil)
660
660
  end
661
661
 
662
662
  JSONAPI.configuration.always_include_to_one_linkage_data = true
@@ -972,7 +972,7 @@ class SerializerTest < ActionDispatch::IntegrationTest
972
972
 
973
973
  posts = []
974
974
  Post.find(1, 2).each do |post|
975
- posts.push PostResource.new(post)
975
+ posts.push PostResource.new(post, nil)
976
976
  end
977
977
 
978
978
  assert_hash_equals(
@@ -1247,7 +1247,7 @@ class SerializerTest < ActionDispatch::IntegrationTest
1247
1247
 
1248
1248
  posts = []
1249
1249
  Post.find(1, 2).each do |post|
1250
- posts.push PostResource.new(post)
1250
+ posts.push PostResource.new(post, nil)
1251
1251
  end
1252
1252
 
1253
1253
  assert_hash_equals(
@@ -1476,13 +1476,13 @@ class SerializerTest < ActionDispatch::IntegrationTest
1476
1476
  JSONAPI::ResourceSerializer.new(ExpenseEntryResource,
1477
1477
  include: ['iso_currency', 'employee'],
1478
1478
  fields: {people: [:id, :name, :email, :date_joined]}).serialize_to_hash(
1479
- ExpenseEntryResource.new(@expense_entry))
1479
+ ExpenseEntryResource.new(@expense_entry, nil))
1480
1480
  )
1481
1481
  end
1482
1482
 
1483
1483
  def test_serializer_empty_links_null_and_array
1484
1484
  planet_hash = JSONAPI::ResourceSerializer.new(PlanetResource).serialize_to_hash(
1485
- PlanetResource.new(Planet.find(8)))
1485
+ PlanetResource.new(Planet.find(8), nil))
1486
1486
 
1487
1487
  assert_hash_equals(
1488
1488
  {
@@ -1523,7 +1523,7 @@ class SerializerTest < ActionDispatch::IntegrationTest
1523
1523
  def test_serializer_include_with_empty_links_null_and_array
1524
1524
  planets = []
1525
1525
  Planet.find(7, 8).each do |planet|
1526
- planets.push PlanetResource.new(planet)
1526
+ planets.push PlanetResource.new(planet, nil)
1527
1527
  end
1528
1528
 
1529
1529
  planet_hash = JSONAPI::ResourceSerializer.new(PlanetResource,
@@ -1619,7 +1619,7 @@ class SerializerTest < ActionDispatch::IntegrationTest
1619
1619
  original_config = JSONAPI.configuration.dup
1620
1620
  JSONAPI.configuration.json_key_format = :underscored_key
1621
1621
 
1622
- preferences = PreferencesResource.new(Preferences.find(1))
1622
+ preferences = PreferencesResource.new(Preferences.find(1), nil)
1623
1623
 
1624
1624
  assert_hash_equals(
1625
1625
  {
@@ -1658,7 +1658,7 @@ class SerializerTest < ActionDispatch::IntegrationTest
1658
1658
  original_config = JSONAPI.configuration.dup
1659
1659
  JSONAPI.configuration.json_key_format = :underscored_key
1660
1660
 
1661
- facts = FactResource.new(Fact.find(1))
1661
+ facts = FactResource.new(Fact.find(1), nil)
1662
1662
 
1663
1663
  assert_hash_equals(
1664
1664
  {
@@ -1691,7 +1691,7 @@ class SerializerTest < ActionDispatch::IntegrationTest
1691
1691
  serialized = JSONAPI::ResourceSerializer.new(
1692
1692
  Api::V5::AuthorResource,
1693
1693
  include: ['author_detail']
1694
- ).serialize_to_hash(Api::V5::AuthorResource.new(Person.find(1)))
1694
+ ).serialize_to_hash(Api::V5::AuthorResource.new(Person.find(1), nil))
1695
1695
 
1696
1696
  assert_hash_equals(
1697
1697
  {
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jsonapi-resources
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.9
4
+ version: 0.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dan Gebhardt
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2015-09-03 00:00:00.000000000 Z
12
+ date: 2015-10-09 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler
@@ -67,20 +67,6 @@ dependencies:
67
67
  - - ">="
68
68
  - !ruby/object:Gem::Version
69
69
  version: '0'
70
- - !ruby/object:Gem::Dependency
71
- name: minitest-reporters
72
- requirement: !ruby/object:Gem::Requirement
73
- requirements:
74
- - - ">="
75
- - !ruby/object:Gem::Version
76
- version: '0'
77
- type: :development
78
- prerelease: false
79
- version_requirements: !ruby/object:Gem::Requirement
80
- requirements:
81
- - - ">="
82
- - !ruby/object:Gem::Version
83
- version: '0'
84
70
  - !ruby/object:Gem::Dependency
85
71
  name: simplecov
86
72
  requirement: !ruby/object:Gem::Requirement
@@ -176,6 +162,7 @@ files:
176
162
  - test/fixtures/categories.yml
177
163
  - test/fixtures/comments.yml
178
164
  - test/fixtures/comments_tags.yml
165
+ - test/fixtures/companies.yml
179
166
  - test/fixtures/craters.yml
180
167
  - test/fixtures/customers.yml
181
168
  - test/fixtures/documents.yml
@@ -205,6 +192,7 @@ files:
205
192
  - test/helpers/value_matchers_test.rb
206
193
  - test/integration/requests/request_test.rb
207
194
  - test/integration/routes/routes_test.rb
195
+ - test/integration/sti_fields_test.rb
208
196
  - test/lib/generators/jsonapi/resource_generator_test.rb
209
197
  - test/test_helper.rb
210
198
  - test/unit/jsonapi_request/jsonapi_request_test.rb
@@ -237,7 +225,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
237
225
  version: '0'
238
226
  requirements: []
239
227
  rubyforge_project:
240
- rubygems_version: 2.4.5
228
+ rubygems_version: 2.4.6
241
229
  signing_key:
242
230
  specification_version: 4
243
231
  summary: Easily support JSON API in Rails.
@@ -251,6 +239,7 @@ test_files:
251
239
  - test/fixtures/categories.yml
252
240
  - test/fixtures/comments.yml
253
241
  - test/fixtures/comments_tags.yml
242
+ - test/fixtures/companies.yml
254
243
  - test/fixtures/craters.yml
255
244
  - test/fixtures/customers.yml
256
245
  - test/fixtures/documents.yml
@@ -280,6 +269,7 @@ test_files:
280
269
  - test/helpers/value_matchers_test.rb
281
270
  - test/integration/requests/request_test.rb
282
271
  - test/integration/routes/routes_test.rb
272
+ - test/integration/sti_fields_test.rb
283
273
  - test/lib/generators/jsonapi/resource_generator_test.rb
284
274
  - test/test_helper.rb
285
275
  - test/unit/jsonapi_request/jsonapi_request_test.rb
@@ -292,4 +282,3 @@ test_files:
292
282
  - test/unit/serializer/polymorphic_serializer_test.rb
293
283
  - test/unit/serializer/response_document_test.rb
294
284
  - test/unit/serializer/serializer_test.rb
295
- has_rdoc: