Skip to content

Commit e23eb93

Browse files
committed
Add some syntactic sugar
1 parent 4ceda48 commit e23eb93

File tree

4 files changed

+27
-20
lines changed

4 files changed

+27
-20
lines changed

lib/jsonapi/utils/exceptions.rb

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,21 +26,25 @@ class BadRequest < ::JSONAPI::Exceptions::Error
2626
def code; '400' end
2727

2828
def errors
29-
[JSONAPI::Error.new(code: code,
30-
status: :bad_request,
31-
title: 'Bad Request',
32-
detail: 'This request is not supported.')]
29+
[JSONAPI::Error.new(
30+
code: code,
31+
status: :bad_request,
32+
title: 'Bad Request',
33+
detail: 'This request is not supported.'
34+
)]
3335
end
3436
end
3537

3638
class InternalServerError < ::JSONAPI::Exceptions::Error
3739
def code; '500' end
3840

3941
def errors
40-
[JSONAPI::Error.new(code: code,
41-
status: :internal_server_error,
42-
title: 'Internal Server Error',
43-
detail: 'An internal error ocurred while processing the request.')]
42+
[JSONAPI::Error.new(
43+
code: code,
44+
status: :internal_server_error,
45+
title: 'Internal Server Error',
46+
detail: 'An internal error ocurred while processing the request.'
47+
)]
4448
end
4549
end
4650
end

lib/jsonapi/utils/response/formatters.rb

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,6 @@ def jsonapi_format_errors(data)
2121

2222
protected
2323

24-
def correct_media_type
25-
if response.body.size > 0
26-
response.headers['Content-Type'] = JSONAPI::MEDIA_TYPE
27-
end
28-
end
29-
3024
def build_response_document(records, options)
3125
results = JSONAPI::OperationResults.new
3226

lib/jsonapi/utils/response/support.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,14 @@ module Support
1111
include ::JSONAPI::Utils::Support::Filter
1212
include ::JSONAPI::Utils::Support::Pagination
1313
include ::JSONAPI::Utils::Support::Sort
14+
15+
protected
16+
17+
def correct_media_type
18+
if response.body.size > 0
19+
response.headers['Content-Type'] = JSONAPI::MEDIA_TYPE
20+
end
21+
end
1422
end
1523
end
1624
end

spec/controllers/posts_controller_spec.rb

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,14 @@
88
let(:fields) { (PostResource.fetchable_fields - %i(id author)).map(&:to_s) }
99
let(:relationships) { %w(author) }
1010
let(:first_post) { Post.first }
11+
let(:user_id) { first_post.user_id }
1112

1213
let(:attributes) do
1314
{ title: 'Lorem ipsum', body: 'Lorem ipsum dolor sit amet.' }
1415
end
1516

1617
let(:author_params) do
17-
{ data: { type: 'users', id: first_post.user_id } }
18+
{ data: { type: 'users', id: user_id } }
1819
end
1920

2021
let(:post_params) do
@@ -27,7 +28,7 @@
2728
describe '#index' do
2829
context 'with ActiveRecord::Relation' do
2930
it 'renders a collection of users' do
30-
get :index, user_id: first_post.user_id
31+
get :index, user_id: user_id
3132
expect(response).to have_http_status :ok
3233
expect(response).to have_primary_data('posts')
3334
expect(response).to have_data_attributes(fields)
@@ -50,7 +51,7 @@
5051
describe '#show' do
5152
context 'with ActiveRecord' do
5253
it 'renders a single post' do
53-
get :show, user_id: first_post.user_id, id: first_post.id
54+
get :show, user_id: user_id, id: first_post.id
5455
expect(response).to have_http_status :ok
5556
expect(response).to have_primary_data('posts')
5657
expect(response).to have_data_attributes(fields)
@@ -73,7 +74,7 @@
7374
context 'when resource was not found' do
7475
context 'with conventional id' do
7576
it 'renders a 404 response' do
76-
get :show, user_id: first_post.user_id, id: 999
77+
get :show, user_id: user_id, id: 999
7778
expect(response).to have_http_status :not_found
7879
expect(error['title']).to eq('Record not found')
7980
expect(error['detail']).to include('999')
@@ -85,7 +86,7 @@
8586
let(:uuid) { SecureRandom.uuid }
8687

8788
it 'renders a 404 response' do
88-
get :show, user_id: first_post.user_id, id: uuid
89+
get :show, user_id: user_id, id: uuid
8990
expect(response).to have_http_status :not_found
9091
expect(error['title']).to eq('Record not found')
9192
expect(error['detail']).to include(uuid)
@@ -97,7 +98,7 @@
9798
let(:slug) { 'some-awesome-slug' }
9899

99100
it 'renders a 404 response' do
100-
get :show, user_id: first_post.user_id, id: slug
101+
get :show, user_id: user_id, id: slug
101102
expect(response).to have_http_status :not_found
102103
expect(error['title']).to eq('Record not found')
103104
expect(error['detail']).to include(slug)

0 commit comments

Comments
 (0)