@@ -16,18 +16,13 @@ def initialize(resource, options = {})
16
16
@resource = resource
17
17
@adapter_opts , @serializer_opts =
18
18
options . partition { |k , _ | ADAPTER_OPTION_KEYS . include? k } . map { |h | Hash [ h ] }
19
+ end
19
20
20
- # TECHDEBT: clean up single vs. collection of resources
21
+ def errors?
21
22
if resource . respond_to? ( :each )
22
- if resource . any? { |elem | elem . respond_to? ( :errors ) && !elem . errors . empty? }
23
- @serializer_opts [ :serializer ] = ActiveModel ::Serializer ::ErrorSerializer
24
- @adapter_opts [ :adapter ] = :'json_api/error'
25
- end
23
+ resource . any? { |elem | elem . respond_to? ( :errors ) && !elem . errors . empty? }
26
24
else
27
- if resource . respond_to? ( :errors ) && !resource . errors . empty?
28
- @serializer_opts [ :serializer ] = ActiveModel ::Serializer ::ErrorSerializer
29
- @adapter_opts [ :adapter ] = :'json_api/error'
30
- end
25
+ resource . respond_to? ( :errors ) && !resource . errors . empty?
31
26
end
32
27
end
33
28
@@ -44,7 +39,11 @@ def serialization_scope_name=(scope_name)
44
39
end
45
40
46
41
def adapter
47
- @adapter ||= ActiveModelSerializers ::Adapter . create ( serializer_instance , adapter_opts )
42
+ @adapter ||=
43
+ begin
44
+ adapter_opts [ :adapter ] = :'json_api/error' if errors?
45
+ ActiveModelSerializers ::Adapter . create ( serializer_instance , adapter_opts )
46
+ end
48
47
end
49
48
alias_method :adapter_instance , :adapter
50
49
@@ -59,6 +58,7 @@ def serializer
59
58
@serializer ||=
60
59
begin
61
60
@serializer = serializer_opts . delete ( :serializer )
61
+ @serializer = ActiveModel ::Serializer ::ErrorSerializer if errors?
62
62
@serializer ||= ActiveModel ::Serializer . serializer_for ( resource )
63
63
64
64
if serializer_opts . key? ( :each_serializer )
0 commit comments