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 +4 -4
- data/gemfiles/rails_4.gemfile +3 -3
- data/gemfiles/rails_5.gemfile +3 -3
- data/lib/generators/jsonapi/templates/controller.rb.erb +12 -6
- data/lib/generators/jsonapi/templates/create_request_spec.rb.erb +6 -2
- data/lib/generators/jsonapi/templates/destroy_request_spec.rb.erb +8 -3
- data/lib/generators/jsonapi/templates/index_request_spec.rb.erb +9 -3
- data/lib/generators/jsonapi/templates/show_request_spec.rb.erb +9 -3
- data/lib/generators/jsonapi/templates/update_request_spec.rb.erb +6 -2
- data/lib/jsonapi_compliable/base.rb +6 -0
- data/lib/jsonapi_compliable/deserializer.rb +10 -2
- data/lib/jsonapi_compliable/errors.rb +3 -0
- data/lib/jsonapi_compliable/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ff9f3051203426cebee5e62694cf097f0aba942d
|
4
|
+
data.tar.gz: f427616b68314981ec7dd9d46a5941042e234637
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2ec6bf442ea94a3213137860adf61e12aff5a29897a1eebee53850c1f69a927ba636c8ad98bf4523adcc976182777abdc314ff75af165e80d7e3d65110b2cca3
|
7
|
+
data.tar.gz: 0ade2b736330f5267b58d97c10ad669ec285d25fffc705436d3398b5869ea2e7d07eabcb81b0767109a8898fdc24a6e23ccf6d2ea7a4280963d02e8a246a2b09
|
data/gemfiles/rails_4.gemfile
CHANGED
@@ -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
|
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
|
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 => "../"
|
data/gemfiles/rails_5.gemfile
CHANGED
@@ -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
|
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
|
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
|
-
|
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
|
-
#
|
83
|
-
#
|
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
|
87
|
-
|
88
|
-
|
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
|
-
|
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
|
-
|
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
|
-
|
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
|
-
|
13
|
+
make_request
|
10
14
|
}.to change { <%= model_klass %>.count }.by(-1)
|
11
15
|
|
12
|
-
expect(response.status).to eq(
|
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
|
-
|
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
|
-
|
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
|
-
|
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
|
-
|
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
|
-
|
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
|
-
|
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
|
210
|
+
if data
|
211
|
+
data[:attributes] || {}
|
212
|
+
else
|
213
|
+
{}
|
214
|
+
end
|
211
215
|
end
|
212
216
|
|
213
217
|
def raw_relationships
|
214
|
-
data
|
218
|
+
if data
|
219
|
+
data[:relationships] || {}
|
220
|
+
else
|
221
|
+
{}
|
222
|
+
end
|
215
223
|
end
|
216
224
|
end
|