jsonapi_compliable 0.11.0 → 0.11.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: cdce2b040460bc3c66f0aa2a869e5be239cef593
4
- data.tar.gz: 37e97590487dbd9eca8edc3157174f4826e79bba
3
+ metadata.gz: ff9f3051203426cebee5e62694cf097f0aba942d
4
+ data.tar.gz: f427616b68314981ec7dd9d46a5941042e234637
5
5
  SHA512:
6
- metadata.gz: 0462e8f0e8745dcefafce80dd8c605254b7b28731a14d6d0c5fdd4cb2cae0575c22dc83d199b00269bbb78d77a2a22d6744b515bf05d4ad24fa0e845529d33e4
7
- data.tar.gz: 6cbacdb485370394b01d8dd80a031b254e0712e182c833671a04c39d62037e2b61e79f9fd084b57e0ebd90cc3b279eb282ae42c0fea5e99f3dc93b817d1c9e7e
6
+ metadata.gz: 2ec6bf442ea94a3213137860adf61e12aff5a29897a1eebee53850c1f69a927ba636c8ad98bf4523adcc976182777abdc314ff75af165e80d7e3d65110b2cca3
7
+ data.tar.gz: 0ade2b736330f5267b58d97c10ad669ec285d25fffc705436d3398b5869ea2e7d07eabcb81b0767109a8898fdc24a6e23ccf6d2ea7a4280963d02e8a246a2b09
@@ -3,15 +3,15 @@
3
3
  source "https://rubygems.org"
4
4
 
5
5
  gem "rails", "~> 4.1"
6
- gem "jsonapi-rails", "~> 0.3.1", require: "jsonapi/rails"
6
+ gem "jsonapi-rails", "~> 0.3.1", :require => "jsonapi/rails"
7
7
  gem "rspec-rails"
8
8
 
9
9
  group :test do
10
10
  gem "pry"
11
- gem "pry-byebug", platform: [:mri]
11
+ gem "pry-byebug", :platform => [:mri]
12
12
  gem "appraisal"
13
13
  gem "guard"
14
14
  gem "guard-rspec"
15
15
  end
16
16
 
17
- gemspec path: "../"
17
+ gemspec :path => "../"
@@ -3,15 +3,15 @@
3
3
  source "https://rubygems.org"
4
4
 
5
5
  gem "rails", "~> 5.0"
6
- gem "jsonapi-rails", "~> 0.3.1", require: "jsonapi/rails"
6
+ gem "jsonapi-rails", "~> 0.3.1", :require => "jsonapi/rails"
7
7
  gem "rspec-rails"
8
8
 
9
9
  group :test do
10
10
  gem "pry"
11
- gem "pry-byebug", platform: [:mri]
11
+ gem "pry-byebug", :platform => [:mri]
12
12
  gem "appraisal"
13
13
  gem "guard"
14
14
  gem "guard-rspec"
15
15
  end
16
16
 
17
- gemspec path: "../"
17
+ gemspec :path => "../"
@@ -39,7 +39,9 @@ class <%= model_klass.name.pluralize %>Controller < ApplicationController
39
39
  <%- end -%>
40
40
  def show
41
41
  scope = jsonapi_scope(<%= model_klass %>.where(id: params[:id]))
42
- render_jsonapi(scope.resolve.first, scope: false)
42
+ instance = scope.resolve.first
43
+ raise JsonapiCompliable::Errors::RecordNotFound unless instance
44
+ render_jsonapi(instance, scope: false)
43
45
  end
44
46
 
45
47
  <%- end -%>
@@ -79,13 +81,17 @@ class <%= model_klass.name.pluralize %>Controller < ApplicationController
79
81
  <%- end -%>
80
82
  <%- if actions?('destroy') -%>
81
83
  <%- unless omit_comments? -%>
82
- # No need for any special logic here as no_content is jsonapi_compliant.
83
- # Customize this if you have a more complex use case.
84
+ # Renders 200 OK with empty meta
85
+ # http://jsonapi.org/format/#crud-deleting-responses-200
84
86
  <%- end -%>
85
87
  def destroy
86
- <%= file_name %> = <%= model_klass %>.find(params[:id])
87
- <%= file_name %>.destroy
88
- return head(:no_content)
88
+ <%= file_name %>, success = jsonapi_destroy.to_a
89
+
90
+ if success
91
+ render json: { meta: {} }
92
+ else
93
+ render_errors_for(<%= file_name %>)
94
+ end
89
95
  end
90
96
  <%- end -%>
91
97
  end
@@ -1,7 +1,11 @@
1
1
  require 'rails_helper'
2
2
 
3
3
  RSpec.describe "<%= type %>#create", type: :request do
4
- context 'basic create' do
4
+ subject(:make_request) do
5
+ jsonapi_post "/<%= api_namespace %>/v1/<%= type %>", payload
6
+ end
7
+
8
+ describe 'basic create' do
5
9
  let(:payload) do
6
10
  {
7
11
  data: {
@@ -15,7 +19,7 @@ RSpec.describe "<%= type %>#create", type: :request do
15
19
 
16
20
  it 'creates the resource' do
17
21
  expect {
18
- jsonapi_post "/<%= api_namespace %>/v1/<%= type %>", payload
22
+ make_request
19
23
  }.to change { <%= model_klass %>.count }.by(1)
20
24
  <%= file_name %> = <%= model_klass %>.last
21
25
 
@@ -1,15 +1,20 @@
1
1
  require 'rails_helper'
2
2
 
3
3
  RSpec.describe "<%= type %>#destroy", type: :request do
4
- context 'basic destroy' do
4
+ subject(:make_request) do
5
+ jsonapi_delete "/<%= api_namespace %>/v1/<%= type %>/#{<%= file_name %>.id}"
6
+ end
7
+
8
+ describe 'basic destroy' do
5
9
  let!(:<%= file_name %>) { create(:<%= file_name %>) }
6
10
 
7
11
  it 'updates the resource' do
8
12
  expect {
9
- delete "/<%= api_namespace %>/v1/<%= type %>/#{<%= file_name %>.id}"
13
+ make_request
10
14
  }.to change { <%= model_klass %>.count }.by(-1)
11
15
 
12
- expect(response.status).to eq(204)
16
+ expect(response.status).to eq(200)
17
+ expect(json).to eq('meta' => {})
13
18
  end
14
19
  end
15
20
  end
@@ -1,13 +1,19 @@
1
1
  require 'rails_helper'
2
2
 
3
3
  RSpec.describe "<%= file_name.pluralize %>#index", type: :request do
4
- context 'basic fetch' do
4
+ let(:params) { {} }
5
+
6
+ subject(:make_request) do
7
+ jsonapi_get "/<%= api_namespace %>/v1/<%= file_name.pluralize %>",
8
+ params: params
9
+ end
10
+
11
+ describe 'basic fetch' do
5
12
  let!(:<%= file_name %>1) { create(:<%= file_name %>) }
6
13
  let!(:<%= file_name %>2) { create(:<%= file_name %>) }
7
14
 
8
15
  it 'serializes the list correctly' do
9
- jsonapi_get "/<%= api_namespace %>/v1/<%= file_name.pluralize %>"
10
-
16
+ make_request
11
17
  expect(json_ids(true)).to match_array([<%= file_name %>1.id, <%= file_name %>2.id])
12
18
  assert_payload(:<%= file_name %>, <%= file_name %>1, json_items[0])
13
19
  assert_payload(:<%= file_name %>, <%= file_name %>2, json_items[1])
@@ -1,12 +1,18 @@
1
1
  require 'rails_helper'
2
2
 
3
3
  RSpec.describe "<%= file_name.pluralize %>#show", type: :request do
4
- context 'basic fetch' do
4
+ let(:params) { {} }
5
+
6
+ subject(:make_request) do
7
+ jsonapi_get "/<%= api_namespace %>/v1/<%= file_name.pluralize %>/#{<%= file_name %>.id}",
8
+ params: params
9
+ end
10
+
11
+ describe 'basic fetch' do
5
12
  let!(:<%= file_name %>) { create(:<%= file_name %>) }
6
13
 
7
14
  it 'serializes the resource correctly' do
8
- jsonapi_get "/<%= api_namespace %>/v1/<%= file_name.pluralize %>/#{<%= file_name %>.id}"
9
-
15
+ make_request
10
16
  assert_payload(:<%= file_name %>, <%= file_name %>, json_item)
11
17
  end
12
18
  end
@@ -1,7 +1,11 @@
1
1
  require 'rails_helper'
2
2
 
3
3
  RSpec.describe "<%= type %>#update", type: :request do
4
- context 'basic update' do
4
+ subject(:make_request) do
5
+ jsonapi_put "/<%= api_namespace %>/v1/<%= type %>/#{<%= file_name %>.id}", payload
6
+ end
7
+
8
+ describe 'basic update' do
5
9
  let!(:<%= file_name %>) { create(:<%= file_name %>) }
6
10
 
7
11
  let(:payload) do
@@ -19,7 +23,7 @@ RSpec.describe "<%= type %>#update", type: :request do
19
23
  # Replace 'xit' with 'it' after adding attributes
20
24
  xit 'updates the resource' do
21
25
  expect {
22
- jsonapi_put "/<%= api_namespace %>/v1/<%= type %>/#{<%= file_name %>.id}", payload
26
+ make_request
23
27
  }.to change { <%= file_name %>.reload.attributes }
24
28
  assert_payload(:<%= file_name %>, <%= file_name %>, json_item)
25
29
 
@@ -226,6 +226,12 @@ module JsonapiCompliable
226
226
  end
227
227
  end
228
228
 
229
+ def jsonapi_destroy
230
+ _persist do
231
+ jsonapi_resource.destroy(params[:id])
232
+ end
233
+ end
234
+
229
235
  # Similar to +render :json+ or +render :jsonapi+
230
236
  #
231
237
  # By default, this will "build" the scope via +#jsonapi_scope+. To avoid
@@ -207,10 +207,18 @@ class JsonapiCompliable::Deserializer
207
207
  end
208
208
 
209
209
  def raw_attributes
210
- data[:attributes] || {}
210
+ if data
211
+ data[:attributes] || {}
212
+ else
213
+ {}
214
+ end
211
215
  end
212
216
 
213
217
  def raw_relationships
214
- data[:relationships] || {}
218
+ if data
219
+ data[:relationships] || {}
220
+ else
221
+ {}
222
+ end
215
223
  end
216
224
  end
@@ -44,5 +44,8 @@ module JsonapiCompliable
44
44
  end
45
45
  end
46
46
  end
47
+
48
+ class RecordNotFound < StandardError
49
+ end
47
50
  end
48
51
  end
@@ -1,3 +1,3 @@
1
1
  module JsonapiCompliable
2
- VERSION = "0.11.0"
2
+ VERSION = "0.11.1"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jsonapi_compliable
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.11.0
4
+ version: 0.11.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Lee Richmond