File tree Expand file tree Collapse file tree 4 files changed +19
-15
lines changed Expand file tree Collapse file tree 4 files changed +19
-15
lines changed Original file line number Diff line number Diff line change 3
3
require 'jsonapi_errorable/version'
4
4
require 'jsonapi_errorable/exception_handler'
5
5
require 'jsonapi_errorable/validatable'
6
- require 'jsonapi_errorable/serializers/serializable_validation '
6
+ require 'jsonapi_errorable/serializers/validation '
7
7
8
8
module JsonapiErrorable
9
9
def self . included ( klass )
Original file line number Diff line number Diff line change 1
1
module JsonapiErrorable
2
2
module Serializers
3
- class SerializableValidation < JSONAPI :: Serializable :: Resource
4
- type :validation_errors
3
+ class Validation
4
+ attr_reader :object
5
5
6
- attribute :errors do
7
- @object . errors . to_hash . map do |attribute , messages |
6
+ def initialize ( object )
7
+ @object = object
8
+ end
9
+
10
+ def errors
11
+ object . errors . to_hash . map do |attribute , messages |
8
12
messages . map do |message |
9
13
{
10
14
code : 'unprocessable_entity' ,
11
15
status : '422' ,
12
16
title : 'Validation Error' ,
13
17
detail : "#{ attribute . capitalize } #{ message } " ,
14
- source : { pointer : pointer_for ( @ object, attribute ) } ,
18
+ source : { pointer : pointer_for ( object , attribute ) } ,
15
19
meta : { attribute : attribute , message : message }
16
20
}
17
21
end
@@ -21,12 +25,12 @@ class SerializableValidation < JSONAPI::Serializable::Resource
21
25
def relationship? ( name )
22
26
return false unless activerecord?
23
27
24
- relation_names = @ object. class . reflect_on_all_associations . map ( &:name )
28
+ relation_names = object . class . reflect_on_all_associations . map ( &:name )
25
29
relation_names . include? ( name )
26
30
end
27
31
28
32
def attribute? ( name )
29
- @ object. respond_to? ( name )
33
+ object . respond_to? ( name )
30
34
end
31
35
32
36
private
Original file line number Diff line number Diff line change 1
1
module JsonapiErrorable
2
2
module Validatable
3
3
def render_errors_for ( record )
4
+ validation = Serializers ::Validation . new ( record )
5
+
4
6
render \
5
- json : record ,
6
- status : :unprocessable_entity ,
7
- serializer : Serializers ::SerializableValidation ,
8
- adapter : :attributes
7
+ json : validation . errors ,
8
+ status : :unprocessable_entity
9
9
end
10
10
end
11
11
end
Original file line number Diff line number Diff line change 1
1
require 'spec_helper'
2
2
3
- RSpec . describe JsonapiErrorable ::Serializers ::SerializableValidation do
3
+ RSpec . describe JsonapiErrorable ::Serializers ::Validation do
4
4
let ( :errors_hash ) { { username : [ "can't be blank" ] } }
5
5
6
6
let ( :object ) { double ( id : 123 ) . as_null_object }
7
- let ( :instance ) { described_class . new ( object : object ) }
7
+ let ( :instance ) { described_class . new ( object ) }
8
8
9
9
before do
10
10
allow ( instance ) . to receive ( :activerecord? ) { true }
15
15
end
16
16
17
17
describe '#errors' do
18
- subject { instance . as_jsonapi [ :attributes ] [ : errors] }
18
+ subject { instance . errors }
19
19
20
20
before do
21
21
allow ( object ) . to receive ( :respond_to? ) . with ( :username ) { true }
You can’t perform that action at this time.
0 commit comments