jsonapi-resources 0.3.0 → 0.3.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 7f8dd7b98dfa7f20a9b75456fd8ca066ae807fd0
4
- data.tar.gz: 4aaa07053a74089ef0880f4154e79261a97529d9
3
+ metadata.gz: c58427d88b5c0bbe33915d8039ac02a1f6ea260b
4
+ data.tar.gz: 62dcacd2190abc5bffe3a10cefe1bba9d50a56b7
5
5
  SHA512:
6
- metadata.gz: 06358dd197a8efa202d8c3b82493be75c5bb65d22338dd78bf56b3475093879ee93703ea99c450d377962ec0dcfe4ff9165c52ce0cac2157be59b45722b121f0
7
- data.tar.gz: 077d88d176ed150fe694fc105f57f0092526139b628ab9c7bb3b38d37c6e8b14ce367ccee4ccac4cbbcf8da57f6e606b658e65c8fabec7b5b629f534790a4bae
6
+ metadata.gz: 5ae8e6c9e4c87a8b5848110cd2acd98f98b6a1ab202edd83b62aabd9806df7880d4345a1678b551ed054706aaae14227261f07f9dce77161e3dbf260dcf1236c
7
+ data.tar.gz: 1d528f86653c12a040dbb52b1db0d0f8affbf7b11440ceb0a75e3eae17d70a219872cbbb34d7fcb7ad3d04ccd366921ba146f00023a7b0910a4e74ae5f40c86a
data/README.md CHANGED
@@ -4,7 +4,7 @@
4
4
 
5
5
  Like JSON API itself, JR's design is focused on the resources served by an API. JR needs little more than a definition of your resources, including their attributes and relationships, to make your server compliant with JSON API.
6
6
 
7
- JR is designed to work with Rails, and provides custom routes, controllers, and serializers. JR's resources may be backed by ActiveRecord models or by custom objects.
7
+ JR is designed to work with Rails 4.0+, and provides custom routes, controllers, and serializers. JR's resources may be backed by ActiveRecord models or by custom objects.
8
8
 
9
9
  ## Demo App
10
10
 
@@ -25,5 +25,5 @@ Gem::Specification.new do |spec|
25
25
  spec.add_development_dependency 'minitest-spec-rails'
26
26
  spec.add_development_dependency 'minitest-reporters'
27
27
  spec.add_development_dependency 'simplecov'
28
- spec.add_development_dependency 'rails', '>= 4.0'
28
+ spec.add_dependency 'rails', '>= 4.0'
29
29
  end
@@ -198,16 +198,9 @@ module JSONAPI
198
198
  end
199
199
 
200
200
  def parse_add_operation(data)
201
- if data.is_a?(Array)
202
- data.each do |p|
203
- @operations.push JSONAPI::CreateResourceOperation.new(@resource_klass,
204
- parse_params(verify_and_remove_type(p),
205
- @resource_klass.createable_fields(@context)))
206
- end
207
- else
208
- @operations.push JSONAPI::CreateResourceOperation.new(@resource_klass,
209
- parse_params(verify_and_remove_type(data),
210
- @resource_klass.createable_fields(@context)))
201
+ Array.wrap(data).each do |p|
202
+ values = parse_params(verify_and_remove_type(p), @resource_klass.createable_fields(@context))
203
+ @operations.push JSONAPI::CreateResourceOperation.new(@resource_klass, values)
211
204
  end
212
205
  rescue JSONAPI::Exceptions::Error => e
213
206
  @errors.concat(e.errors)
@@ -46,7 +46,7 @@ module JSONAPI
46
46
  @changing = true
47
47
  run_callbacks callback do
48
48
  yield
49
- save if @save_needed
49
+ save if @save_needed || is_new?
50
50
  end
51
51
  end
52
52
  end
@@ -110,6 +110,8 @@ module JSONAPI
110
110
  add_included_object(@primary_class_name, id, object_hash(resource, requested_associations), true)
111
111
  end
112
112
  else
113
+ return {} if source.nil?
114
+
113
115
  resource = source
114
116
  id = resource.id
115
117
  # ToDo: See if this is actually needed
@@ -1,5 +1,5 @@
1
1
  module JSONAPI
2
2
  module Resources
3
- VERSION = "0.3.0"
3
+ VERSION = "0.3.1"
4
4
  end
5
5
  end
@@ -1335,6 +1335,19 @@ class PostsControllerTest < ActionController::TestCase
1335
1335
  assert_response :bad_request
1336
1336
  assert_match /2,1 is not a valid value for id/, response.body
1337
1337
  end
1338
+
1339
+ def test_show_has_one_relationship_nil
1340
+ get :show_association, {post_id: '17', association: 'author'}
1341
+ assert_response :success
1342
+ assert_hash_equals json_response,
1343
+ {
1344
+ data: nil,
1345
+ links: {
1346
+ self: 'http://test.host/posts/17/links/author',
1347
+ related: 'http://test.host/posts/17/author'
1348
+ }
1349
+ }
1350
+ end
1338
1351
  end
1339
1352
 
1340
1353
  class TagsControllerTest < ActionController::TestCase
@@ -1678,6 +1691,50 @@ class PeopleControllerTest < ActionController::TestCase
1678
1691
  assert_equal json_response['data'][0]['id'], '1'
1679
1692
  assert_equal json_response['data'][0]['name'], 'Joe Author'
1680
1693
  end
1694
+
1695
+ def test_get_related_resource
1696
+ get :get_related_resource, {post_id: '2', association: 'author', :source=>'posts'}
1697
+ assert_response :success
1698
+ assert_hash_equals json_response,
1699
+ {
1700
+ data: {
1701
+ id: '1',
1702
+ type: 'people',
1703
+ name: 'Joe Author',
1704
+ email: 'joe@xyz.fake',
1705
+ dateJoined: '2013-08-07 16:25:00 -0400',
1706
+ links: {
1707
+ self: 'http://test.host/people/1',
1708
+ comments: {
1709
+ self: 'http://test.host/people/1/links/comments',
1710
+ related: 'http://test.host/people/1/comments'
1711
+ },
1712
+ posts: {
1713
+ self: 'http://test.host/people/1/links/posts',
1714
+ related: 'http://test.host/people/1/posts'
1715
+ },
1716
+ preferences: {
1717
+ self: 'http://test.host/people/1/links/preferences',
1718
+ related: 'http://test.host/people/1/preferences',
1719
+ linkage: {
1720
+ type: 'preferences',
1721
+ id: '1'
1722
+ }
1723
+ }
1724
+ }
1725
+ }
1726
+ }
1727
+ end
1728
+
1729
+ def test_get_related_resource_nil
1730
+ get :get_related_resource, {post_id: '17', association: 'author', :source=>'posts'}
1731
+ assert_response :success
1732
+ assert_hash_equals json_response,
1733
+ {
1734
+ data: nil
1735
+ }
1736
+
1737
+ end
1681
1738
  end
1682
1739
 
1683
1740
  class Api::V5::AuthorsControllerTest < ActionController::TestCase
@@ -93,4 +93,10 @@ post_16:
93
93
  id: 16
94
94
  title: SDFGH
95
95
  body: Not First!!!!
96
- author_id: 3
96
+ author_id: 3
97
+
98
+ post_17:
99
+ id: 17
100
+ title: No Author!!!!!!
101
+ body: This post has no Author
102
+ author_id:
@@ -156,6 +156,41 @@ class RequestTest < ActionDispatch::IntegrationTest
156
156
  assert_equal 201, status
157
157
  end
158
158
 
159
+ def test_post_single_missing_data_contents
160
+ post '/posts',
161
+ {
162
+ 'data' => {
163
+ }
164
+ }.to_json, "CONTENT_TYPE" => JSONAPI::MEDIA_TYPE
165
+
166
+ assert_equal 400, status
167
+ end
168
+
169
+ def test_post_single_minimal_valid
170
+ post '/comments',
171
+ {
172
+ 'data' => {
173
+ 'type' => 'comments'
174
+ }
175
+ }.to_json, "CONTENT_TYPE" => JSONAPI::MEDIA_TYPE
176
+
177
+ assert_equal 201, status
178
+ assert_nil json_response['data']['body']
179
+ assert_nil json_response['data']['links']['post']['linkage']
180
+ assert_nil json_response['data']['links']['author']['linkage']
181
+ end
182
+
183
+ def test_post_single_minimal_invalid
184
+ post '/posts',
185
+ {
186
+ 'data' => {
187
+ 'type' => 'posts'
188
+ }
189
+ }.to_json, "CONTENT_TYPE" => JSONAPI::MEDIA_TYPE
190
+
191
+ assert_equal 422, status
192
+ end
193
+
159
194
  def test_update_association_without_content_type
160
195
  ruby = Section.find_by(name: 'ruby')
161
196
  patch '/posts/3/links/section', { 'data' => {type: 'sections', id: ruby.id.to_s }}.to_json
@@ -189,7 +224,7 @@ class RequestTest < ActionDispatch::IntegrationTest
189
224
  def test_post_update_association_has_many
190
225
  rogue = Comment.find_by(body: 'Rogue Comment Here')
191
226
  post '/posts/5/links/comments', { 'data' => [{type: 'comments', id: rogue.id.to_s }]}.to_json, "CONTENT_TYPE" => JSONAPI::MEDIA_TYPE
192
-
227
+
193
228
  assert_equal 204, status
194
229
  end
195
230
 
@@ -27,6 +27,7 @@ end
27
27
  puts "Testing With RAILS VERSION #{Rails.version}"
28
28
 
29
29
  class TestApp < Rails::Application
30
+ config.eager_load = false
30
31
  config.root = File.dirname(__FILE__)
31
32
  config.session_store :cookie_store, key: 'session'
32
33
  config.secret_key_base = 'secret'
@@ -57,6 +57,15 @@ class SerializerTest < ActionDispatch::IntegrationTest
57
57
  )
58
58
  end
59
59
 
60
+ def test_serializer_nil_handling
61
+ assert_hash_equals(
62
+ {
63
+ data: nil
64
+ },
65
+ JSONAPI::ResourceSerializer.new(PostResource).serialize_to_hash(nil)
66
+ )
67
+ end
68
+
60
69
  def test_serializer_namespaced_resource
61
70
  assert_hash_equals(
62
71
  {
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.3.0
4
+ version: 0.3.1
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-04-07 00:00:00.000000000 Z
12
+ date: 2015-04-08 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler
@@ -102,7 +102,7 @@ dependencies:
102
102
  - - ">="
103
103
  - !ruby/object:Gem::Version
104
104
  version: '4.0'
105
- type: :development
105
+ type: :runtime
106
106
  prerelease: false
107
107
  version_requirements: !ruby/object:Gem::Requirement
108
108
  requirements: