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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 693f8ed867afa11d9d23f0631bdf2ccd7e19acbe
4
- data.tar.gz: 5a0edbc2ab2a5f7c801c3120307c54c7c6a1142a
3
+ metadata.gz: 8dde1ba06781fcada973e949ba64e7b7821c8b23
4
+ data.tar.gz: cf3aefe531485a4ed3efeba48cf9b7deecf78a74
5
5
  SHA512:
6
- metadata.gz: e5f18920d6adee8dd748507071addacc51f58ba2719bf0961fb4fad01de7fb4af229755f90eb22b7c06ec20772fdff835fb6b7234b54c7a341d1eb6885ea879d
7
- data.tar.gz: 83901cfc4f1d2dad3883e782bf90cef7cbf62873faba9b872fb84b756ec71c5020bca86acba72cbc72617a55b2cc58bab3b12152a89cf20e5f9e6a2c40aaa9aa
6
+ metadata.gz: 89cb1ccd8c7b620cced0007df3735ddec354e972e2a7f1a88907277089a5e509a23c7e90bb877bf676fd5edd6e71a49028b698fb888656a37854361e40a25c83
7
+ data.tar.gz: 2a7211219f7ae1849220122d49fc785b41639a48b79d3666fa8da553957ed6f09031e61c2aad6311117b558f8fc73232f9094ebd8c2bc914ff6c73e1779a0202
@@ -223,9 +223,23 @@ module JSONAPI
223
223
  end
224
224
 
225
225
  class ValidationErrors < Error
226
- attr_accessor :errors
227
- def initialize(errors)
228
- @errors = errors
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
 
@@ -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.count == 1 ? e.errors[0].code : :bad_request, nil, 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.count == 1 ? e.errors[0].code : :bad_request, nil, 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
@@ -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
- return resource_klass.verify_keys(raw.split(/,/), context)
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, status = :bad_request)
146
- render(json: {errors: errors}, status: errors.count == 1 ? errors[0].status : status)
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 :status => errors.count == 1 ? errors[0].status : :bad_request, json: {errors: errors}
167
+ render status: errors[0].status, json: {errors: errors}
167
168
  else
168
169
  if results.length > 0 && resources.length > 0
169
- render :status => results[0].code,
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 :status => results[0].code, json: nil
177
+ render status: results[0].code, json: nil
177
178
  end
178
179
  end
179
180
  rescue => e
@@ -1,5 +1,5 @@
1
1
  module JSONAPI
2
2
  module Resources
3
- VERSION = "0.0.11"
3
+ VERSION = "0.0.12"
4
4
  end
5
5
  end
@@ -285,7 +285,7 @@ class PostsControllerTest < ActionController::TestCase
285
285
  }
286
286
  }
287
287
 
288
- assert_response :bad_request
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 :bad_request
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 :bad_request
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, class_name: 'Person'
474
- has_many :friends, class_name: 'Person'
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.11
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-18 00:00:00.000000000 Z
12
+ date: 2014-11-29 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler