Skip to content

Commit cb0021f

Browse files
authored
Merge pull request #55 from tiagopog/update-jsonapi-resources-to-0-8-3
Update jsonapi-resources to v0.8.3
2 parents 0a11793 + 104ba66 commit cb0021f

File tree

11 files changed

+209
-175
lines changed

11 files changed

+209
-175
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,13 +59,13 @@ Support:
5959
For Rails 4 add this to your application's Gemfile:
6060

6161
```ruby
62-
gem 'jsonapi-utils', '~> 0.4.8'
62+
gem 'jsonapi-utils', '~> 0.4.9'
6363
```
6464

6565
For Rails 5:
6666

6767
```ruby
68-
gem 'jsonapi-utils', '~> 0.5.1'
68+
gem 'jsonapi-utils', '~> 0.5.2'
6969
```
7070

7171
And then execute:

jsonapi-utils.gemspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ Gem::Specification.new do |spec|
1919
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
2020
spec.require_paths = ['lib']
2121

22-
spec.add_runtime_dependency 'jsonapi-resources', '~> 0.8.0'
22+
spec.add_runtime_dependency 'jsonapi-resources', '0.8.3'
2323

2424
spec.add_development_dependency 'bundler', '~> 1.10'
2525
spec.add_development_dependency 'rake', '~> 10.0'

lib/jsonapi/utils/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
module JSONAPI
22
module Utils
3-
VERSION = '0.5.1'.freeze
3+
VERSION = '0.5.2'.freeze
44
end
55
end

spec/controllers/posts_controller_spec.rb

Lines changed: 47 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,19 @@
33
describe PostsController, type: :controller do
44
include_context 'JSON API headers'
55

6-
before(:all) { FactoryGirl.create_list(:post, 3) }
6+
before(:all) do
7+
@post = FactoryGirl.create_list(:post, 3).first
8+
end
79

810
before(:each) do
911
JSONAPI.configuration.json_key_format = :underscored_key
1012
end
1113

12-
let(:fields) { (PostResource.fields - %i(id author category)).map(&:to_s) }
13-
let(:relationships) { %w(author category) }
14-
let(:resource) { Post.first }
15-
let(:parent_id) { resource.user_id }
16-
let(:category_id) { resource.category_id }
14+
let(:relationships) { PostResource._relationships.keys.map(&:to_s) }
15+
let(:fields) { PostResource.fields.reject { |e| e == :id }.map(&:to_s) - relationships }
16+
let(:blog_post) { @post }
17+
let(:parent_id) { blog_post.user_id }
18+
let(:category_id) { blog_post.category_id }
1719

1820
let(:attributes) do
1921
{ title: 'Lorem ipsum', body: 'Lorem ipsum dolor sit amet.', content_type: 'article' }
@@ -38,7 +40,7 @@
3840
end
3941

4042
describe 'GET #index' do
41-
subject { get :index, params }
43+
subject { get :index, params: params }
4244

4345
let(:params) { { user_id: parent_id } }
4446

@@ -53,7 +55,7 @@
5355
end
5456

5557
context 'with Hash' do
56-
subject { get :index_with_hash, params }
58+
subject { get :index_with_hash, params: params }
5759

5860
it 'renders a collection of users' do
5961
expect(subject).to have_http_status :ok
@@ -148,19 +150,19 @@
148150

149151
describe 'GET #show' do
150152
context 'with ActiveRecord' do
151-
subject { get :show, id: resource.id }
153+
subject { get :show, params: { id: blog_post.id } }
152154

153155
it 'renders a single post' do
154156
expect(subject).to have_http_status :ok
155157
expect(subject).to have_primary_data('posts')
156158
expect(subject).to have_data_attributes(fields)
157159
expect(subject).to have_relationships(relationships)
158-
expect(data.dig('attributes', 'title')).to eq("Title for Post #{resource.id}")
160+
expect(data.dig('attributes', 'title')).to eq("Title for Post #{blog_post.id}")
159161
end
160162
end
161163

162164
context 'with Hash' do
163-
subject { get :show_with_hash, id: resource.id }
165+
subject { get :show_with_hash, params: { id: blog_post.id } }
164166

165167
it 'renders a single post' do
166168
expect(subject).to have_http_status :ok
@@ -173,7 +175,7 @@
173175

174176
context 'when resource was not found' do
175177
context 'with conventional id' do
176-
subject { get :show, id: 999 }
178+
subject { get :show, params: { id: 999 } }
177179

178180
it 'renders a 404 response' do
179181
expect(subject).to have_http_status :not_found
@@ -184,7 +186,7 @@
184186
end
185187

186188
context 'with uuid' do
187-
subject { get :show, id: uuid }
189+
subject { get :show, params: { id: uuid } }
188190

189191
let(:uuid) { SecureRandom.uuid }
190192

@@ -197,7 +199,7 @@
197199
end
198200

199201
context 'with slug' do
200-
subject { get :show, id: slug }
202+
subject { get :show, params: { id: slug } }
201203

202204
let(:slug) { 'some-awesome-slug' }
203205

@@ -212,7 +214,7 @@
212214
end
213215

214216
describe 'POST #create' do
215-
subject { post :create, params.merge(body) }
217+
subject { post :create, params: params.merge(body) }
216218

217219
let (:params) { { user_id: parent_id } }
218220

@@ -225,7 +227,7 @@
225227
end
226228

227229
context 'when validation fails on an attribute' do
228-
subject { post :create, params.merge(invalid_body) }
230+
subject { post :create, params: params.merge(invalid_body) }
229231

230232
let(:invalid_body) do
231233
body.tap { |b| b[:data][:attributes][:title] = nil }
@@ -234,15 +236,15 @@
234236
it 'renders a 422 response' do
235237
expect { subject }.to change(Post, :count).by(0)
236238
expect(response).to have_http_status :unprocessable_entity
237-
expect(errors[0]['id']).to eq('title')
238-
expect(errors[0]['title']).to eq('Title can\'t be blank')
239-
expect(errors[0]['code']).to eq('100')
240-
expect(errors[0]['source']['pointer']).to eq('/data/attributes/title')
239+
expect(errors.dig(0, 'id')).to eq('title')
240+
expect(errors.dig(0, 'title')).to eq('Title can\'t be blank')
241+
expect(errors.dig(0, 'code')).to eq('100')
242+
expect(errors.dig(0, 'source', 'pointer')).to eq('/data/attributes/title')
241243
end
242244
end
243245

244246
context 'when validation fails on a relationship' do
245-
subject { post :create, params.merge(invalid_body) }
247+
subject { post :create, params: params.merge(invalid_body) }
246248

247249
let(:invalid_body) do
248250
body.tap { |b| b[:data][:relationships][:author] = nil }
@@ -252,15 +254,15 @@
252254
expect { subject }.to change(Post, :count).by(0)
253255
expect(subject).to have_http_status :unprocessable_entity
254256

255-
expect(errors[0]['id']).to eq('author')
256-
expect(errors[0]['title']).to eq('Author can\'t be blank')
257-
expect(errors[0]['code']).to eq('100')
258-
expect(errors[0]['source']['pointer']).to eq('/data/relationships/author')
257+
expect(errors.dig(0, 'id')).to eq('author')
258+
expect(errors.dig(0, 'title')).to eq('Author can\'t be blank')
259+
expect(errors.dig(0, 'code')).to eq('100')
260+
expect(errors.dig(0, 'source', 'pointer')).to eq('/data/relationships/author')
259261
end
260262
end
261263

262264
context 'when validation fails on a foreign key' do
263-
subject { post :create, params.merge(invalid_body) }
265+
subject { post :create, params: params.merge(invalid_body) }
264266

265267
let(:invalid_body) do
266268
body.tap { |b| b[:data][:relationships][:category] = nil }
@@ -270,15 +272,15 @@
270272
expect { subject }.to change(Post, :count).by(0)
271273
expect(subject).to have_http_status :unprocessable_entity
272274

273-
expect(errors[0]['id']).to eq('category')
274-
expect(errors[0]['title']).to eq('Category can\'t be blank')
275-
expect(errors[0]['code']).to eq('100')
276-
expect(errors[0]['source']['pointer']).to eq('/data/relationships/category')
275+
expect(errors.dig(0, 'id')).to eq('category')
276+
expect(errors.dig(0, 'title')).to eq('Category can\'t be blank')
277+
expect(errors.dig(0, 'code')).to eq('100')
278+
expect(errors.dig(0, 'source', 'pointer')).to eq('/data/relationships/category')
277279
end
278280
end
279281

280282
context 'when validation fails on a private attribute' do
281-
subject { post :create, params.merge(invalid_body) }
283+
subject { post :create, params: params.merge(invalid_body) }
282284

283285
let(:invalid_body) do
284286
body.tap { |b| b[:data][:attributes][:title] = 'Fail Hidden' }
@@ -288,15 +290,15 @@
288290
expect { subject }.to change(Post, :count).by(0)
289291
expect(subject).to have_http_status :unprocessable_entity
290292

291-
expect(errors[0]['id']).to eq('hidden_field')
292-
expect(errors[0]['title']).to eq('Hidden field error was tripped')
293-
expect(errors[0]['code']).to eq('100')
294-
expect(errors[0]['source']).to be_nil
293+
expect(errors.dig(0, 'id')).to eq('hidden_field')
294+
expect(errors.dig(0, 'title')).to eq('Hidden field error was tripped')
295+
expect(errors.dig(0, 'code')).to eq('100')
296+
expect(errors.dig(0, 'source', 'pointer')).to be_nil
295297
end
296298
end
297299

298300
context 'when validation fails with a formatted attribute key' do
299-
subject { post :create, params.merge(invalid_body) }
301+
subject { post :create, params: params.merge(invalid_body) }
300302

301303
let(:invalid_body) do
302304
body.tap { |b| b[:data][:attributes][:title] = 'Fail Hidden' }
@@ -315,17 +317,17 @@
315317
expect { subject }.to change(Post, :count).by(0)
316318
expect(subject).to have_http_status :unprocessable_entity
317319

318-
expect(errors[0]['id']).to eq('content-type')
319-
expect(errors[0]['title']).to eq('Content type can\'t be blank')
320-
expect(errors[0]['code']).to eq('100')
321-
expect(errors[0]['source']['pointer']).to eq('/data/attributes/content-type')
320+
expect(errors.dig(0, 'id')).to eq('content-type')
321+
expect(errors.dig(0, 'title')).to eq('Content type can\'t be blank')
322+
expect(errors.dig(0, 'code')).to eq('100')
323+
expect(errors.dig(0, 'source', 'pointer')).to eq('/data/attributes/content-type')
322324
end
323325
end
324326
end
325327

326328
describe 'PATCH #update' do
327329
shared_context 'update request' do |action:|
328-
subject { patch action, params.merge(body) }
330+
subject { patch action, params: params.merge(body) }
329331

330332
let(:params) { { id: 1 } }
331333
let(:body) { { data: { id: 1, type: 'posts', attributes: { title: 'Foo' } } } }
@@ -343,10 +345,10 @@
343345
expect { subject }.to change(Post, :count).by(0)
344346
expect(response).to have_http_status :unprocessable_entity
345347

346-
expect(errors[0]['id']).to eq('base')
347-
expect(errors[0]['title']).to eq('This is an error on the base')
348-
expect(errors[0]['code']).to eq('100')
349-
expect(errors[0]['source']['pointer']).to eq('/data')
348+
expect(errors.dig(0, 'id')).to eq('base')
349+
expect(errors.dig(0, 'title')).to eq('This is an error on the base')
350+
expect(errors.dig(0, 'code')).to eq('100')
351+
expect(errors.dig(0, 'source', 'pointer')).to eq('/data')
350352
end
351353
end
352354
end

spec/controllers/profile_controller_spec.rb

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
describe ProfileController, type: :controller do
44
include_context 'JSON API headers'
55

6-
let(:fields) { (ProfileResource.fields - %i(id)).map(&:to_s) }
7-
let(:resource) { Profile }
8-
let(:attributes) { { location: 'Springfield, USA' } }
6+
let(:relationships) { ProfileResource._relationships.keys.map(&:to_s) }
7+
let(:fields) { ProfileResource.fields.reject { |e| e == :id }.map(&:to_s) - relationships }
8+
let(:attributes) { { nickname: 'Foobar', location: 'Springfield, USA' } }
99

1010
let(:body) do
1111
{
@@ -30,10 +30,10 @@
3030
it 'renders a 422 response' do
3131
patch :update, params: body
3232
expect(response).to have_http_status :unprocessable_entity
33-
expect(errors[0]['id']).to eq('location')
34-
expect(errors[0]['title']).to eq("Location can't be blank")
35-
expect(errors[0]['code']).to eq('100')
36-
expect(errors[0]['source']['pointer']).to eq('/data/attributes/location')
33+
expect(errors.dig(0, 'id')).to eq('nickname')
34+
expect(errors.dig(0, 'title')).to eq("Nickname can't be blank")
35+
expect(errors.dig(0, 'code')).to eq('100')
36+
expect(errors.dig(0, 'source', 'pointer')).to eq('/data/attributes/nickname')
3737
end
3838
end
3939
end

0 commit comments

Comments
 (0)