jsonapi-resources 0.0.11 → 0.0.12
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/jsonapi/exceptions.rb +17 -3
- data/lib/jsonapi/operation.rb +3 -3
- data/lib/jsonapi/resource.rb +1 -12
- data/lib/jsonapi/resource_controller.rb +7 -6
- data/lib/jsonapi/resources/version.rb +1 -1
- data/test/controllers/controller_test.rb +33 -3
- data/test/fixtures/active_record.rb +19 -2
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8dde1ba06781fcada973e949ba64e7b7821c8b23
|
4
|
+
data.tar.gz: cf3aefe531485a4ed3efeba48cf9b7deecf78a74
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 89cb1ccd8c7b620cced0007df3735ddec354e972e2a7f1a88907277089a5e509a23c7e90bb877bf676fd5edd6e71a49028b698fb888656a37854361e40a25c83
|
7
|
+
data.tar.gz: 2a7211219f7ae1849220122d49fc785b41639a48b79d3666fa8da553957ed6f09031e61c2aad6311117b558f8fc73232f9094ebd8c2bc914ff6c73e1779a0202
|
data/lib/jsonapi/exceptions.rb
CHANGED
@@ -223,9 +223,23 @@ module JSONAPI
|
|
223
223
|
end
|
224
224
|
|
225
225
|
class ValidationErrors < Error
|
226
|
-
attr_accessor :
|
227
|
-
def initialize(
|
228
|
-
@
|
226
|
+
attr_accessor :messages
|
227
|
+
def initialize(messages)
|
228
|
+
@messages = messages
|
229
|
+
end
|
230
|
+
|
231
|
+
def errors
|
232
|
+
messages.inject([]) do |arr, element|
|
233
|
+
arr.concat(
|
234
|
+
element[1].map do |message|
|
235
|
+
JSONAPI::Error.new(code: JSONAPI::VALIDATION_ERROR,
|
236
|
+
status: :unprocessable_entity,
|
237
|
+
title: "#{element[0]} - #{message}",
|
238
|
+
detail: message,
|
239
|
+
path: "/#{element[0]}")
|
240
|
+
end
|
241
|
+
)
|
242
|
+
end
|
229
243
|
end
|
230
244
|
end
|
231
245
|
|
data/lib/jsonapi/operation.rb
CHANGED
@@ -27,7 +27,7 @@ module JSONAPI
|
|
27
27
|
return JSONAPI::OperationResult.new(:created, resource)
|
28
28
|
|
29
29
|
rescue JSONAPI::Exceptions::Error => e
|
30
|
-
return JSONAPI::OperationResult.new(e.errors
|
30
|
+
return JSONAPI::OperationResult.new(e.errors[0].code, nil, e.errors)
|
31
31
|
end
|
32
32
|
end
|
33
33
|
|
@@ -48,7 +48,7 @@ module JSONAPI
|
|
48
48
|
record_locked_error = JSONAPI::Exceptions::RecordLocked.new(e.message)
|
49
49
|
return JSONAPI::OperationResult.new(record_locked_error.errors[0].code, nil, record_locked_error.errors)
|
50
50
|
rescue JSONAPI::Exceptions::Error => e
|
51
|
-
return JSONAPI::OperationResult.new(e.errors
|
51
|
+
return JSONAPI::OperationResult.new(e.errors[0].code, nil, e.errors)
|
52
52
|
end
|
53
53
|
end
|
54
54
|
|
@@ -185,4 +185,4 @@ module JSONAPI
|
|
185
185
|
return JSONAPI::OperationResult.new(:no_content)
|
186
186
|
end
|
187
187
|
end
|
188
|
-
end
|
188
|
+
end
|
data/lib/jsonapi/resource.rb
CHANGED
@@ -100,18 +100,7 @@ module JSONAPI
|
|
100
100
|
def save
|
101
101
|
@model.save!
|
102
102
|
rescue ActiveRecord::RecordInvalid => e
|
103
|
-
errors
|
104
|
-
e.record.errors.messages.each do |element|
|
105
|
-
element[1].each do |message|
|
106
|
-
errors.push(JSONAPI::Error.new(
|
107
|
-
code: JSONAPI::VALIDATION_ERROR,
|
108
|
-
status: :bad_request,
|
109
|
-
title: "#{element[0]} - #{message}",
|
110
|
-
detail: "can't be blank",
|
111
|
-
path: "\\#{element[0]}"))
|
112
|
-
end
|
113
|
-
end
|
114
|
-
raise JSONAPI::Exceptions::ValidationErrors.new(errors)
|
103
|
+
raise JSONAPI::Exceptions::ValidationErrors.new(e.record.errors.messages)
|
115
104
|
end
|
116
105
|
|
117
106
|
# Override this on a resource instance to override the fetchable keys
|
@@ -120,7 +120,8 @@ module JSONAPI
|
|
120
120
|
end
|
121
121
|
|
122
122
|
def parse_key_array(raw)
|
123
|
-
|
123
|
+
keys = raw.nil? || raw.empty? ? [] : raw.split(',')
|
124
|
+
resource_klass.verify_keys(keys, context)
|
124
125
|
end
|
125
126
|
|
126
127
|
# override to set context
|
@@ -142,8 +143,8 @@ module JSONAPI
|
|
142
143
|
{}
|
143
144
|
end
|
144
145
|
|
145
|
-
def render_errors(errors
|
146
|
-
render(json: {errors: errors}, status: errors
|
146
|
+
def render_errors(errors)
|
147
|
+
render(json: {errors: errors}, status: errors[0].status)
|
147
148
|
end
|
148
149
|
|
149
150
|
def process_request_operations
|
@@ -163,17 +164,17 @@ module JSONAPI
|
|
163
164
|
end
|
164
165
|
|
165
166
|
if errors.count > 0
|
166
|
-
render :
|
167
|
+
render status: errors[0].status, json: {errors: errors}
|
167
168
|
else
|
168
169
|
if results.length > 0 && resources.length > 0
|
169
|
-
render :
|
170
|
+
render status: results[0].code,
|
170
171
|
json: JSONAPI::ResourceSerializer.new.serialize_to_hash(resources.length > 1 ? resources : resources[0],
|
171
172
|
include: @request.include,
|
172
173
|
fields: @request.fields,
|
173
174
|
attribute_formatters: attribute_formatters,
|
174
175
|
key_formatter: key_formatter)
|
175
176
|
else
|
176
|
-
render :
|
177
|
+
render status: results[0].code, json: nil
|
177
178
|
end
|
178
179
|
end
|
179
180
|
rescue => e
|
@@ -285,7 +285,7 @@ class PostsControllerTest < ActionController::TestCase
|
|
285
285
|
}
|
286
286
|
}
|
287
287
|
|
288
|
-
assert_response :
|
288
|
+
assert_response :unprocessable_entity
|
289
289
|
# Todo: check if this validation is working
|
290
290
|
assert_match /author - can't be blank/, response.body
|
291
291
|
end
|
@@ -307,6 +307,29 @@ class PostsControllerTest < ActionController::TestCase
|
|
307
307
|
assert_match /asdfg is not allowed/, response.body
|
308
308
|
end
|
309
309
|
|
310
|
+
def test_create_with_invalid_data
|
311
|
+
post :create,
|
312
|
+
{
|
313
|
+
posts: {
|
314
|
+
title: 'JSONAPIResources is the greatest thing...',
|
315
|
+
body: 'JSONAPIResources is the greatest thing since unsliced bread.',
|
316
|
+
links: {
|
317
|
+
author: nil
|
318
|
+
}
|
319
|
+
}
|
320
|
+
}
|
321
|
+
|
322
|
+
assert_response :unprocessable_entity
|
323
|
+
|
324
|
+
assert_equal "/author", json_response['errors'][0]['path']
|
325
|
+
assert_equal "can't be blank", json_response['errors'][0]['detail']
|
326
|
+
assert_equal "author - can't be blank", json_response['errors'][0]['title']
|
327
|
+
|
328
|
+
assert_equal "/title", json_response['errors'][1]['path']
|
329
|
+
assert_equal "is too long (maximum is 35 characters)", json_response['errors'][1]['detail']
|
330
|
+
assert_equal "title - is too long (maximum is 35 characters)", json_response['errors'][1]['title']
|
331
|
+
end
|
332
|
+
|
310
333
|
def test_create_multiple
|
311
334
|
post :create,
|
312
335
|
{
|
@@ -1198,7 +1221,7 @@ class PeopleControllerTest < ActionController::TestCase
|
|
1198
1221
|
}
|
1199
1222
|
}
|
1200
1223
|
|
1201
|
-
assert_response :
|
1224
|
+
assert_response :unprocessable_entity
|
1202
1225
|
assert_equal 2, json_response['errors'].size
|
1203
1226
|
assert_equal JSONAPI::VALIDATION_ERROR, json_response['errors'][0]['code']
|
1204
1227
|
assert_equal JSONAPI::VALIDATION_ERROR, json_response['errors'][1]['code']
|
@@ -1215,7 +1238,7 @@ class PeopleControllerTest < ActionController::TestCase
|
|
1215
1238
|
}
|
1216
1239
|
}
|
1217
1240
|
|
1218
|
-
assert_response :
|
1241
|
+
assert_response :unprocessable_entity
|
1219
1242
|
assert_equal 1, json_response['errors'].size
|
1220
1243
|
assert_equal JSONAPI::VALIDATION_ERROR, json_response['errors'][0]['code']
|
1221
1244
|
assert_match /name - can't be blank/, response.body
|
@@ -1350,3 +1373,10 @@ class BreedsControllerTest < ActionController::TestCase
|
|
1350
1373
|
end
|
1351
1374
|
|
1352
1375
|
end
|
1376
|
+
|
1377
|
+
class Api::V2::PreferencesControllerTest < ActionController::TestCase
|
1378
|
+
def test_show_singleton_resource_without_id
|
1379
|
+
get :show
|
1380
|
+
assert_response :success
|
1381
|
+
end
|
1382
|
+
end
|
@@ -5,12 +5,17 @@ require 'jsonapi/exceptions'
|
|
5
5
|
require 'rails'
|
6
6
|
require 'rails/all'
|
7
7
|
|
8
|
+
ActiveSupport::Inflector.inflections(:en) do |inflect|
|
9
|
+
inflect.uncountable 'preferences'
|
10
|
+
end
|
11
|
+
|
8
12
|
### DATABASE
|
9
13
|
ActiveRecord::Schema.define do
|
10
14
|
create_table :people, force: true do |t|
|
11
15
|
t.string :name
|
12
16
|
t.string :email
|
13
17
|
t.datetime :date_joined
|
18
|
+
t.belongs_to :preferences
|
14
19
|
t.timestamps
|
15
20
|
end
|
16
21
|
|
@@ -94,6 +99,7 @@ class Person < ActiveRecord::Base
|
|
94
99
|
has_many :posts, foreign_key: 'author_id'
|
95
100
|
has_many :comments, foreign_key: 'author_id'
|
96
101
|
has_many :expense_entries, foreign_key: 'employee_id', dependent: :restrict_with_exception
|
102
|
+
belongs_to :preferences
|
97
103
|
|
98
104
|
### Validations
|
99
105
|
validates :name, presence: true
|
@@ -107,6 +113,7 @@ class Post < ActiveRecord::Base
|
|
107
113
|
belongs_to :section
|
108
114
|
|
109
115
|
validates :author, presence: true
|
116
|
+
validates :title, length: { maximum: 35 }
|
110
117
|
end
|
111
118
|
|
112
119
|
class Comment < ActiveRecord::Base
|
@@ -148,6 +155,11 @@ class Moon < ActiveRecord::Base
|
|
148
155
|
belongs_to :planet
|
149
156
|
end
|
150
157
|
|
158
|
+
class Preferences < ActiveRecord::Base
|
159
|
+
has_one :author, class_name: 'Person'
|
160
|
+
has_many :friends, class_name: 'Person'
|
161
|
+
end
|
162
|
+
|
151
163
|
class Breed
|
152
164
|
|
153
165
|
def initialize(id = nil, name = nil)
|
@@ -470,8 +482,12 @@ class PreferencesResource < JSONAPI::Resource
|
|
470
482
|
attribute :id
|
471
483
|
attribute :advanced_mode
|
472
484
|
|
473
|
-
has_one :author,
|
474
|
-
has_many :friends
|
485
|
+
has_one :author, foreign_key: :person_id
|
486
|
+
has_many :friends
|
487
|
+
|
488
|
+
def self.find_by_key(key, context: nil)
|
489
|
+
new(Preferences.first)
|
490
|
+
end
|
475
491
|
end
|
476
492
|
|
477
493
|
warn 'start testing Name Collisions'
|
@@ -643,3 +659,4 @@ betay = Planet.create(name: 'Beta X', description: 'Newly discovered Planet Y',
|
|
643
659
|
betaz = Planet.create(name: 'Beta X', description: 'Newly discovered Planet Z', planet_type_id: unknown.id)
|
644
660
|
betaw = Planet.create(name: 'Beta W', description: 'Newly discovered Planet W')
|
645
661
|
|
662
|
+
preference = Preferences.create
|
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.0.
|
4
|
+
version: 0.0.12
|
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: 2014-11-
|
12
|
+
date: 2014-11-29 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: bundler
|